- Home /
many "characters" data
I'm making a game that will have many different characters that all have the same variables: move, range, attack, defense, maxHealth. Once created, none of these variables will change. For instance one will have
byte move = 3;
byte range = 2;
byte attack = 2;
byte defense = 5;
byte maxHealth = 5;
My question is what's the best way to go about storing this data? Should I make a class for each character and pull it from there? Or just put the data into the variables as they are instantiated through direct code (which is how I'm doing it atm), or some other way?
http://gameprogram$$anonymous$$gpatterns.com/component.html
http://gameprogram$$anonymous$$gpatterns.com/type-object.html
Answer by dscroggi · Mar 28, 2014 at 12:55 AM
Great question
Definitely use classes, if the characters are different types use an abstract base class. The absolute best way is to use a database of course, next best would be a less permanent form of storage like xml, and worst would be hard coding constants. However depending on what you're doing any of those could be good.
If youre 100% sure that they will never ever change just put the values in the constructors.
For example abstract base class Character has the members listed above and accepts them in the ctor. Derived class Knight passes in his values etc. So to make it last longer and be more flexible you can put together a unity editor window with some inputs that export to xml with for each character type, then in your ctor you can read the xml/database.
Using a base class like that allows for methods that accept Character parameters so you can do your calculations in one place instead of having separate/duplicated ones for each character.
To take it one step further use a list of stats instead of a finite set of members. If you make your list recursive that opens a lot of possibilities as well cause now you have a character with a sword with an enchantment which each add to attack so in your stats class just either inherit from a stat list like class Stat : List or contain it and provide a method that calculates the value. This way when you call your Character.Fight() or whatever it can sum up all his equips/buffs and things attached to those.
Probably make some interface like ITarget which has that calculation method so in unity you can just to a raycast on some enemies and get a list of ITarget and pass each one some combat value. Proceed to epic developer status.
Could you show me a sample of code showing how to do the xml thing? I've never used it before. Also, how will a unity editor window help me? (I've never done that before, either).
Thanks
Okay, I looked up how to use xml. It seems to only work with primitive types. How would I save a texture or other non primitive data? And it only works with public attributes. How can I use it with private attributes? (that's more important as public is too easily abused)
Answer by getyour411 · May 17, 2014 at 11:40 PM
XML or a database for this use case is overkill. Use the code as you are if that's working for you, and you say it is.
It might be overkill, but I'm trying to be efficient as possible, and I'm still learning how to do that.
Answer by Andres-Fernandez · May 22, 2014 at 07:23 AM
If all your characters have the same variables you are just defining a class. Create a Character class with those fields and set them in the constructor:
public Class Character {
byte move = 3;
byte range = 2;
byte attack = 2;
byte defense = 5;
byte maxHealth = 5;
public Character(byte setMove, byte setRange, byte setAttack, byte setDefense, byte setMaxHealth) {
move = setMove;
range = setRange;
attack = setAttack;
defense = setDefense;
maxHealth = setMaxHealth;
}
}
Then you can create different characters like this:
Character myCharacterA = new Character(3, 2, 2, 5, 5);
Character myCharacterB = new Character(3, 4, 1, 2, 4);
Yeah, that's exactly how I was doing it before. I was just trying to make it last for more than one instance of the game, and able to transfer them between scenes. It also helps that other members of my $$anonymous$$m can more easily tweek the numbers without going into the code.
Your answer
Follow this Question
Related Questions
Reading and Storing External Data into Memory (From Text) 1 Answer
Database-like arrays 1 Answer
Trying to learn Generics 1 Answer
How to save and load any data type? 1 Answer
Lists and Structs instead of Arrays? 4 Answers