Hello,
I am working on an RTS game: I have a gamelord class (attached to its own object) that is supposed to handle the status of the game, then i have a playerController class that handles player actions and is attached on the player prefab, which gets fed to the network manager and spawned correctly when a new client connects to the game. The idea would be to centralize all gameplay actions into the gamelord and separate all the local stuff that needs to happen to other classes.
The gamelord is “unique” in the sense that it’s meant to hold the same information on each client, most of its variables are syncvars, and all of its functions are supposed to be things that should happen on the server and then echo on every client, so stuff like creating a unit or building , keeping track of winning/losing conditions, etc.
playerControllers on the other hand handle the local player’s input and then translate that input into game actions by calling the appropriate functions on the gamelord. So if the player builds something, the playerController class would do something like “gamelord.Build(something)” and then the gamelord would actually instantiate and spawn that something.
So, in code, here is what i am trying to do
on the playerController :
[Command]public void CmdCreateBuildingHere(Vector3 pos,GameObject what)
{
//some other non-related code
gameLord.SpawnBuilding(playerId, pos, what);
}
then, on the gamelord
[Server]
public void SpawnBuilding(string owner,Vector3 where,GameObject what)
{
GameObject newBuilding = (GameObject) GameObject.Instantiate(what,where,Quaternion.identity);
RTSGamePiece newbuildingCtrl = newBuilding.GetComponent<RTSGamePiece>();
newbuildingCtrl.owner = owner;
NetworkServer.Spawn(newBuilding);
}
This approach does not work, as it behaves correctly only on the host, while if the client builds something, it won’t show on the server.
On the other hand, moving the SpawnBuilding() lines of code directly into the first function and forgetting about the gamelord does make it work, but architecturally it’s not what i meant to do, and it seems less logical to give more weight to the player object in an RTS where the important objects are not the player objects (like in FPSes).
All the objects that i want to spawn are already registered in the netmanager (i did it manually via the editor, the prefab for the netmanager already contains all the prefabs it needs in the “spawn info” section), so that should not be the cause.
My question is:
Considering the constraints of Unet (some of which i am aware, such as commands only working on playerObjs or prefab registering, and many of which i’m not) ,is there a way to do this in the way i wanted it to?
Specifically:in the client, have the playerController class calling a function in the gamelord, and then on the server have the gamelord actually instantiating the object and spawning it across the network.
Should i rethink my approach? Am i misusing Commands and the [Server] attribute? Is there something about NetworkServer.Spawn that i am missing?
Thank you for your time,
Dario.