Search Unity

Making unlockable systems

Discussion in 'Scripting' started by MrZeker, Jul 31, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    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#.
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    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.
     
    MrZeker likes this.
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    What if player have multiple saved games and unlocked different items?
     
    MrZeker likes this.
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    I assume that the save system would serialize the changes from default.
     
    MrZeker likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    MrZeker likes this.
  6. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    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.
     
    MrZeker, palex-nx and Joe-Censored like this.
  7. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    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?
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    No because more unlocked items means more string comparsions when checking arbitrary item unlocked, while with booleans you always do only 1 bool check.
     
    MrZeker likes this.
  9. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Add this to your class being saved :
    Code (CSharp):
    1. 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 :
    Code (CSharp):
    1. if (unlockedItems.Contains(251)) // Returns true if the value is in the list
    2. {
    3. }
    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.
     
    MrZeker likes this.
  10. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    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.
     
  11. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    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.
     
  12. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    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 :\
     
  13. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Any help on that please?
     
  14. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  15. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    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.