Search Unity

Best Practices for Random Events?

Discussion in 'Game Design' started by Redlar, Nov 22, 2022.

  1. Redlar

    Redlar

    Joined:
    Aug 17, 2016
    Posts:
    16
    As a general question, what are some best practices for storing and accessing random events in a Unity game?

    So far I've just been writing big JSON files that store text elements, image names, and various requirements based on player stats or items. However, adding new events or event chains feels very clunky, and there's no easy way to track the event relationships or arrange them in an Editor-viewable format using that JSON.

    I've thought about creating a custom inspector for a custom "event" class so that I can do everything in the editor, but I'm not sure if that's worth the time.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Maybe a scriptable object for each event with the results, all needed data and a chance to happen.
    Then have 1 class with simply an array of the objects
     
    BrandyStarbrite, Redlar and Niter88 like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,001
    How do events and the json data relate? What kind of events, Unity event or C# event or game specific actions?

    Depending on what you are trying to accomplish it may be helpful to look at some assets for guidance or even just using it, for example a fully fleshed out resource, items and crafting system.

    If by events you mean something more like scripting you should also take a look at GraphView and assets that use graphs like NodeCanvas, BehaviourTree and others to script (conditional) action sequences without having to resort to low-level Visual Scripting.
     
    Redlar and DevDunk like this.
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    What about Excel? That way you can see them all in a relatively compact table form, run comparisons, and easily tweak numbers.

    It's pretty simple to export Excel to CSV and read it into Unity.
     
    Redlar likes this.
  5. Redlar

    Redlar

    Joined:
    Aug 17, 2016
    Posts:
    16
    Thanks for this. I can't remember why I went JSON over CSV back when I first started messing with Unity but it would make things a lot easier.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    It might have been because Unity has a built-in JsonUtility class, but no corresponding class for CSV. Fortunately CSV is fairly easy to parse, and I'm sure you can find some code in a github gist if you don't feel like typing it up yourself. Or you can use Unity-Quicksheet to import xlsx files directly so you don't have to export to CSV.
     
    Redlar likes this.