Paul Marsh

May 27, 2021

3 min read

Sync Child/Parent relationships with MLAPI

In my previous article I talked about the basic mechanisms behind synchronising the flying enemies in multiplayer Xevious. I know that the ground enemy will require additional work. The problem is that the ground enemy need to be positioned relatively to the scrolling background. For fixed positions you can get away with ignoring this issue because the Server instance will have created the enemy and correctly set up the parent child relationship.

Ground Enemy Parent/Child structure

Then by adding the basic Network Transform object the world space position will, fortunately, be correct. Although still suffering because I have not changed the Min Meters setting.

Ground Enemy sync’d in world space

However, for enemies that follow roads and paths I need to implement the correct relationships.

Sync Parent/Child Relationship

Since the Network Transform does not sync the parent/child relationship I chose to implement this with a Client RPC call. But the game objects do not share their unique instance identifiers across networked machines. So how can the server say to the client, “You must parent X to Y”? Whilst Unity may not have a guaranteed identifier, MLAPI does share the network object Id. When Id 99 is created for object X on the server, then all the clients will have an object X with Id 99. Since the enemy object we are spawning already has a Network Object, the missing part of the puzzle is to ensure that the Parent also has an Network Object.

Parent And Child with Network Objects

Therefore when the server creates a new ground enemy it can tell the clients, “You must parent Network Id X to Network Id Y”.

Re-parenting RPC

That seemed to work. However, our good friend Pooling had a problem when re-hydrating a pooled instance

Problem with re-using from Pool

The problem is that the re-parenting was keeping the world position of child. The keen eyed among you will have noticed a commented-out line in the code. That line is the fix, in my case the child should always be at (0,0,0) local space.

Pooled objects working

I have also added a Zolbak ground enemy. This is to show that event though I have not added a Network Animation object the clients are still able to animate the enemy (it flashes). Whilst this is okay for the mundane Zolbak, the little turret enemy of the Logram is more complicated.

Logram Firing

The Logram plays an animation of its turret opening and a bullet emerging from it. The bullet only emerges at a certain point within the animation track. This is achieved with using an Animation Signal. However, animation signals are not synchronised with MLAPI. Not to worry, we “just” need to add another RPC call.

Logram client firing

In this case the Server notifies the the client that it should fire. It works…(sort of)

Logram animated firing

The animation and the fire work. However, there are two obvious problems.

  1. The bullets are not yet shared.
  2. Currently each Logram only knows about the local player. Therefore the Lograms for Player 1 fire at Player 1, the Lograms for Player 2 fire at Player 2.

Next up

  • Making the enemy aware of the players
  • Share/Sync the bullets