Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Data Structure for what I am trying to achieve?

Discussion in 'General Discussion' started by DishwashaBubble, Nov 21, 2023.

  1. DishwashaBubble


    Oct 19, 2023

    I am making a game about washing dirty dishes. I have plate GameObjects and I want to attach certain substance scripts to them ex: cheese, spinach, ketchup. (Eventually will have a visual component.)

    I want each substance object to have its own float subdata such as hardness, crunchiness, temperature, etc. that can be set to default values for each substance. (ex: 0.5, 0.1, etc.)

    - I want to do this in a way that has as much re-used code as possible. For instance I might like all of the scripts to realize that 'temperature' is a float without me having to re-code it in each substance, etc. Not really necessary but more of a best practices kind of thing.

    -The subdata values can be changed dynamically on an instanced object. (i.e. cheese on an in-game plate can be made less hard by spraying it with water)

    My thinking so far
    I'm familiar with unity scripts, as well as the concept of objects and classes. I could just make a bunch of scripts, each w/ a class representing a different substance? This would then be instanced per plate GameObject automatically, according to the unity docs.

    Regarding the subdata type, Two options:
    1.) Forget the idea of making subdata types their own structure, since they'll literally be floats, and just manually code them into each substance as needed.
    2.) The actual subdata types would probably need to each have their own script/class also with a public member called value storing the actual float within.

    It just seems like a lot of scripts to create.

    Any guidance is appreciated. Thank you.
  2. neginfinity


    Jan 27, 2013
    It is a simple struct or class. With fields "hardness", "crunchiness", "temperature".

    Because each substance has its own values, and you attach it to objects, then the default values should be a ScriptableObject. For each substance you'll be creating asset and entering values by hand.

    Because the values can be altered, you can create a copy of the structure, which stores altered version of original configuration with modifiers applied. So, basically.

    Code (csharp):
    2. public class SubstanceConfig{
    3.     public float hardness;
    4.     public float crunchiness;
    5.     public float temperature;
    6. }
    8. public class SubstanceAsset: ScriptableObject{
    9.     public SubstanceConfig config;
    10. }
    12. public class InGameData: MonoBehaviour{
    13.     [SerializeField]SubstanceAsset defaultSubstance;
    14.     SubstanceConfig currentSubstance;
    15. }
    DishwashaBubble likes this.