This is the place for discussion and support for LunaLua and related modifications and libraries.
Forum rules
Before you make a topic/post, consider the following:
-Is there a topic for this already?
-Is your post on topic/appropriate?
-Are you posting in the right forum/following the forum rules?
|
|
|
|
-
erkyp3rky
- Ninji
- Posts: 934
- Joined: Fri Apr 15, 2016 1:41 am
- Flair: formerly theloaflord
Postby erkyp3rky » Wed Jul 18, 2018 8:38 am
Hey, if you read the title, you might now know I've never used LunaLua because it's no secret to anybody that I can't code. Either that or I haven't really tried. Maybe both. I don't know. Anyways.
I've read in a few places that "Event fields" are possible in LunaLua, where you pass through a collision field and it triggers an event. I have no idea how to actually code, or code this. I also want it to happen once. This was supposed to be added in the new SMBX2 Beta 4 (I think.) but with the looks of that spreadsheet we'll be waiting until Half Life 3. for a while.
So, erm, who's first to step up and help a literal and metaphorical new-ster to coding?
|
|
|
|
|
|
|
|
|
-
ElectriKong
- Posts: 4650
- Joined: Mon Jun 06, 2016 4:32 pm
- Flair: I have NO idea what to put here
- Pronouns: he/him
-
Contact:
Postby ElectriKong » Wed Jul 18, 2018 9:12 am
Currently using invisible axes with nogravity = 1 can be used (the event field NPC in beta 4 is intended to replace this) to do this without the use of lunalua. As for collision fields with LunaLua, I don't know.
If you want to be able to code though, then you can learn by following tutorials or taking online courses etc.
|
|
|
|
|
|
|
|
|
-
Emral
- Cute Yoshi Egg
- Posts: 9729
- Joined: Mon Jan 20, 2014 12:58 pm
- Flair: Phoenix
Postby Emral » Wed Jul 18, 2018 9:41 am
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.
|
|
|
|
|
|
|
|
|
-
erkyp3rky
- Ninji
- Posts: 934
- Joined: Fri Apr 15, 2016 1:41 am
- Flair: formerly theloaflord
Postby erkyp3rky » Wed Jul 18, 2018 11:54 pm
So, just double checking, where it specifies the event, do I put my event name in the speech marks?
Added in 2 hours 45 minutes 34 seconds:
Enjl wrote: ↑Wed Jul 18, 2018 9:41 am
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.
The first one half works, however the event activates straight away. I don't have "autostart" ticked or anything like that either.
|
|
|
|
|
|
|
|
|
-
Emral
- Cute Yoshi Egg
- Posts: 9729
- Joined: Mon Jan 20, 2014 12:58 pm
- Flair: Phoenix
Postby Emral » Thu Jul 19, 2018 2:47 am
TheLoafLord wrote: ↑Thu Jul 19, 2018 2:40 am
So, just double checking, where it specifies the event, do I put my event name in the speech marks?
Added in 2 hours 45 minutes 34 seconds:
Enjl wrote: ↑Wed Jul 18, 2018 9:41 am
long quote applause
The first one half works, however the event activates straight away. I don't have "autostart" ticked or anything like that either.
Have you replaced triggerPosition with the position you would like? You can grab a reliable position by right-clicking a block on the X-coordinate you want and going to "Copy Preferences -> Position X,Y". Then you can paste that into the triggerPosition variable and remove everything that isn't the x-coordinate.
Just to have this said too: the first one assumes that you're moving from left to right (as seen through the use of the ">" symbol). If you want your player to approach the x-coordinate where the trigger happens from the right, you'll need "<".
Also keep in mind that Section 1 is the leftmost section, while Section 21 is naturally further right than any other. If that's the problem but you still want the player to trigger it by approaching it from the left, you can add a section check (or use the third code snippit where you'd need to define entire bounding boxes).
With added section check:
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
local triggerSection = 0 --0 is Section 1, 1 is Section 2, 2 is Section 3 .... 20 is Section 21.
function onTickEnd() --check every frame after SMBX did its movement code
if player.section == triggerSection and not hasTriggeredMyEvent then
if player.x > triggerPosition then
triggerEvent(triggeredEvent)
hasTriggeredMyEvent = true
end
end
end
Last edited by Emral on Thu Jul 19, 2018 2:48 am, edited 1 time in total.
|
|
|
|
|
|
|
|
|
-
erkyp3rky
- Ninji
- Posts: 934
- Joined: Fri Apr 15, 2016 1:41 am
- Flair: formerly theloaflord
Postby erkyp3rky » Thu Jul 19, 2018 2:47 am
Got it to work. Thanks!
Added in 1 minute 2 seconds:
Just checking in, with the Width and Height segments, does 1 block amount to 100 or 1?
|
|
|
|
|
|
|
|
|
-
Emral
- Cute Yoshi Egg
- Posts: 9729
- Joined: Mon Jan 20, 2014 12:58 pm
- Flair: Phoenix
Postby Emral » Thu Jul 19, 2018 2:51 am
TheLoafLord wrote: ↑Thu Jul 19, 2018 2:49 am
Got it to work. Thanks!
Added in 1 minute 2 seconds:
Just checking in, with the Width and Height segments, does 1 block amount to 100 or 1?
Width and height are measured in pixels, so the standard block dimensions are a width and height of 32. Long slopes have 64 width, loooooong slopes have 128, etc.
|
|
|
|
|
|
|
|
|
-
erkyp3rky
- Ninji
- Posts: 934
- Joined: Fri Apr 15, 2016 1:41 am
- Flair: formerly theloaflord
Postby erkyp3rky » Thu Jul 19, 2018 2:52 am
Enjl wrote: ↑Thu Jul 19, 2018 2:51 am
TheLoafLord wrote: ↑Thu Jul 19, 2018 2:49 am
Got it to work. Thanks!
Added in 1 minute 2 seconds:
Just checking in, with the Width and Height segments, does 1 block amount to 100 or 1?
Width and height are measured in pixels, so the standard block dimensions are a width and height of 32. Long slopes have 64 width, loooooong slopes have 128, etc.
Thank you so much, works like a charm.
|
|
|
|
|
Return to “LunaLua”
Users browsing this forum: No registered users and 1 guest
|