Unity How to store multiple character data?

Asaarii

New Member
May 1, 2020
1
0
Hi!

I'm pretty new to game programming (and programming in general, I started a little more than a year ago), just follow a few tutorials to get the basics of Unity.
I want to start a little project and make a game inspired by Strive for Power and Strive: Conquest.
I already made a few "scene" for each different menu and a few basic classes.
But I face a problem and I'm not sure which approach to take...

For those who don't know the games, its basically a sandbox game where you can recruit characters to help you in your quests, in combat... Each have their own statistics, skills, traits, equipment...
All of them are randomly generated and you can have as much companions as you want.
And I want to do something similar, i.e. generate random characters with their own attributes and store them somewhere to have access to their data and display them.
The random generation is not the problem, it's just the data storing part.

Do you know if there's a tutorial that cover that sort of problematics? And if not, how should I do it?
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
1,005
You want to look into making a serializable class.
Serializable just means, the data will be saved as a file (a binary or something)
so you can create a bunch of random characters, store them all in a serializable array, write to a stream (such as the save file can be one big serializable object with data insidef of it to be saved, and everything will be saved using one command.
to load, you just deserialize, which just converts a file back into an object or variable, and if that object is one save file, it will remember variables, such as a list of characters with each of their unique properties.
 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,494
7,055
The exact technique you would use depends greatly on the game engine you're planning to use.

As Saki_Sliz says, you could create a class that has all the various character attributes, create instances of the class, and then serialize those instances into your save. Ren'py does much of this automatically for you with Python classes. It isn't hard to do the same in C# with Unity. Alternately, you can implement some kind of "export/import" in which the character class dumps and reloads its internal data into a form that can be easily serialized.

One of the downfalls of serialized classes is that they can be hard to update in a subsequent game version - if I serialize out an instance of a class in version 1, change the class definition, game version 2 may not be able to read the save data from version 1. That's one of the advantages of using an export/import mechanism - you're in more control of the format of the data, and can deal with things like that.

All that being said, if you're new to programming, this is a fairly advanced technique - it might be a bit early to be biting off something that sophisticated. Just saying...
 

Marduke

Member
Jun 12, 2019
124
300
ScriptableObjects can help with this. Check in your favorite unity learning youtube channel