Custom event notifier
Overview
The Custom Event Notifier component is a core tool for enabling advanced, script-driven interactions in Yahaha. By attaching this component to any object, you allow it to listen for and respond to custom events, making it possible to create dynamic, interactive gameplay that goes beyond built-in no-code logic.
Use the Custom Event Notifier to:
- Enable objects to react to custom, scriptable events
- Create more flexible interactions than built-in triggers allow
- Coordinate complex behaviors between multiple objects
How Custom Event Notifier works
- Receiver: Any object with the Custom Event Notifier attached can receive custom events.
- Sender: Custom events are sent using the
yahaha.EventSystem:NotifyObjectEvent() function
in Lua or via the Event Trigger component'sNotifyCustomEventNow
action. - Response: When a notified event matches an object's configuration, it executes the assigned action (such as playing audio or running a script).
Typical usage workflow
Attach notifiers to receiver objects
- Add each object you want to respond to custom events (e.g., a light, an audio source) to the scene.
- Add the Custom Event Notifier to them by choosing Add Components > Custom Event Notifier to make them ready to receive custom events.
Set up the event sender
-
Create an empty object to act as the event sender and central controller.
-
Add the Custom Event Notifier to the object by choosing Add Components > Custom Event Notifier.
-
Add the Event Trigger component to the empty object by choosing Event Trigger.
-
Configure the Event Trigger component:
- Trigger Object: The object or interaction that initiates the event (e.g., a collectible)
- Trigger Event: The specific event to listen for (e.g., OnPropInteracted)
- Action Object: The object to notify (usually the empty object itself)
- Action Function: NotifyCustomEventNow
This setup ensures the empty object receives the trigger event and immediately sends a custom event notification to its registered listeners.
Script the event logic
- Add a Lua script to the event sender object if you want to control which objects receive the event or add custom logic.
- Define fields in
{ComponentName}.editor.lua
for each target object you want to notify. - In the runtime
{ComponentName}.lua
script, use theyahaha.EventSystem:NotifyObjectEvent(targetObject, "com.yahaha.sdk.trigger.CustomEvent")
function to send the event.
Code sample
local notifyObject
if notifyObject then
yahaha.EventSystem:NotifyObjectEvent(notifyObject, "com.yahaha.sdk.trigger.CustomEvent")
end
For example, with the following code when a player makes a certain action, it triggers thunder and darkness:
if do_action == "play_thunder" and script.fields.AudioThunder then
yahaha.EventSystem:NotifyObjectEvent(script.fields.AudioThunder, "com.yahaha.sdk.trigger.CustomEvent")
end
if do_action == "turnoff_light" and script.fields.Light then
SetDark()
end
Troubleshooting
- If events do not trigger:
- Check that the Custom Event Notifier is attached to the receiver object.
- Ensure the event name in your script matches the event configured in the Event Trigger.
- If no response or action:
- Verify that the Event Trigger is set up correctly and points to the right action (e.g., play audio).
- Make sure the referenced objects are correctly assigned in your script fields.
- If you encounter script errors or unexpected behavior:
- Use print/debug logs to confirm your script runs as expected.
- Double-check that all objects are active and available in the scene.