Wednesday, October 20, 2010

Droids - Spawn Points with Gizmos

Enlisting the Unity editor to help create content is a powerful feature. In my case I wanted to create some spawn points for some work stations as part of my droid factory. The work stations are spawned in random locations. The possible locations are pre-determined (my spawn points). One thing I didn't want to do is fidget with these points to get the location just right. I wanted something that lets me spend as much time as possible in the editor.

My solution was to draw an approximation of the model using the editor. To top it off I pulled this information from the prefab itself. This code also includes finding a prefab and using its data without there being an instance of it. I also had to filter out game objects that weren't prefabs (otherwise the editor will draw those).

Here's the code:

This is how it looks:
Yellow spawn points indicate where objects can appear


Monday, October 11, 2010

Droids - Can't send RPC function because the target is not connected to the server.

This is a fun little bug that resulted from me trying to call an RPC method when it was targeted at the owner itself! To work around this I checked to see if the target player had the same GUID as the Network.player. If it was the same I'd call the normal local method rather than the RPC. I found out about this here.

By the way, fixed player spawns are up the Droids github repo now.

Thursday, October 7, 2010

Droid Colors

3 colors for 3 players
Adding colors to Droids proved to be a lot more difficult than I had imagined. Sending all of this data as a one-shot RPC call on player connect was just creating a maze in my head, and things weren't working quite as I had expected.

My ultimate solution was a combination of wasteful serialization and some RPC calls. The server is the only app that handles color management. It also now tells clients when to spawn their droids. When the droid is spawned, it immediately sends out an RPC telling that client to change its droid's color to whatever color it assigned for that player (keyed by player GUID).


Clearly, green is hacking here.
That alone was not enough, however. New players joining wouldn't get old player data. Using the OnPlayerConnected event to fire off colors to joining players didn't seem to cut it. In the end I just added a new NetworkView, tied it to the DroidColor, and added an OnSerializeNetworkView to send the color segments over the wire. In the future I'd like to cut back on state sync, but at this point I'd rather move onto other features and this just might be over-optimization.

As normal, the new version of the Droids game (web and stand-alone builds) are up in the games section.