Sync bombs and state with MLAPI

Orange Bomb Target

In my previous article I wrote about synchronising the bullets. Xevious has a secondary fire, a bomb that is dropped onto a bomb target. I.e. when you drop a bomb there are two objects created, a target and a bomb. Both of these must be synced between the players. Previously I have used two different mechanisms; a) Only create the objects on the Server and let MLAPI sync the Network Transform and Animations to the clients b) Sync the trigger of the “Drop Bomb” event and let the clients control their own local versions. The game rules for Xevious is that you can only fire one bomb at a time. Therefore, since there will only ever be four bomb objects on the screen, I thought I would go with the simpler solution and allow MLAPI to control the sync. However, game logic failed.

Sync State with MLAPI

Game logic for dropping a bomb

When the player requests that a bomb is dropped the game logic first checks with the Solvalou Target to see if a bomb is already dropping. The problem with my MLAPI code is that although the game objects are nicely synced the IsBombDropping is neither a transform nor an animation. I.e. MLAPI does not automatically sync this detail. Therefore, when the player drops their first bomb it is correctly shown to both players and correct vanishes but they can never drop another bomb since the IsBombDropping state on the clients is always left at TRUE. Fortunately syncing a state property in MLAPI is relatively easy.

Network Variable

Network variable

Replacing the bool _isBombDropping with a NetworkVariable<bool> _isBombDropping is pretty easy. The only thing you have to watch out for is the ‘permissions’ about who can read/write values to the variable. In my case I want the Server be to be in control and the clients to be able to read it. However, when you set the permissions you must enforce them in your own code too otherwise you will get permission denied exceptions.

Remember to enforce the permissions

Once that was synchronised the two players can now, to use the Xevious terms, “zap and bomb”.

I think the sync is just a little too slow (both bomb starting position and explosions), so I may use the more complicated sync technique I used for the bullets, but for now I will push on. Although I think most of the main challenges have been met. Now it is the time to decide if I should leave this as a technical demo or go to the next steps and have a complete game?

Unity, VR, Enterprise and .Net Developer