Capstone – Week 7 Retrospective

At last, some breakthroughs in the networking. Here’s the main problems I was having:

In the original prototype, I had three “trap preview” objects that the defender had references to. To “place a trap”, I would just instantiate a trap at the location/rotation of the current trap preview object. However, networking makes this much more complicated. In the original prototype, I could just instantiate the three previews and have immediate references to those three GameObjects. However, with networking, you have to first spawn them through the server using NetworkServer.Spawn (since the previews should be visible on all clients). Unfortunately, spawning something through the server and then trying to get a reference to it on only one specific client is quite difficult. Here is essentially what is happening: the client version of the defender initializes and calls a Command function. This Command function is run on the server instance of that defender, and it spawns the trap previews across the network. But, there’s no easy way to really connect the code to get references to these newly created GameObjects easily.

So, what I ended up having to do is spawn them with a Command using NetworkServer.SpawnWithClientAuthority (this gives the client that called the Command authority in regards to that GameObject), then cycling through all the traps in the scene until it fills the three slots with the corresponding trapsĀ that it has authority to. A bit of a roundabout solution, but it works.

Spawning the trap previews
Assigning the trap previews

As a side note, I only just recently realized upon reviewing the Unity Networking documentation that it appears as though ClientRpc functions don’t have to be called on a script attached to a player object (unlike Command functions, which have this requirement). I previously thought that both Commands and ClientRpc functions had to be called from a player object, but I guess not. Because of this, I also previously believed that you had to route a lot of your networking code through the player object, but according to this discovery, this may not be the case. This might solve some of my problems.

-“David Hartman”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s