Search Unity

Coding Situations

Discussion in 'Scripting' started by Teddles, Aug 13, 2019.

  1. Teddles

    Teddles

    Joined:
    Aug 13, 2019
    Posts:
    5
    Hi there,

    I was wondering how would you code a game like 60 Seconds!, where each day you get a random event to happen... Would you store these events in a class with different variables like textOutput (The text it gives you, in that case - in the book), options (if you can accept someone in the shelter or deny them from coming) etc... And then store multiple variants of that class somewhere, maybe in an arraylist? I was thinking about it and with my coding knowledge I cant find a simple way to do it, altho there probably is one.

    Would love to know more about this, so if anyone has any idea please do share.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Yeah you would want to create a class, probably a scriptable object, which stores all the data which represents an event. The scriptable object is like the blueprint for the event. Then you can make many instances of that scriptable object, and create all kinds of different data variations. You can store those objects in an array or list, and select randomly at runtime.
     
  3. Teddles

    Teddles

    Joined:
    Aug 13, 2019
    Posts:
    5
    Thank you so much for replying, I had a quick look at scriptable objects and that is most likely what I need. Much love.
     
    LiterallyJeff likes this.
  4. Teddles

    Teddles

    Joined:
    Aug 13, 2019
    Posts:
    5
    I have one more question about this.

    So I looked into ScriptableObjects and it seems that you would have to manually reference each ScriptableObject you create by drag and dropping it on the other script and then putting them all into an array or a list. Is there any way to reference all of them at the same time? Maybe something like looping through all of the items in a specific folder full of these certain events?

    Otherwise, what jeffreyschoch suggested worked perfectly, I'm just looking if there's any way to automate this.
     
  5. Grizmu

    Grizmu

    Joined:
    Aug 27, 2013
    Posts:
    131
    Teddles likes this.
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The linked solution above only works in the editor. So you could create an editor script to find and assign them all.

    You could also load them from a Resources folder to get all of them in one go at runtime.

    https://docs.unity3d.com/ScriptReference/Resources.Load.html

    It's also possible to lock the inspector with the little lock icon at the top, then select and drag all of the scriptable objects to the empty list/array field, and it will populate with all of dragged items.
     
    Teddles and Grizmu like this.
  7. Teddles

    Teddles

    Joined:
    Aug 13, 2019
    Posts:
    5
    So from my understanding, it would be alright to just make multiple SOs for events? I am talking about hundreds of events, which would result in hundreds of SOs... I tried making one SO contain multiple events with lists, but I could not figure out how I would keep the choices in pairs - so an event would have 2 choices and they would be pre-set for each event, meaning if I would use a list, I would somehow have to use a twodimensional list or something like that... Just a list that can get two strings per one entry...
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Generally you define a SO, then make many instances of that SO for all the variants of it. For instance you could make an ItemData SO and then make a new instance for each item in your game.

    I'm not sure exactly how your system works, but if you just need two strings to be associated you can do something like this:
    Code (CSharp):
    1. [Serializable]
    2. public class EventPair
    3. {
    4.     public string A;
    5.     public string B;
    6. }
    7.  
    8. public List<EventPair> eventPairs;
     
  9. Teddles

    Teddles

    Joined:
    Aug 13, 2019
    Posts:
    5
    MAD! You're a genious and saviour @jeffreyschoch

    Thank you both so much for everything!