Triggers.lua

Download Latest Version

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:

External Use Functions
These are functions you will need to use the library

Internal Use Functions
These are functions that the library uses to function, but are not necessary for the user