Improbable Icon

SpatialOS Discourse Forums

Conditional Spawning

I was going through the docs for the unity-gdk, and I found that it said to not conditionally spawn new gameobjects as the worker may not have write access when it comes time to spawn. To work around this I propose an example strategy. Please inform me if you think there are issues with this.

There is a hive that spawns bees. Bees move directly forward until they run into a physical object, and then die. When they die, they tell the hive to spawn another bee. If the bee hit a player, then it tells the hive to send an attack bee. If an attack bee hit the player, then it does damage to the player.

Entities: Spawner, Worker Bee, Attack Bee, Player (handled elsewhere)
Components: Spawning Info Component, Spawning Component, Movement Component, Spawn Info Component, Worker Bee Component, Attack Bee Component

Workers: Generic-Server-Worker (GSW), Spawner-Server-Worker (SSW), Client-Worker (CW) (handled elsewhere)

Worker Logic (CW):
This isn’t important for this example. It lets the player move around and do what they need to.

Worker Logic (GSW):
There are many GSWs that constantly swap access to Spawners and Bees based on load balancing. They move bees forward and check for collisions. On bee collisions, they tell the bee to die and set a flag on the appropriate spawner, based on the bee’s spawn info component, to start spawning and whether or not the bee hit a player. If the bee did hit a player and it has the attack component, it also does damage to the player.

Worker Logic (SSW):
There is one SSW that never swaps access to Spawners to other workers. This is important due to the constraints noted above. This worker checks the Spawner Info component of all spawners and if it is tagged to spawn, then it starts spawning a worker bee or an attack bee based on the information in the spawner info component.