Lua help thread
Lua help thread
There's already a help thread, but Lua is a whole 'nother thing to tackle, so I thought I'd set up a thread specifically for help with Lua.
Starting with the tutorials from the website:
LunaLua basics
Simple filters
Music and sounds
Starting with the tutorials from the website:
LunaLua basics
Simple filters
Music and sounds
Re: Lua help thread
I kinda figured the one help thread would work alright for any sort of question - especially given how often I at least seem to default to "use the Luna" as the solution to everything - but I suppose splitting off lua-specific questions into a separate thread for ease of access for lua people couldn't really hurt.
Anyway I dunno if the quote-to-ping thingy actually works but
That should start up autoscroll in section 1 from wherever you enter it from, be it the start of the level, a midpoint, or a warp (haven't tested the warp though). I haven't quite implemented stopping it yet, but for the purposes of putting together the autoscroll section I don't expect that it'd be all that important.
(left is how far from the left side of the screen you start, speed is of course the speed)
Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
e: Code edited to use a boolean.
Anyway I dunno if the quote-to-ping thingy actually works but
RenaTurnip wrote:[something about SMBX autoscroll being a butt]
Code: Select all
left = 198
right = 800 - left
speed = 2
function onLoadSection0(asdf)
frame = false
end
function onLoopSection0(asdf)
if asdf == 1 then
screen = player.sectionObj.boundary
if frame == false then
screen.left = player.x - left
screen.right = player.x + right
frame = true
end
screen.left = screen.left + speed
screen.right = screen.right + speed
player.sectionObj.boundary = screen
end
end
(left is how far from the left side of the screen you start, speed is of course the speed)
Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
e: Code edited to use a boolean.
Re: Lua help thread
I figured that Lua questions can get quite complex, and often end up having little to do with SMBX, as it's often the inner workings of Lua that is the problem. I figured since the help required is so different, a Lua help thread could be useful.
Nothing that immediately jumps out. What's actually happening here?Zyglrox Odyssey wrote: Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
Re: Lua help thread
Alright, good.Hoeloe wrote:Nothing that immediately jumps out. What's actually happening here?Zyglrox Odyssey wrote: Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
What's happening there is on the first frame in the section it sets the section boundaries to one screen around where the player is, and then it moves them to the right by 2 every frame. It's basically the same thing SMBX autoscroll does, from what I've gathered, only doing it like this it can start anywhere. With a bit of tweaking it could go in any other direction as well, in theory.
Re: Lua help thread
Ah, I assumed there was an issue with it, but it all looks fairly sensible, and working code is good code after all.Zyglrox Odyssey wrote:Alright, good.Hoeloe wrote:Nothing that immediately jumps out. What's actually happening here?Zyglrox Odyssey wrote: Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
What's happening there is on the first frame in the section it sets the section boundaries to one screen around where the player is, and then it moves them to the right by 2 every frame. It's basically the same thing SMBX autoscroll does, from what I've gathered, only doing it like this it can start anywhere. With a bit of tweaking it could go in any other direction as well, in theory.
Re: Lua help thread
I suppose I did kinda present it in a way that could be construed as there being something wrong with it, didn't I. Whoops. But yeah it all works as intended, I was just concerned if any of it was dumb and inefficiently coded or anything.Hoeloe wrote:Ah, I assumed there was an issue with it, but it all looks fairly sensible, and working code is good code after all.
Re: Lua help thread
Only thing I would say is that "frame" should probably be a boolean (value of true or false), rather than a number.Zyglrox Odyssey wrote:I suppose I did kinda present it in a way that could be construed as there being something wrong with it, didn't I. Whoops. But yeah it all works as intended, I was just concerned if any of it was dumb and inefficiently coded or anything.Hoeloe wrote:Ah, I assumed there was an issue with it, but it all looks fairly sensible, and working code is good code after all.
Re: Lua help thread
That would make a bit more sense. Alright, noted, thanks.Hoeloe wrote:Only thing I would say is that "frame" should probably be a boolean (value of true or false), rather than a number.
- TaviTurnip
- Reach for the top
- Abministrator
- Posts: 1077
- Joined: 14 years ago
- First name: Rena
- Location: Winter
Re: Lua help thread
Judging from how frustrating autoscroll is becoming (you can't seem to change the autoscroll speed once it starts, thanks SMBx??? <_<) and since there's no deadline looming over our heads anymore, I'm going to finally submit and use Lua when needed. Let's pretend I'm either using default autoscroll or using the Lua code you gave me to start one; what would I do with your code if I wanted the autoscroll to slow down midway into the level after an event is started?Zyglrox Odyssey wrote:[Lua stuff]
I regularly stream on Twitch! with other members of the talkhaus. Come watch Monday, Tuesday and Friday at 2PM for blind playthroughs and Pokémon and Touhou and stuff. Come hang out with us!
Re: Lua help thread
RenaTurnip wrote:Judging from how frustrating autoscroll is becoming (you can't seem to change the autoscroll speed once it starts, thanks SMBx??? <_<) and since there's no deadline looming over our heads anymore, I'm going to finally submit and use Lua when needed. Let's pretend I'm either using default autoscroll or using the Lua code you gave me to start one; what would I do with your code if I wanted the autoscroll to slow down midway into the level after an event is started?
Code: Select all
function onEvent(event)
if event == "[your event name here]" then
speed = [your new speed here]
end
end
Code: Select all
if event == "[your event name here]" then
speed = [your new speed here]
end
- Dog In Da Grass
- Woof!
- Posts: 522
- Joined: 11 years ago
- First name: Dog
- Pronouns: that dog
- Location: Da Grass
Re: Lua help thread
Zyglrox Odyssey wrote:RenaTurnip wrote:Judging from how frustrating autoscroll is becoming (you can't seem to change the autoscroll speed once it starts, thanks SMBx??? <_<) and since there's no deadline looming over our heads anymore, I'm going to finally submit and use Lua when needed. Let's pretend I'm either using default autoscroll or using the Lua code you gave me to start one; what would I do with your code if I wanted the autoscroll to slow down midway into the level after an event is started?Just drop that in the bottom of your lunadll.lua (replacing [your event name here] with your event name and [your new speed here] with whatever speed, of course) and it'll change the speed when the event's triggered. And if you need any further speed changes just put anotherCode: Select all
function onEvent(event) if event == "[your event name here]" then speed = [your new speed here] end end
in that function define thing.Code: Select all
if event == "[your event name here]" then speed = [your new speed here] end
This has been giving me a lot more trouble than it should >w>
My LunaLua isnt even recognizing the function as a thing and isn't triggering when my events are
Code: Select all
function onEvent(event)
if event=="autosound2" then
playSFX(19)
speed=6
end
playSFX(21)
end
As another note, is there a way to initialize variables only ONCE? it seems like a glaring flaw of Lua that it constantly is resetting the variables back to what they where initially set as
Code: Select all
//not actual code but just a demo
speed = 2
if something then
speed=6
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EDIT: I found a way to brute force it =P
Code: Select all
function onLoop()
bark=bark+1;
printText(tostring(bark),3,0,0);
if bark==650 then
speed=2
end
if bark==800 then
speed=6
end
end
Last edited by Dog In Da Grass 9 years ago, edited 1 time in total.
Re: Lua help thread
onEvent is a recent-ish addition to LunaLua, the version in the latest devkit doesn't have it implemented. Get the latest version here.Dog In Da Grass wrote:This has been giving me a lot more trouble than it should >w>
My LunaLua isnt even recognizing the function as a thing and isn't triggering when my events areI have it so that when entering a certain event it changes the auto scroll speed and plays a sound to let me know it did and another sound to let me know the function triggered.... but neither of them did! D: Am I doing something wrong or is my lunalua broken because the "function onEvent()" doesn't trigger when an event happens?Code: Select all
function onEvent(event) if event=="autosound2" then playSFX(19) speed=6 end playSFX(21) end
Put the speed = 2 in one of the onLoad functions rather than onLoop. onLoad runs only on loading the level (or section, depending which you use).As another note, is there a way to initialize variables only ONCE? it seems like a glaring flaw of Lua that it constantly is resetting the variables back to what they where initially set as
yup, because if I change this at the end of the file it immediately goes back to being 2 and I find that a big thorn in my paw.Code: Select all
//not actual code but just a demo speed = 2 if something then speed=6 end
- Dog In Da Grass
- Woof!
- Posts: 522
- Joined: 11 years ago
- First name: Dog
- Pronouns: that dog
- Location: Da Grass
Re: Lua help thread
Zyglrox Odyssey wrote:onEvent is a recent-ish addition to LunaLua, the version in the latest devkit doesn't have it implemented. Get the latest version here.Dog In Da Grass wrote:This has been giving me a lot more trouble than it should >w>
My LunaLua isnt even recognizing the function as a thing and isn't triggering when my events areI have it so that when entering a certain event it changes the auto scroll speed and plays a sound to let me know it did and another sound to let me know the function triggered.... but neither of them did! D: Am I doing something wrong or is my lunalua broken because the "function onEvent()" doesn't trigger when an event happens?Code: Select all
function onEvent(event) if event=="autosound2" then playSFX(19) speed=6 end playSFX(21) end
Put the speed = 2 in one of the onLoad functions rather than onLoop. onLoad runs only on loading the level (or section, depending which you use).As another note, is there a way to initialize variables only ONCE? it seems like a glaring flaw of Lua that it constantly is resetting the variables back to what they where initially set as
yup, because if I change this at the end of the file it immediately goes back to being 2 and I find that a big thorn in my paw.Code: Select all
//not actual code but just a demo speed = 2 if something then speed=6 end
Okay thank you, that explains a lot.
Re: Lua help thread
The multipoints library seems to be kinda not working, telling me "module 'triggers' not found" on line 1.
I have the version from the last devkit put out during MaGL X2's submission period, I believe. Is my problem just that I have an outdated version and need to update?
(I would just try it but that's kinda a lot more effort than I feel like putting in at the moment to maybe fix a thing, given that my SMBXing computer has no internet.)
I have the version from the last devkit put out during MaGL X2's submission period, I believe. Is my problem just that I have an outdated version and need to update?
(I would just try it but that's kinda a lot more effort than I feel like putting in at the moment to maybe fix a thing, given that my SMBXing computer has no internet.)
Re: Lua help thread
Yes, that's the reason. The version in the devkit is very outdated and isn't compatible with newer versions of LunaLua. The latest version doesn't even use the triggers library, since that was deprecated with the addition of onEvent.Zyglrox Odyssey wrote:The multipoints library seems to be kinda not working, telling me "module 'triggers' not found" on line 1.
I have the version from the last devkit put out during MaGL X2's submission period, I believe. Is my problem just that I have an outdated version and need to update?
(I would just try it but that's kinda a lot more effort than I feel like putting in at the moment to maybe fix a thing, given that my SMBXing computer has no internet.)
- TaviTurnip
- Reach for the top
- Abministrator
- Posts: 1077
- Joined: 14 years ago
- First name: Rena
- Location: Winter
Re: Lua help thread
Okaaaaay, so I was busy yesterday and only finally got to actually using the Lua autoscroll today instead of the in-engine one, and it works great! ... Except that the player can outrun the autoscroll if he or she wishes. I can just completely run through everything and the level won't scroll until the scrolling itself catches up with me. Basically the player can move the camera, and that's a problem. Is there something that can be done about that? ;_;
I regularly stream on Twitch! with other members of the talkhaus. Come watch Monday, Tuesday and Friday at 2PM for blind playthroughs and Pokémon and Touhou and stuff. Come hang out with us!
- Rednaxela
- Maker of Shenanigans
- Posts: 897
- Joined: 10 years ago
- Pronouns: they/them
- https://rednaxela.talkhaus.com
Re: Lua help thread
Are you using pretty much the exact same code as above? It sounds to me like the code you're using is setting the left boundary as you expect, but something is going wrong with the right boundary (assuming your level is a usual left-to-right one). Perhaps paste what code you're using?RenaTurnip wrote:Okaaaaay, so I was busy yesterday and only finally got to actually using the Lua autoscroll today instead of the in-engine one, and it works great! ... Except that the player can outrun the autoscroll if he or she wishes. I can just completely run through everything and the level won't scroll until the scrolling itself catches up with me. Basically the player can move the camera, and that's a problem. Is there something that can be done about that? ;_;
- TaviTurnip
- Reach for the top
- Abministrator
- Posts: 1077
- Joined: 14 years ago
- First name: Rena
- Location: Winter
Re: Lua help thread
I copied and pasted directly from above, but nevertheless:Rednaxela wrote: Are you using pretty much the exact same code as above? It sounds to me like the code you're using is setting the left boundary as you expect, but something is going wrong with the right boundary. Perhaps paste what code you're using?
Code: Select all
left = 198
right = 800 - left
speed = 2
function onLoadSection0(asdf)
frame = false
end
function onLoopSection0(asdf)
if asdf == 1 then
screen = player.sectionObj.boundary
if frame == false then
screen.left = player.x - left
screen.right = player.x + right
frame = true
end
screen.left = screen.left + speed
screen.right = screen.right + speed
player.sectionObj.boundary = screen
end
end
I regularly stream on Twitch! with other members of the talkhaus. Come watch Monday, Tuesday and Friday at 2PM for blind playthroughs and Pokémon and Touhou and stuff. Come hang out with us!
- Rednaxela
- Maker of Shenanigans
- Posts: 897
- Joined: 10 years ago
- Pronouns: they/them
- https://rednaxela.talkhaus.com
Re: Lua help thread
You didn't mess up copying... there must be something else happening with the way your level is set up, because I pasted exactly what you had there and nothing else into a test level, and it worked perfectly. The player was in fact not able to control the horizontal camera movement at all.RenaTurnip wrote:Is this incorrect or did I mess up copying it somehow?
Test level to show this is attached.
Re: Lua help thread
...Yeah I suppose it would be deprecated at this point wouldn't it. Really should've realized that, that was some grade-A dumb on my end.Hoeloe wrote:Yes, that's the reason. The version in the devkit is very outdated and isn't compatible with newer versions of LunaLua. The latest version doesn't even use the triggers library, since that was deprecated with the addition of onEvent.Zyglrox Odyssey wrote:The multipoints library seems to be kinda not working, telling me "module 'triggers' not found" on line 1.
I have the version from the last devkit put out during MaGL X2's submission period, I believe. Is my problem just that I have an outdated version and need to update?
(I would just try it but that's kinda a lot more effort than I feel like putting in at the moment to maybe fix a thing, given that my SMBXing computer has no internet.)
Yeah I just replaced my level's entire lunadll.lua with that code and it worked fine on my end. Even with warps (which I'm glad to have confirmed functional, I was a bit worried about that actually).Rednaxela wrote:You didn't mess up copying... there must be something else happening with the way your level is set up, because I pasted exactly what you had there and nothing else into a test level, and it worked perfectly. The player was in fact not able to control the horizontal camera movement at all.RenaTurnip wrote:Is this incorrect or did I mess up copying it somehow?
What else, if anything, is in your lunadll.lua?
-
- ? Title Title Title Title ?
- Posts: 30
- Joined: 14 years ago
- Location: USA
Re: Lua help thread
Did you set the bounds of the level to one screen? I remember something about not doing that screwing someone up previously.RenaTurnip wrote:Okaaaaay, so I was busy yesterday and only finally got to actually using the Lua autoscroll today instead of the in-engine one, and it works great! ... Except that the player can outrun the autoscroll if he or she wishes. I can just completely run through everything and the level won't scroll until the scrolling itself catches up with me. Basically the player can move the camera, and that's a problem. Is there something that can be done about that? ;_;
Re: Lua help thread
That's with actual SMBX autoscroll. That code does that automatically wherever you enter the section... when it's working, of course.KingTwelveSixteen wrote:Did you set the bounds of the level to one screen? I remember something about not doing that screwing someone up previously.RenaTurnip wrote:Okaaaaay, so I was busy yesterday and only finally got to actually using the Lua autoscroll today instead of the in-engine one, and it works great! ... Except that the player can outrun the autoscroll if he or she wishes. I can just completely run through everything and the level won't scroll until the scrolling itself catches up with me. Basically the player can move the camera, and that's a problem. Is there something that can be done about that? ;_;
- Rednaxela
- Maker of Shenanigans
- Posts: 897
- Joined: 10 years ago
- Pronouns: they/them
- https://rednaxela.talkhaus.com
Re: Lua help thread
Y'know, that gives an idea of one thing that might explain RenaTurnip's level's symptoms. It may explain what was described if the in-engine SMBX autoscroll is still enabled.
Re: Lua help thread
Not necessary with the latest version. You can save variables directly into the level folder now.Horikawa Otane wrote:Guys remember to preface your variable names with your username and/or level name to ensure no conflicts.
- Sorel
- In the shadows
- Posts: 702
- Joined: 10 years ago
- First name: Alexander
- Pronouns: he/him/his
- Location: some place
Re: Lua help thread
Hm, is there a way to trigger some sort of autoscroll with lua?
I want the autoscroll to start at section 2 instead of 1 and to change directions, but the way the engine works, you can only do autoscroll in section 1, with only one direction.
I want the autoscroll to start at section 2 instead of 1 and to change directions, but the way the engine works, you can only do autoscroll in section 1, with only one direction.
Former active member, yearly lurker now.