Often we’ll want to ‘hook’ or ‘latch’ onto existing spawnables and other GameObjects to perform our own logic on them. We may want to despawn props, alter their mass, or just generally change their behaviour.
In our case, the end-goal of this page is to make a zone that despawns NPCs! Set-up a new zone:
Explanation
- I put the GameObject on the
BeingTrigger
layer. This ensures our trigger will pick up beings (things that are alive). - I added a Box Collider and set it to
IsTrigger
. The zone requires this. - I added the
Being
tag to the ‘activator tags’ list - our zone enter event will fire when entered by a living being (so a Nullbody, Ford, etc.), but specifically not the player which uses a different tag.- you probably want to avoid despawning the player rig by the way… this may require a game restart to revert for most players…
Install ObjectPathExtensions
!
Before we continue - The ExtendedSDK provides dummy versions of the scripts SLZ uses to develop BONELAB internally. In short, these don’t contain any actual code. Trying to use them in the Unity Editor will do nothing.
Let’s replace a script now so that we can lazily test in-editor without having to boot up the game! Download this recreation script, and replace the dummy one. How I did that:
You’ll need to do this per project by the way - sorry…
Getting the Path to Things
The SLZ.Marrow.Utilities.ObjectPath
provides the path to objects:
We have 3 choices of object types - thanks to these handy overloads!
Below it’s used to log the path of any GameObject that enters a trigger:
Pay attention to the logging along the bottom!
We can also use string.Format
to alter the path we’re given - used here to add a /
to the start, and do something useless like log the path twice:
Again - pay attention to the logging along the bottom!
Hooking Transforms
With that knowledge, let’s use string.Format
to get the exact path to objects in the scene. Remember that adding a /
to the front is needed to insure we search from the top of the hierarchy - as covered in 1.1 Parent Under Anything.
Here our BEAM
GameObject underneath any collider that enters our trigger:
The ‘hook’ (
BEAM
) is parented under anything that enters the trigger. SetLocalPositionAndRotation
is used to center our position/rotation in the middle of the collider.
Finishing our NPC Despawner
How do we despawn things?
Well, every spawnable in BONELAB has the Poolee
component on it. If it doesn’t have one, it’s automatically added when spawned in-game. That includes NPCs, such as this Early Exit Zombie:
Using the Poolee.Despawn
method is the proper way to despawn things in BONELAB.
Avoid Destroying Spawnables!
When something is ‘despawned’ in BONELAB - its actually just disabled and then re-enabled when it’s needed again. This is known as pooling, and is done for performance reasons. Using something like
Object.Destroy
instead may cause problems - so usePoolee.Despawn
instead!
How will we call Poolee.Despawn
?
Component.SendMessageUpwards
loops through every GameObject in the hierarchy above ours, looks for a method that matches the given name (Despawn
in our case), and will run it.
Enabling the ‘Don’t Require Receiver’ option prevents script errors if the method isn’t found at all.
Let’s make use of SendMessageUpwards
to make a hook that despawns anything it’s parented to:
Now that we have a despawner - we just need to parent it underneath things and Invoke its event!
The final logic for our zone enter event:
Zones do not work in-editor, and Poolee is a dummy script. You’ll have to test in-game.