irc.cat.biomario fan games galaxysmw central

firefox related css updates (bbcode/shoutbox) - ctrl-f5 if they're still jacked up for you

Lua help thread

If you lost something come look for it here
User avatar
Hoeloe
One of them Lua people
Posts: 807
Joined: 5 years ago
Location: Spaaace

Lua help thread

Postby Hoeloe » 2 years ago

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

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
Hoeloe
One of them Lua people
Posts: 807
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
Hoeloe
One of them Lua people
Posts: 807
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
Hoeloe
One of them Lua people
Posts: 807
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
TaviTurnip
I'm just here to remind people I'm here
Abministrator
Posts: 984
Joined: 7 years ago
First name: Rena, despite assumptions
Location: Up

Re: Lua help thread

Postby TaviTurnip » 2 years ago

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'm just here to annoy you so that you aren't here instead.

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
Dog In Da Grass
Perro de los Muertos
Posts: 433
Joined: 3 years ago
First name: Dog
Pronouns: that dog
Location: somewhere between the sacred silence and sleep

Re: Lua help thread

Postby Dog In Da Grass » 2 years ago

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 on 21 Mar 2015, 18:42, edited 1 time in total.
Image
Image
Image

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
Dog In Da Grass
Perro de los Muertos
Posts: 433
Joined: 3 years ago
First name: Dog
Pronouns: that dog
Location: somewhere between the sacred silence and sleep

Re: Lua help thread

Postby Dog In Da Grass » 2 years ago

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

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
Hoeloe
One of them Lua people
Posts: 807
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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

User avatar
TaviTurnip
I'm just here to remind people I'm here
Abministrator
Posts: 984
Joined: 7 years ago
First name: Rena, despite assumptions
Location: Up

Re: Lua help thread

Postby TaviTurnip » 2 years ago

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'm just here to annoy you so that you aren't here instead.

User avatar
Rednaxela
Maker of Shenanigans
Posts: 654
Joined: 3 years ago

Re: Lua help thread

Postby Rednaxela » 2 years ago

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?
Currently LPing: Ys Origin

User avatar
TaviTurnip
I'm just here to remind people I'm here
Abministrator
Posts: 984
Joined: 7 years ago
First name: Rena, despite assumptions
Location: Up

Re: Lua help thread

Postby TaviTurnip » 2 years ago

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'm just here to annoy you so that you aren't here instead.

User avatar
Rednaxela
Maker of Shenanigans
Posts: 654
Joined: 3 years ago

Re: Lua help thread

Postby Rednaxela » 2 years ago

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 19 times
Currently LPing: Ys Origin

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
KingTwelveSixteen
🐩 Title Title Title Title 🐩
Posts: 30
Joined: 6 years ago
Location: USA

Re: Lua help thread

Postby KingTwelveSixteen » 2 years ago

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.

Zyglrox Odyssey
Posts: 511
Joined: 4 years ago
Contact:

Re: Lua help thread

Postby Zyglrox Odyssey » 2 years ago

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

User avatar
Rednaxela
Maker of Shenanigans
Posts: 654
Joined: 3 years ago

Re: Lua help thread

Postby Rednaxela » 2 years ago

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.
Currently LPing: Ys Origin

User avatar
Horikawa Otane
The MaGL Goddess
Posts: 1412
Joined: 4 years ago
First name: Servalan
Location: The Last of the Babylon Stations

Re: Lua help thread

Postby Horikawa Otane » 2 years ago

Guys remember to preface your variable names with your username and/or level name to ensure no conflicts.
Image
Watch my Butt on the YouTubes
raocow wrote:Dang it Oyster this level is not called Peregrine Penguin and the Soft Bon-Bon, it clearly says Ghost House with a Ghost in It!

User avatar
Hoeloe
One of them Lua people
Posts: 807
Joined: 5 years ago
Location: Spaaace

Re: Lua help thread

Postby Hoeloe » 2 years ago

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


Return to “Magl X : the Lost Levels”

Who is online

Users browsing this forum: No registered users and 1 guest

cron