(shouting)

Lua help thread

If you lost something come look for it here
Post Reply
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Lua help thread

Post by Hoeloe »

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
Image
Image
Image
Image
Image
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

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
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
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.
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

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.
Zyglrox Odyssey wrote: Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
Nothing that immediately jumps out. What's actually happening here?
Image
Image
Image
Image
Image
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

Hoeloe wrote:
Zyglrox Odyssey wrote: Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
Nothing that immediately jumps out. What's actually happening here?
Alright, good.
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.
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

Zyglrox Odyssey wrote:
Hoeloe wrote:
Zyglrox Odyssey wrote: Also Hoeloe you know lua, is there anything glaringly stupid in my code there?
Nothing that immediately jumps out. What's actually happening here?
Alright, good.
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.
Ah, I assumed there was an issue with it, but it all looks fairly sensible, and working code is good code after all.
Image
Image
Image
Image
Image
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

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.
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.
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

Zyglrox Odyssey wrote:
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.
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.
Only thing I would say is that "frame" should probably be a boolean (value of true or false), rather than a number.
Image
Image
Image
Image
Image
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

Hoeloe wrote:Only thing I would say is that "frame" should probably be a boolean (value of true or false), rather than a number.
That would make a bit more sense. Alright, noted, thanks.
User avatar
TaviTurnip
Reach for the top
Abministrator
Posts: 1077
Joined: 14 years ago
First name: Rena
Location: Winter

Re: Lua help thread

Post by TaviTurnip »

Zyglrox Odyssey wrote:[Lua stuff]
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?
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!
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

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
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 another

Code: Select all

	if event == "[your event name here]" then
		speed = [your new speed here]
	end
in that function define thing.
User avatar
Dog In Da Grass
Woof!
Posts: 522
Joined: 10 years ago
First name: Dog
Pronouns: that dog
Location: Da Grass

Re: Lua help thread

Post by Dog In Da Grass »

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?

Code: Select all

function onEvent(event)
	if event == "[your event name here]" then
		speed = [your new speed here]
	end
end
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 another

Code: Select all

	if event == "[your event name here]" then
		speed = [your new speed here]
	end
in that function define thing.


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
I 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?

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
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.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
Basically start up an internal timer (and print it to the screen for debug purposes then change the speed whenever it hits a certain value.
Last edited by Dog In Da Grass 9 years ago, edited 1 time in total.
:pal: Image :pal:

Image
Image
Image
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

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 are

Code: Select all

function onEvent(event)
   if event=="autosound2" then
        playSFX(19)
        speed=6
   end 
   playSFX(21)
end
I 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?
onEvent is a recent-ish addition to LunaLua, the version in the latest devkit doesn't have it implemented. Get the latest version here.
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
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.
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).
User avatar
Dog In Da Grass
Woof!
Posts: 522
Joined: 10 years ago
First name: Dog
Pronouns: that dog
Location: Da Grass

Re: Lua help thread

Post by Dog In Da Grass »

Zyglrox Odyssey wrote:
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 are

Code: Select all

function onEvent(event)
   if event=="autosound2" then
        playSFX(19)
        speed=6
   end 
   playSFX(21)
end
I 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?
onEvent is a recent-ish addition to LunaLua, the version in the latest devkit doesn't have it implemented. Get the latest version here.
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
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.
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).

Okay thank you, that explains a lot.
:pal: Image :pal:

Image
Image
Image
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

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.)
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

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.)
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.
Image
Image
Image
Image
Image
User avatar
TaviTurnip
Reach for the top
Abministrator
Posts: 1077
Joined: 14 years ago
First name: Rena
Location: Winter

Re: Lua help thread

Post by TaviTurnip »

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!
User avatar
Rednaxela
Maker of Shenanigans
Posts: 897
Joined: 10 years ago
Pronouns: they/them
https://rednaxela.talkhaus.com

Re: Lua help thread

Post by Rednaxela »

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? ;_;
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?
User avatar
TaviTurnip
Reach for the top
Abministrator
Posts: 1077
Joined: 14 years ago
First name: Rena
Location: Winter

Re: Lua help thread

Post by TaviTurnip »

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?
I copied and pasted directly from above, but nevertheless:

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
Is this incorrect or did I mess up copying it somehow?
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!
User avatar
Rednaxela
Maker of Shenanigans
Posts: 897
Joined: 10 years ago
Pronouns: they/them
https://rednaxela.talkhaus.com

Re: Lua help thread

Post by Rednaxela »

RenaTurnip wrote:Is this incorrect or did I mess up copying it somehow?
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.

Test level to show this is attached.
ScrollTest.zip
(1.23 KiB) Downloaded 172 times
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

Hoeloe wrote:
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.)
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.
...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.
Rednaxela wrote:
RenaTurnip wrote:Is this incorrect or did I mess up copying it somehow?
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.
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).
What else, if anything, is in your lunadll.lua?
KingTwelveSixteen
? Title Title Title Title ?
Posts: 30
Joined: 14 years ago
Location: USA

Re: Lua help thread

Post by KingTwelveSixteen »

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? ;_;
Did you set the bounds of the level to one screen? I remember something about not doing that screwing someone up previously.
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

KingTwelveSixteen wrote:
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? ;_;
Did you set the bounds of the level to one screen? I remember something about not doing that screwing someone up previously.
That's with actual SMBX autoscroll. That code does that automatically wherever you enter the section... when it's working, of course.
User avatar
Rednaxela
Maker of Shenanigans
Posts: 897
Joined: 10 years ago
Pronouns: they/them
https://rednaxela.talkhaus.com

Re: Lua help thread

Post by Rednaxela »

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.
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

Horikawa Otane wrote:Guys remember to preface your variable names with your username and/or level name to ensure no conflicts.
Not necessary with the latest version. You can save variables directly into the level folder now.
Image
Image
Image
Image
Image
User avatar
Sorel
In the shadows
Posts: 702
Joined: 10 years ago
First name: Alexander
Pronouns: he/him/his
Location: some place

Re: Lua help thread

Post by Sorel »

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.
Former active member, yearly lurker now.
Post Reply