One fundamental limitation of UltEvents is that the target cannot be changed dynamically.
Sure we can run GetComponent
- but we canât actually use the output as a targetâŠ
To work around this limitation, the UltSwapping framework was devised. âUltSwapâ is a term used to describe logic thatâs able edit the event and swap out the target.
The âSwappedâ Ult Event Holder originally contains logic that runs on the âDummyâ Rigidbody. When the swap is ran, the Swapped Ult Event Holder is programmatically âswappedâ to use an entirely different return value.
The UltSwap Prefabs
Here is an overview of the main Prefab:
UltSwap
- When ran, it will edit the âSwappedâ GameObject, and replace its âDummyâ target with another one.Swapped
- Contains the logic you want to swap the target out on.Dummy
- Contains any dummy components you want to be swapped for the real target.SwapSource
- Holds the target you want to swap the dummy out for.ReferenceHolder
- Used internally by the UltSwap system, and allows for multiple swaps.
The UltSwapFromParent
has the same hierarchy. The difference is that it contains extra logic for performing the swap on whatever you parent it to, rather than you manually providing the swap source.
Samples - Zero Gravity Crate Spawner
UltSwapping allows us to directly use the GameObject
that weâre provided inside of an OnSpawnEvent
:
CrateSpawner (Gym Cube 1x1)
Swapped (GetComponent)
Swapped (Disable Gravity)
- We set the swap source to the
GameObject
argument - this is the GameObject of whatever is spawned. - We
Invoke
the UltSwap to change theDummy
GameObject used by our logic on the Swapped GameObject out for what the Crate Spawner provided. - We finally
Invoke
the Swapped GameObject to run our un-dummified logic.
The first swap gets the Rigidbody from our spawnable, and then feeds it into the 2nd swap which simply disables its gravity.
Using Ult Utils to create an UltSwap
The Ult Utils package allows you to generate an Ult Swap for a component in a few clicks via the Inspector: