🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Syncing stuff like what weapon & attachments the player uses

Started by
7 comments, last by Erik Nivala 6 years ago

So, as the title says i am trying to figure out a good way sync all that information with other players in Unity. My problem is that i can't come up with a good solution since i am used to creating classes for everything e.g. attachments are its own class and then the weapon would save a reference to that attachment. But since you can't send custom classes over [Command] & [ClientRPC] i am a little stuck. A solution for this would be giving each attachment for a slot a unique ID and then passing the ID to other player but i feel like that is very error prone if other ppl add a new attachment or the IDs get mixed up.

Is there a "standard" way that this is usually done that i am missing?

I am fairly new to programming so any help is appreciated!

Advertisement

Hey your ID solution would probably be fine. *something* has to be sent between players so it has the potential to be mixed up anyway.

What you should look into though is serialisation. To give you a quick summary this would basically take your Weapon object which has all its references to different attachments and essentially save it down into an encoded piece of memory or a file, so it could be a chunk of 1s and 0s. You then send that across to the other player, their client de-serialises it and hey presto they can re-create your original Weapon object!

13 hours ago, MidnightPacific said:

Hey your ID solution would probably be fine. *something* has to be sent between players so it has the potential to be mixed up anyway.

What you should look into though is serialisation. To give you a quick summary this would basically take your Weapon object which has all its references to different attachments and essentially save it down into an encoded piece of memory or a file, so it could be a chunk of 1s and 0s. You then send that across to the other player, their client de-serialises it and hey presto they can re-create your original Weapon object!

Thanks for that! So i looked into that a little but it seems like there is no way of serializing what kind of mesh the weapon has? My current setup is i have a weapon_base script that has data like damage fire rate etc on the root GameObject and as child is the imported mesh and that is saved as a Prefab. Which then is spawned by a weapon pool where the players then get the desired weapon from. Which also currently is not setup correctly since new connecting players don't have the weapons synced for already connected players.. but i probably should create a new post just for that. 

So i guess what i am trying to ask if there is a way to serialize complete prefabs because i will probably have the same problem once i start working on the attachments.

You certainly could serialise your mesh, but that would be inefficient. No need to send the mesh across the network if the other players already have it on their machine. You just want to know what other players have, you don't want what they have, if that makes sense.

You would be better off redesigning how you access your assets. Rather than your Weapon having it's mesh as a member of it's class, just have some kind of ID. Make another class e.g. AssetManager have a list of all your meshes with unique IDs and in your Weapon class you just make a call to the AssetManager to check the ID of the mesh you want to use.

So your Weapon class should just have simple data for damage etc, and IDs for it's base mesh and any attachments. You serialise that object, send it across the network and another client can de-serialise, see it has mesh ID 5 and it checks its AssetManager to see what mesh that is.

I understand what you are saying but at the same time i am super confused how to actually implement this. I currently when a player connects he equips a Loadout and sends all the needed data to other players - and that works fine for players already in the game to have new players updated. But when a player connects none of the already connected players get properly updated. I don't know if i am missing a callback that unity has or if my setup is completely wrong. If you need code to that let me know but maybe that is a common problem that has an obv solution.

Thanks for the help so far @MidnightPacific.

When the server sees a new player connect, it should collect all the state of the already-connected players and send those to the new player.

enum Bool { True, False, FileNotFound };
2 hours ago, Erik Nivala said:

I understand what you are saying but at the same time i am super confused how to actually implement this.

I made the same mistake when I started.

A way to think of it is like playing chess with someone in another room. Each of you have your own chess board. There is a 3rd person who keeps running between the rooms to tell each player what move the other made.

So when ever the 3rd person tells you that the other player moved pawn E7 to E5 you move the pawn on your own board to mimic their board. The 3rd person can also act as a referee because this person sees both boards and can correct mistakes.

 

So if you equip a item on one player, the server tells the other player that the item was equipped and the local computer updates to reflect this change.

All you need is some way for the server to keep track of the game that is played.

That's a great explanation :D 

I managed to get it working by checking when a client is connected and ready, then i iterate a list of the connected players and update the weapons only on that client.

Thanks for all the help!

This topic is closed to new replies.

Advertisement