- Home /
Multiplayer RTS Script
Hello everyone,
I am currently building my own RTS and now am stuck with the management of different players.
I couldn't get any information on how to do this the best way, so I am kind of helpless. I thought I instantiate a GameObject for every connected player. All his Units and Buildings are a children of this Object to easily determine its owner. But to achiev this, I need to make sure then these objects are on every client the same state.
How do I do this or is there even a better way?
Edit: I know the basics like: http://www.paladinstudios.com/2013/07/10/how-to-create-an-online-multiplayer-game-with-unity/
Its more about how to create units/buildings and assign them to players, so that its on every client the same.
Answer by GraviterX · Dec 04, 2014 at 12:23 AM
Try attaching the units to the player in the hierarchy and then saving them as a prefab. For my games I've been working with Photon Cloud. It's a free plugin in the asset store. Here is a link to a tutorial I found : https://www.youtube.com/watch?v=mLAilMkRxYU. This tutorial also guides you through a script that instantiates the prefab for every new connected player. Hope this helps.
Thanks. But still, the main problem I have is to make a game-wide list of all players and their gameobject right at the start.
Answer by jpthek9 · Dec 05, 2014 at 01:38 AM
That might get very messy, Graviter. You should instead create a script that controls the player: his status (dead or alive) and possibly number of upgrades. Instantiate this for everyone and use an RPC to set its unique player number (I.e. Player 1's controller gets 1).
Put an extra RPC function on all the spawned units that gets called when they're instantiated. To make everything clean in neat, this function can also set the unit's stats and turns on all needed scripts, but the main thing is that you call the RPC with a certain integer representing its player. You use this integer to find the controller that has the ID of the same integer. Maybe even add the unit to the player controller's list for greater control and less bandwidth usage.
This solution has the added benefit of being able to call a single RPC on the player controller for upgrading a player's units.
I.e. in the RTS game I'm working on there are 8 players. If player 1 spawns a unit, that unit gets an RPC targeted for everyone, and on everyone's client it finds its player controller. If player 1's base gets destroyed, an RPC gets called on itself for everyone and on everyone's client, all units contained in its list are destroyed. If I upgrade my units, the upgrade goes through the player controller and calls an RPC that tells all other instances of itself on others' clients to let all units in the player controller's list know to be upgraded.
Okay this may get very messy as well but it's the same concept and lets you use list access for everything.