Hello. It’s probably o noob question, but important to me.
I’m using C#. I’ve just succesfuly made my code so that when player joins or hosts, he adds his “players stats” to a static List as an instance of the “PlayerStats” class.
It should work that way for every player - whenever someone joins, his instance of his statistics is added to the list.
Now, this list should be one, global, so I thought - it should be synchronized by server. Maybe as some kind of SyncVar, probably SyncList of some type. And here’s the problem - is there any way to synchronize a list of class instances?
I’ve checked Unity Documentation and seems that’s a bummer. Or maybe I understand it wrong. SyncList seems to have only basic types like integer, etc, there is also a struct, but… Do I really need to convert class with player stats into a struct for a server-wide synchronization?
Once again what I want to achieve: I need to have one, global list of players into which new joining players would be added and idententified. I’ve already done identifying - while adding new instance, I’m giving that player an ID = PlayerList.Count - 1. Probably best if elements of PlayerList could be PlayerStats class instances. So each player (at his own index) loads his data from disk into his instance in PlayerList. I thought it would be convenient that way (not sure how to do it otherwise, though). It works in singe player mode, but in Multiplayer I don’t know how to design it properly. Because I need that static list to be server-wide, aware of other players.
And what if I had to synchronize equipments, which are made of classes in my project.
Can you help me somehow with designing that? I’m trying to create a simple multiplayer rpg based on our new Unity Multiplayer tutorial.
Now there’s a need to identify players and load their data in proper place, data which is already generated by character generator and saved, and which properly loads in single player. I thought I should use a local singleton instance of a class PlayerStats for player data, and to keep those instances in global server-wide list.
My PlayerStats are a component of PlayerStatsObject (empty gameobject in hierarchy that has DontDestroyOnLoad). PlayerStats class, made into singleton instance, contains whole generated character, with his vitals, class, race, equipment lists made of classes, etc.
So basically, I need every player to be added to the list, have his own PlayerStats, and make those PlayerStats easily interactable/modifiable by other players (and AI enemies / Environment, but that’s easy, it’s single player). Adding PlayerStats as a component to Player prefab seems overkill to me, as those stats will be called by almost everything in game all the time. So that’s a ton of GameObject.Find(“Player”).GetComponent();. Seems that would be a disastrous design.