Hello everyone, im trying to build a shop unlock system for additional costumes in my game. I need some suggestions on how to best implement it:
I already have a binary formatting save system, but my question, since there is like 200s items.
Should i just use one bool for each item? and have like 200 booleans?
or is there a better way? if so, can you show me a little of code of that idea?
im still kinda new to c#.
If every item has the potential to be locked or unlocked, I’d make that a bool field in the item. So, each item knows if it is locked or not, and the store asks the item if it is locked when displaying it.
What if player have multiple saved games and unlocked different items?
I assume that the save system would serialize the changes from default.
What do players do to unlock these items?
For example, in my game learning skills is the primary way of unlocking items which can be purchased in the in game market, and skill progress occurs continually even when the game isn’t running. So when checking if a player has unlocked the ability to use an item, I recalculate their progress on the relevant skills. What I save is just their current progress on skills, not whether they have unlocked any items.
If you went with saving 200 bools, you’d probably just have an array or list of bools you save instead of 200 individual bools. Code would be much simpler.
Stupid simple way is to give your items a unique identifier and add unlocked items to a list. Then just check against the list to see what items have been unlocked.
yes!
im not quite sure how to do it, but im guessing that is better than having 200 bools.
so i will have to make a unlockedStuff list, im not sure how to add stuff to the list to be honest, but that would work? and on the Start function ask the game to load the list so everything that is bought is unlocked?
No because more unlocked items means more string comparsions when checking arbitrary item unlocked, while with booleans you always do only 1 bool check.
Add this to your class being saved :
public List<int> unlockedItems = new List<int>();
List has a few interesting methods, including .Contains(). Say your wooden short sword has the GUID 251, you can do this, assuming unlockedItems is a list of integers :
if (unlockedItems.Contains(251)) // Returns true if the value is in the list
{
}
You add to the list with .Add(), remove with .Remove() and you use .Count instead of the array’s .Length when you want to know how many elements are in the list.
Problem is, im going to use that in a character selection screen for costumes, wouldn’t that be expensive in update function? I mean, using alist looks like the best solution, but i dont know much about optimization and just tend to avoid stuff in update unless is necessary.
You said 200ish items? No it wouldn’t be expensive. Lists are highly optimized. But more importantly, why in the world do you believe you need to run this code every frame?
You only need to check for items status once when the character UI is brought up and when the user performs unlock actions. Say when they watch an ad to unlock, or spend their coins, or whatever.
Update is for things that need to be updated every frame. Let’s say you’re talking about your typical casual mobile game character screen. If you wanted to be able to scroll through your characters while dragging your finger on the screen, the movement code would be in update. It NEEDS to be updated every frame in order to reflect the user’s input.
im trying to figure out how to use the unlocked/locked stuff in the character selection.
what i got in mind is, when the character is selected, on the same script in the update that changes what is displayed and stats and all that things, it would also have to check if the costume 2 is unlocked, to enable the character to be selected or not.
that’s why i think it goes in update :\
Any help on that please?
What’s wrong with recalculating the unlocked/locked stuff immediately before all the UI stuff on your character selection? You shouldn’t be redoing that on every update. Just whenever you are rebuilding the UI, which is probably just the frame in which the UI opens.
nothing, i just cant figure out , how to check in the character selection (when selecting each character) to enable/disable the costume selection.
My guess so far is that with a list i would have to use another variable to be checked (in update) when the character is selected, to see if it enables or not to be picked with that costume.