There's a pretty easy solution for this. It's not very flexible, but in small amounts this is a decent solution:
Code: Select all
local hasTriggeredMyEvent = false --make sure it only happens once
--edit these two
local triggerPosition = -199400 --position which needs to be passed. Imagine it as a vertical line.
local triggeredEvent = "myEvent" --name of the event
function onTickEnd() --check every frame after SMBX did its movement code
if not hasTriggeredMyEvent then
if player.x > triggerPosition then
triggerEvent(triggeredEvent)
hasTriggeredMyEvent = true
end
end
end
Essentially what this does is wait for the player to step past a certain boundary. If the boundary needs to be on seperate axes or a box, further checks can be added. Such as one for player.x < triggerPosition + triggerZoneWidth, or an equivalent to the two along the y-axis with seperate coordinates.
When doing that, though, it might be smart to wrap triggerPosition into a table:
Code: Select all
local myTrigger = {
x=-199400,
y=-200300,
width=400,
height=300,
event="myEvent",
triggered=false
}
function onTickEnd()
if not myTrigger.triggered then
if player.x + player.width > myTrigger.x
and player.x < myTrigger.x + myTrigger.width
and player.y + player.height > myTrigger.y
and player.y < myTrigger.y + myTrigger.height
then
myTrigger.triggered = true
triggerEvent(myTrigger.event)
end
end
end
You can even go a step further and automate it for an arbitrary number of trigger zones:
Code: Select all
local triggers = {}
local function addTrigger(x,y,w,h,event)
return {
x=x,
y=y,
width=w,
height=h,
event=event,
triggered=false
}
end
table.insert(triggers, addTrigger(-199400, -200300, 400, 300, "myEvent"))
table.insert(triggers, addTrigger(-192400, -209300, 800, 200, "myEvent2"))
function onTickEnd()
for _, triggerZone in ipairs(triggers) do
if not triggerZone.triggered then
if player.x + player.width > triggerZone.x
and player.x < triggerZone.x + triggerZone.width
and player.y + player.height > triggerZone.y
and player.y < triggerZone.y + triggerZone.height
then
triggerZone .triggered = true
triggerEvent(triggerZone.event)
end
end
end
end
That for loop will loop over any zones you have in the triggers table, and you just have to use the table.insert lines to add more.
The next more convenient step would be to make it so that the zones are defined through a placed sizeable which is set to hidden in onStart, but that requires a bit more lua knowledge and me just typing that out could get confusing very quickly. This should be good enough for now though.
If you have any questions about the code, don't hesitate to ask.