Search code examples
c#unity-game-enginescriptable-object

Using scriptable objects for characters information


hey everyone

I asked this question in unity answers but I didn't get any answers. I'm new to programming and I have a question about scriptable objects. I'm making a strategy management game and I have characters in the game with unique information like name, age, cloth, skills, and many more (+35 variables include int, strings, Lists, and GameObjects), I made a scriptable object for this purpose and read each character data from it and change them if needed.

firstly, I want to know is it logical to have 100+ characters with these scriptable objects attached to each one of them or just use a simple script for my character to handle variables.

secondly, make them from the start or instantiate them in the middle of the game which one is better?

thirdly, how about saving and using them in runtime?

Thanks

Edit:

After searching for answers finally, it is obvious that there is no need for scriptable objects for this type of variables and GameObjects. so I decided to update my question.

now my question is what is the best way to use this type of character variable? my characters heavily depend on stats and variables that need to change or read all of the time like stress of the character drop over time and needs to show always.


Solution

  • I use this simple, easy to edit and maintain method, which purists will probably poo-har, but it will get you running quick and easy. Put it all in your class constructor method, with a switch statement.

    You set shared attributes outside, and specific attributes inside the switch statement.

    Then you can create an NPC as easy as MyNpc = new NPC(16) Where 16 is Warrior type or whatever. Its easy to add new attributes at the top without editing 100 scriptable objects, you can see everything in one spot.

    I've got five as an example, but you could have a 100 here without taking up much space. You can add one without doing any modifications. You can create them at runtime.

    public class NPC
    {
        public int NPC_ID;                    // the unique ID of the NPC
        public string Name;
        public bool active;
        public string SpriteName;
        public int PassiveAbility;            // The passive ability the NPC grants 0= none
        public int Dice_ID;                   // The Dice ID the NPC adds to the party 0=none
        public int HP;                        // Max hit points (fully healed)
        public int HPR;                       // Hit points remaining
        public int FP;                        // Max food points
        public int FPR;                       // food points remaining
        public int MaxSlots;                  // Number of item slots  
        public bool Fem;                      // Use female audio  
    
    public NPC(int ID)   // Pass the ID to create the NPC of that ID
    {
        NPC_ID = ID;
        HP = 2;
        PassiveAbility = 0;
        MaxSlots = 3;
        FP = 5;
        Fem = false;
        switch (ID)
        {
            case 1:
                Name = "Bisky"; SpriteName = "N25"; PassiveAbility = 12; MaxSlots = 0; FP = 20; break;
            case 2:
                Name = "Zahid"; SpriteName = "N28"; PassiveAbility = 2; MaxSlots = 4; break;
            case 3:
                Name = "Anna"; SpriteName = "N17"; HP = 1; PassiveAbility = 3; FP = 8; Fem = true; break;
            case 4:
                Name = "Carl"; SpriteName = "N30"; PassiveAbility = 4; MaxSlots = 4; break;
            case 5:
                Name = "Max"; SpriteName = "N06"; PassiveAbility = 5; break;
    
            default:
                Debug.Log("ERROR: Got passed a bad NPC number: {NPC constructor}");
                break;
        }
    
        HPR = HP;   // Set current hit points to Max Hit points
        FPR = FP;   // Set current food points to Max Food points
    }
    
    
    }   // End Class