Triggers.lua
DEPRECATED IN LUNALUA V0.6!!! Use onEvent instead!!!
The Trigger library allows Lua code to be triggered by SMBX events. This can be used to trigger Lua when hitting a specific block, or killing a specific enemy. It does require a little bit of settings up in order to work correctly, though.
Installation
Place the file triggers.lua in the LuaScriptsLib folder.
How to use
1. Create a new layer in SMBX, and place an NPC on this layer. It is important that the NPC should not move, either horizontally or vertically. The axe is a recommended item, but objects such as coins would also work. For the smoothest results, place this NPC outside the section boundaries.
2. Make a note of the X and Y position of the NPC. You can find this in the SMBX Debugger (View>Debugger) or in the Item Properties in WohlEditor (Right Click>Item Properties). You will need these coordinates later.
3. Hide the layer containing this NPC.
4. Create an event in SMBX that shows this layer.
5. Create another event in SMBX that hides the layer again. This will be triggered from Lua, so make a note of what you call it. (In the example, it is referred to as "HideTriggerLayer").
This concludes the basic setup, and the library is now ready to use.
6. Load the library using the loadAPI function.
7. Create an array, and use the Trigger function to create a new trigger object. Here, you should enter the X and Y position of the trigger you noted in step 2, as well as the actions that should run when the event is triggered, and the name of the event you created in step 5, which resets the trigger.
8. You must manually call the testTriggers function, and specify the array of triggers to test, and what NPC ID is used for those triggers.
9. Run the game, and see that your Lua code is run from a SMBX event!
Example
After first creating a layer with an axe NPC, placed at the location (-200000,-200000) (just outside the bottom left of the default SMBX section), and creating an event to show this layer, and one called "HideTriggerLayer" that hides it again, this code will give the player a fire flower when the first event is triggered from SMBX:
local triggers = loadAPI("triggers");
local npcid = loadAPI("npcid");
trigs = {}
trigs[0] = triggers.Trigger(-200000,-200000,
function()
player.powerup = PLAYER_FIREFLOWER;
end,
"HideTriggerLayer");
function onLoop()
triggers.testTriggers(npcid.AXE,trigs);
end
Documentation
Structures
| Trigger | x | y | actions | event |
|---|---|---|---|---|
| number
The X coordinate of the trigger. |
number
The Y coordinate of the trigger. |
function
The Lua code to execute when triggered. |
string
The name of the SMBX event to execute after the trigger code. Typically, this event should hide the trigger layer. |
External Use Functions
These are functions you will need to use the library
| Trigger | x | y | actions | event | |
|---|---|---|---|---|---|
| Creates a new Trigger object. | Trigger | number
The X coordinate of the trigger. |
number
The Y coordinate of the trigger. |
function
The actions to perform when the trigger is activated. |
string:optional
The SMBX event to trigger after the Lua code has run. This typically hides the trigger layer. |
| testTriggers | triggerID | trigs | |||
| Tests the specified trigger list for the given trigger ID, and runs the Lua code if triggered. | nil | number
The NPC ID of the trigger NPCs in the given array of triggers. |
table(Trigger)
A table of Trigger objects, containing triggers of the given NPC ID. |
Internal Use Functions
These are functions that the library uses to function, but are not necessary for the user
| withinSquare | x | y | sqx | sqy | |
|---|---|---|---|---|---|
| Tests if the point (x,y) is within one SMBX tile of the point (sqx,sqy). | boolean | number
The X coordinate of the first point. |
number
The Y coordinate of the first point. |
number
The X coordinate of the second point. |
number
The Y coordinate of the second point. |
| getTrigger | npc | tx | ty | ||
| Determines whether the given trigger NPC has been triggered and is at the given position. | boolean | NPC
The trigger NPC. |
number
The X coordinate of the trigger NPC. |
number
The Y coordinate of the trigger NPC. | |
| resetTrigger | npc | eventName | |||
| Resets the given trigger for later use. | nil | NPC
The NPC to reset. |
string:optional
The SMBX event to run after resetting the NPC. This typically hides the NPC layer. |