Navigate

Sunday, May 27, 2018

Generating a Shopping List

(Before I get started: there is some code from the game at the bottom, for those interested in reviewing and providing feedback!)

So with a my First-Person Controller and Projectile working...ahem...perfectly...hack (*sorry, fibbing dries out my throat), it was time to generate a shopping list for my FPS shopping simulator. It didn't go as smoothly as I anticipated.

A colleague is currently in school for video game development and wants to focus on UI. I asked if she would like to help on the shopping list element (which I would like to be much fancier than what I accomplished), until she can beef it up--I just wanted it working.

I decided to give the player 8 items to find.

...You know...because...

And I thought for replay, the items should be random. So I needed:
1. The Game needs to pick 8 items at random from a pool.
2. Generate a UI list on screen of those 8 randomly chosen items.
3. Calculate a budget for the player.
4. Keep tabs of the total cost of all items in the shopping cart and track whether we're staying in budget.

Look at all those items on the shelves!


Public Variables -- AMAZING!
I started by first creating 16 items. Until art is available, I made them solid colored cereal box sized objects and named them after their individual colors.



I then created a simple script to hold properties for each object so that I could utilize them within scripts. I think the properties are pretty self explanatory, unless you've never worked retail--then Sku might be confusing. It's usually just a number (different from the UPC) that represents the product within the retailer's database.



Such shopping carts


I forgot to mention. I also made a shopping cart. It's just sprites and invisible collision boxes. I also put a trigger box inside the cart to keep track of what items where falling inside of it. So it's on this trigger box that I decided to create my list.




To Generate my shopping list I created a list and placed every single shopping item's prefab into it, and then I created another list that would eventually become my shopping list. I used a For Loop to randomly pick 8 elements from my product pool and added them to my shopping list. I had to use .remove to take the chosen items out of the first list so they would not be accidentally chosen twice. I then set each item chosen to a UI Text object. It worked pretty flawlessly!


 Here's a brief sample of the script I used to create the list. You'll also see that I calculate a budget for the player. After the objects are chosen, I cycle through them and get all their prices. I add those prices up and multiply them by 1.10 to make the budget 110% of the cost of all the required; which can be adjusted if needed.


No comments:

Post a Comment