Logic behind making the master client control an AI?

Hi there,

I'm wanting to know how I'd go about making the master client control an AI within my game, especially one that is using state machines for the specific states of the AI.

How would I go about basically switching states of the AI, and then having the regular users know that the AI has changed states. Is it just using RPC's? I'm wanting to limit hacking as much as possible, so basically making sure a regular user does NOT control the AI nav agent.

My way, in my head, at least, would be this:

if (!view.IsMine) agent.enabled = false;

if (view.IsMine) // change states here

However, I know that I'd have to do a RPC check when a state has been changed so the other players know, but how would I go about doing that, especially if the non-hosts have the nav agent as enabled = false? Would the AI still be shown as moving if the agent is disabled from the non-hosts?


  • I think you have a few options, depending on how complex your AI is and the scale of your game, you could observe the changes, or send them as probably an event over an RPC - an RPC would work, though I think an RaiseEvent would just be a bit more clean since you wouldnt need to target the AI's specific photon view to send the event for everyone, either method should work perfectly fine though

    If your AI changes states often, maybe OnPhotonSerializeView (part of IPunObservable) would be a good option: https://doc.photonengine.com/en-us/pun/current/gameplay/lagcompensation

    If your AI changes are specific (like the first time it sees an enemy or is given a "follow me" command or something), then maybe just a RaiseEvent would be a good option: https://doc.photonengine.com/en-us/pun/current/gameplay/rpcsandraiseevent#ioneventcallback_callback

    You will likely want to include view.IsMine with either solution to ensure only the owner ever has influence over when those events get sent or written, but you may not need to disable your AI navmesh for non-owners, if your using view.IsMine with the agent still enabled, then it should sit around and do nothing until you decide to override its state from receiving the event, or have the IObsevable script lerp to the new values (if you wanted your movement to be smooth, otherwise you can just set the values directly and instantly)

  • That's a good take on the RaiseEvent, I'm basically trying out the RPC method at the moment and I think it has started to work. I do get some weird issue on the non-client stating that there's no active nav agent, that is most likely due to me disabling it on the non-master client. I'll probably use your take on the view.IsMine for this.

    I have another small question, how would you go about those hackers that try to force host on the servers? I've noticed this happening in some games, and it seems to be a strange one to figure out? Would there be a way to only have the lowest ping of all players in the lobby to be the host, that way even if someone was to force host, it wouldn't really work? Just a thought I had.

  • Networking and security is like Ying and Yang or night and day, they are so important to eachother but they are also so fundamentally different from one-another, and unfortunately I am not very experienced with handling security/hacking, its probably one of the most complicated parts of multiplayer games, somehow the server has to know what is genuine actions from the client and what is as far as the code is aware, a manipulation of data, or data that is desynced from the server simulation and predictions (for example, in a simple form, if a player is moving at 12 units per second, the server could predict the next frame they should be within 10 units, so up to 22 units or as low as 2 units, if they jump to 500 units or even 30 units then their client no longer matches the prediction and can just not send the change to the server or "flag" the player to monitor other desynced predictions, this would have to factor in any abilities in the game that can genuinely increase or decrease that value and also consider any packet loss that might spoof the prediction)

    You CAN chose a host based on lowest ping if you have every player send for example a custom player property or event of PhotonNetwork.GetPing() every interval, compare the lowest then PhotonNetwork.CurrentRoom.SetMasterClient(...), though youd probably want to limit how often you do that so the master client isnt constantly changing every few seconds, though if possible, it may be better to first find out HOW these players are able to force the master client to change to begin with, otherwise any solution would just be fighting with their forced changes and nullify your efforts, hacking is a big and complicated topic with a lot of edge cases to consider, and I am really not sure the best way you could solve this - if your game is released, you should at the very least have authentication setup and an app version you can change with each major update/patch, this way its one extra step for hackers to go through, and lets you track those players by their "auth ticket" (for example: Steam ID)

  • PUN is not really designed to have a proper Host for a game. If that's needed, you might want to use Fusion instead. In Host Mode, the clients only send their input and the Host simulates the world. Each client can predict what's going to happen (to avoid lag becoming visible) but the server's state is always re-applied and is the one authority that defines the results.

    PUN is rather open and you need to add a lot of checks to just detect exploits.

  • Really appreciate the insight on this.

    I'm definitely going down the authentication route, especially with it being released on Steam whenever I am finished with it. I think for now, it's best to just figure out the cheats as it is released, let the cheaters find the exploits and then patch them as they come.

    I will most likely just end up doing a report system, luckily it is more-so a party game rather than PVP, so the thought of cheating shouldn't be there in the first place, but it will happen anyway, especially since the virtual currency earning if you will is done with player prefs in mind, which is just easy to manipulate. Not much I can truthfully do with that with PUN, I believe.