Simple Trigger that calls Lua
From Valve Developer Community
- Step 1: Create a box with the block tool ( + ). Make sure you have the "toolstrigger.vmat" material selected. This box will serve as the trigger area.
- Step 2: Select the box and go to Tools > Tie To Entity ( + ). This will create a trigger entity that is tied to your box.
- Step 3: Select the trigger and change its class to "trigger_hero". This trigger is meant to be used for when a player enters or leaves the trigger area. There are many types of triggers, but let's keep it simple for now.
- Step 4: Set a name for your trigger, and set the path to the script file you want to call functions from. Example: If you want to call a function from
scripts/vscripts/my_file.luawhen the trigger fires, you should set Entity Scripts: my_file.lua
- Warning: When you set Entity Scripts: my_file.lua, the game will call the
my_file.lua(if there is one) when your addon is first loaded. So don't set Entity Scripts: addon_game_mode.lua unless you want the
addon_game_mode.luato be called several times.
- Step 5: With the trigger selected press + to open the Object Properties window, then select the Outputs tabs.
- Step 6: Add an output named "OnStartTouch". Set the trigger as the target. The input should be "CallScriptFunction" and the parameter should be the name of the function you want to be called (e.g. OnStartTouch or OnEndTouch).
- Step 7: Add another output with the same values, but the name should be "OnEndTouch". You probably want to set a different function name as well.
Example lua code:
function OnStartTouch(trigger) print(trigger.activator) print(trigger.caller) end function OnEndTouch(trigger) print(trigger.activator) print(trigger.caller) end