Search Unity

Unity Card Game Structure

Discussion in 'Scripting' started by Raes93, Nov 16, 2020.

  1. Raes93

    Raes93

    Joined:
    Dec 6, 2019
    Posts:
    2
    Hi guys!

    Hi guys! Right now I'm trying to code a card editor for a personal card game, here's my question:

    As you can see in the picture my cards are Scriptable Objects with all the card data in it. The main goal is having a modular tool that allows design to add any effect coded to any card and set the effect values. Basically i need to access to any value of the effect and I want to be shown in this inspector tab (I probably will code a custom inspector for this). Right now the effect is just an enum value, and here's the main question: What structure you recommend to use for this Effect thing? Each effect right now has an int value, a target, a trigger condition, and his particular effect (e.g.:- Block, Damage-Heal-)

    Thx a lot for the help!
     
    Last edited: Nov 16, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Use extreme caution with enums in Unity. Unity does NOT store the actual enum such as "Block".

    Instead, Unity stores whatever integer number is associated with "Block"

    This means if you have an enum with:

    Attack,
    Block

    and you later add one at the start:

    Prepare,
    Attack,
    Block

    Then Block will now be integer 2, and your previously-serialized Block will now magically (and silently) become an Attack.

    In my opinion this is a MASSIVE failure in the serialization design that Unity chose but we're stuck with it. The only solution is to NOT use enums and instead use something like strings, or else make 100% sure you NEVER remove or insert an enum, and only ADD new ones at the end.

    You have been warned.

    That said, play with whatever structures work best with your design idea, keeping the above restrictions in mind.
     
    Raes93 likes this.
  3. diego-giacomelli

    diego-giacomelli

    Joined:
    Oct 27, 2012
    Posts:
    26
    I agree. When I work with Unity, I also avoid using enum in most places, because most of the time using a ScriptableObject is better and always more flexible.

    One thing I usually do when using enum is to make its integer value explicit and with intervals, in case I want to add new values in the middle:

    Code (CSharp):
    1. public enum EffectKind
    2. {
    3.     None = 0,
    4.     Block = 10,
    5.     Damage = 20,
    6.     Heal = 30  
    7. }
    It is not perfect, but at least it will be clear to any other developer changing the code that he needs to take the integer values into account (and probably not change the existing ones)
     
    Raes93 and Kurt-Dekker like this.