2

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.

shingo
  • 18,436
  • 5
  • 23
  • 42
Mehrad
  • 23
  • 6
  • 1
    Unlike `MonoBehaviour`, `ScriptableObject` is intended for project-wide information that doesn't have any real "code." and persists between scene changes. It sounds like you'd be better of using a prefab for each character type. – 3Dave Dec 09 '22 at 16:42
  • @3Dave - thank you for your answer, actually i use ScriptableObjects for their persistent behavior. – Mehrad Dec 09 '22 at 18:15
  • 1
    If you really just want an object to persist between scenes, you can use `DontDestroyOnLoad`. https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html – 3Dave Dec 10 '22 at 22:44

1 Answers1

1

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
Ratbyte Boss
  • 461
  • 4
  • 13
  • that's really easy and fast way to create and manipulate characters but I think the downside of this is that I can't change the values of one specific character, for example, I can't change anna's HP in runtime. am I right or I'm missing something? BTW thank you for your answer it's helpful. – Mehrad Dec 10 '22 at 18:51
  • 1
    You can easily. After you create Anna with Anna= new NPC(16); at any time you can always change her stats by Anna.HP=8; – Ratbyte Boss Dec 11 '22 at 22:32
  • 1
    Once created you can modify an stat and it will only affect that unique variable, once you have created the class. You can have a party of making a list of these, like this: public List TheParty = new(); // This list holds the current Party! Then add chars to the party: TheParty.Add( new(NPC(5) ) ); – Ratbyte Boss Dec 11 '22 at 22:41
  • that's great. for now, I'm using a script with character variables assigned to each character and using them throughout the game and changing them when needed. the method you said is very efficient and uses low space. does my method uses too much space that later make problems for my game? – Mehrad Dec 12 '22 at 15:36
  • my game heavily depends on each character variable and stat which need to be read and written at any time. do I need to change the way I'm storing data or stick to my way? – Mehrad Dec 12 '22 at 15:41