So i am setting up multiplayer networking for an RTS style game. I have a unitBase class which is a child of the character class. I have defined the owners of the unit on begin play, and i am trying to ensure that a client can control characters that it owns but i am running into unexpected behavior that i do not understand.
If i call an event ‘clientCallsServer’ from the client in unit base, that then attempts to call ‘ServerEvent’ which is set to be a server only call (which would then issue instructions to an ai to direct the character)
I believe i have set ownership correctly, as i am checking that the owner and player controller match, and they do, and i am checking the role of the controller, which returns as autonomous proxy. These were the conditions i believed would allow Server RPC’s to run, however the server event is never called.
I have managed to issue commands to the AI by routing commands through the player controller class, and the servers instance of player controller then sends commands to the units, which works, but it feels really hacky and my code is spaghettified, so i am not a fan.
What am i missing here?
Cheers for the help in advance, here is the blueprint that runs in the unitBase when i send the unit a command on the client.
That makes sense in theory, However it doesn’t explain why my test is not firing the server event.
It also complicates defining who can control a unit, it makes sense to set the unit owner to the player who spawns it, and then the authority to control it is just based on that. Feels like what ownership should be for, and can imply a players ownership of an AI too.
Also i am currently using an AI to give move to commands, and they seem to still work despite ownership not changing. I am printing the owner on select and it will return a player controller, not an AI, and that functionality works as expected
Edit: also i am currently using Ownership to define whether the character can be selected, and seems to work fine.
You are correct, i disabled AI possession and the server event did fire.
A bit irritating that it wouldn’t alert me to this ownership issue, and return the AI controller as the owner, or break the ai when i took ownership. Seems like a hidden failure in the setting of the ownership.
So yes, it seems like the method is to relay commands to the player controller and then call from the server, which is a bit of a maze, as it just looks like a redundant extra step in the code.
Currently the (working) method to harvest a resource node is :
Right click event on node → PlayerController Client harvest command → PlayerController ServerHarvestCommand-> set blackboard variables on serverside Unit instances and ai takes over
The method i wanted was :
Right click event on node → Unit ServerHarvest → Set blackboard Ai takes over.
It still seems odd that i need two commands on the player controller and cant just immediately call serverHarvest command. Could i be doing something wrong here?
well you always need a server RPC so that doubles the commands.
the way i do it though is have one Event, SendCommand with an input of GameplayTag, RPC that to server, and then in the unit SwitchOnTag to handle events.