Triggering a custom event upon item collection
This tutorial is intended for Horror Game Kit users.
Overview
This tutorial guides creators to create a custom event—triggering thunder, rain, and lighting changes—when the player collects an item. Using Yahaha's Custom Event system and Lua scripting, you will extend the kit’s capabilities beyond built-in components.
Prerequisites
Before starting, ensure you have:
- Familiarity with Horror Game Kit, especially the Event Trigger component.
 - Basic knowledge of the Lua programming language.
 - Experience with Lua Components or completion of the Quick Start Guide
 
Step 1: Set up thunder audio
- 
In Scene Explorer, create a new audio object by choosing Create > Audio.

 - 
(Optional) Rename the audio object to Thunder.
 - 
Open the object's Properties window and assign a thunder audio clip (e.g., thunder (1)).
 - 
Add a Custom Event Notifier component to the audio by choosing Add Components > Custom Event Notifier.
 - 
Configure the Event Trigger component on the audio:
- Trigger Object: Self
 - Trigger Event: CustomEvent
 - Action Object: Self
 - Action Function: Play
 
Leave other fields as default. This setup plays the thunder sound when the custom event fires.

 
Step 2: Set up lighting
- In Scene Explorer, add a point light source by choosing Create > Light > Point Light.
 - Position and adjust brightness to contrast with a dark environment.
 - Add the Custom Event Notifier component to the light.
This allows the light to respond to custom event notifications.

 
Step 3: Add a collectible
- From Packages, add the Collectable Item smart asset to your scene. It includes built-in collectible logic.
 - Position it as desired.

 
Step 4: Create and configure an empty object for event handling
- 
In Scene Explorer, add an empty object from Create > Empty Object to your scene. This will host the Lua script controlling your custom event logic.
 - 
Attach components to the empty object
 - 
Add a Custom Event Notifier component.
 - 
Add an Event Trigger component with the following settings:
- Trigger Object: The collectible item
 - Trigger Event: OnPropInteracted
 - Action Object: Self
 - Action Function: NotifyCustomEventNow Leave other fields as default.
 
This config sends a custom event to the empty object when the collectible is interacted with.

 
Step 5: Define and assign script fields
- Add a Lua script to the empty object by clicking Add script, then name it CustomEvent.
 - In the 
CustomEvent.luascript, define fields for audio, light, and empty object: 
local fieldDefs = {
    {
        name = "AudioThunder",
        type = "GameObject"
    },
    {
        
        name = "Light",
        type = "GameObject",
        
    },
    {
        name = "triggerEventObject",
        type = "GameObject"
    },
}
script.DefineFields(fieldDefs)
- In the empty object's Properties window, assign the thunder audio, point light, and empty object to these fields.

 
Step 6: Implement runtime logic
In the empty object's CustomEvent.editor.lua script, add the following script to control the weather, audio and lighting effects:
local YaResourceManager = YahahaMiddleLayerSlim.Resource.YaResourceManager 
local skySystem = require("com.yahaha.sdk.graphics.SkyUtils")
local weatherSystem = require("com.yahaha.sdk.graphics.WeatherUtils")
-- Script to trigger dark weather effects (thunder, rain, lightning changes)
-- Called during in-game response to trigger dark mode
function response_in_game()
    SetDark()
end
-- Activates thunder audio, darkens Sun, starts to rain, and disables light 
function SetDark()
    script.fields.AudioThunder:SetActive(true)
    skySystem.SunColor = skySystem.SunColor + Color.New(0,0,0,255)
    weatherSystem.ToRain()
    if script.fields.Light then
        script.fields.Light:SetActive(false)
    end
    if script.fields.AudioThunder then
        yahaha.EventSystem:NotifyObjectEvent(script.fields.AudioThunder, "com.yahaha.sdk.trigger.CustomEvent")
    end
end
local eventObject = script.fields.triggerEventObject
-- Callback to run SetDark when event fires
local callback = function()
    -- Your loigc here
    SetDark()
end
-- Register callback to custom event if eventObject exists
if eventObject then
    yahaha.EventSystem:RegisterObjectEvent(eventObject, "com.yahaha.sdk.trigger.CustomEvent", callback)
end
-- Clean up event registration on script disposal
script.OnDispose(function()
    yahaha.EventSystem:UnregisterObjectEvent(eventObject, "com.yahaha.sdk.trigger.CustomEvent", callback)
end)
Step 7: Playtest
Enter Play Mode and collect the item. Confirm that thunder sounds play, rain starts, and lighting changes as scripted.
Related resources
- Custom event notifier
 - For detailed environment system APIs, refer to the Sky and Weather documentation.
 - Use F5 to start playtesting and F12 to open the console for debugging.