(shouting)

Lua help thread

If you lost something come look for it here
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:Colliders is amazing btw Hoeloe I'm using it extensively.
Thanks! I've got an improved version sitting by waiting for some OpenGL commands so I can make a debug drawing option.

The unrealeased version has support for polygon colliders of arbitrary shape. I do want to add tests for things like Link's down slash too.
Image
Image
Image
Image
Image
User avatar
snoruntpyro
cutest girl 2023
Posts: 884
Joined: 9 years ago
Pronouns: she/her
Contact:

Re: Lua help thread

Post by snoruntpyro »

Is there any way to check if the player is currently being hit? I need to make it so autoscrolling will stop temporarily when the player in that state.
Image
Image
Image
Image
Image
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

Hoeloe wrote:
Zyglrox Odyssey wrote:Okay it doesn't kill the enemy anymore but now instead if I hold the spinjump button the player dies? Or gets hurt, at least.
You should be using the regular "collides" function for damaging the player, and the "bounce" function for spinjumping. Otherwise, this is the same problem. What you can do it add a delay after a spinjump so that you have a few frames of invincibility to that enemy after a spinjump. I realise that's not exactly elegant, but most of SMBX isn't, either.
I was using speedCollide, which wasn't exactly helping matters. Aside from that I set it up to just move the player up a couple pixels if the thing would otherwise be happening, or something idk i haven't messed with the level in like a week or so in all honesty. Anyway thanks, I believe I have it all working without issue now.
Pyro wrote:Is there any way to check if the player is currently being hit? I need to make it so autoscrolling will stop temporarily when the player in that state.

Code: Select all

if player:mem(0x122,FIELD_WORD) ~= 0 then
[stuff]
0x122 is the timer for the power-up/down animations, as I recall. I used this in my own (now scrapped) level and it seemed to work fine.
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: 0x122 is the timer for the power-up/down animations, as I recall. I used this in my own (now scrapped) level and it seemed to work fine.
That doesn't account for a lot of scenarios. This condition will be true when it is safe for movement to occur, and false otherwise:

Code: Select all

if(player:mem(0x122,FIELD_WORD) == 0 or player:mem(0x122,FIELD_WORD) == 7 or player:mem(0x122,FIELD_WORD) == 500) then
Image
Image
Image
Image
Image
User avatar
WestonSmith
Bunny
Posts: 188
Joined: 11 years ago

Re: Lua help thread

Post by WestonSmith »

Zyglrox Odyssey wrote: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.
Trying to use this code for a vertical autoscrol. Exchanged the word "left" for "bottom" and "right" for "top." The speed was turned negative as well.

The autoscroll itself seems to be working fine... but Demo dies instantly upon entering the Screen. I assume the code uses the player location to determine the screen's starting position, and the result is that Demo spawns at the absolute bottom of the autoscroll. So... is there a way around this? Has anyone else tried vertical autoscrolling yet?
Zygl
Posts: 616
Joined: 11 years ago

Re: Lua help thread

Post by Zygl »

WestonSmith wrote:Trying to use this code for a vertical autoscrol. Exchanged the word "left" for "bottom" and "right" for "top." The speed was turned negative as well.

The autoscroll itself seems to be working fine... but Demo dies instantly upon entering the Screen. I assume the code uses the player location to determine the screen's starting position, and the result is that Demo spawns at the absolute bottom of the autoscroll. So... is there a way around this? Has anyone else tried vertical autoscrolling yet?
Try replacing the 800 with a 600 and every player.x with player.y. If that doesn't work swap the signs on the screen.[direction] = player.x [+/-] [direction variable thing] lines. That should work, I think. If not I'll have to fiddle with it a bit myself probably because that'd be kinda weird and dumb.
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: Try replacing the 800 with a 600 and every player.x with player.y. If that doesn't work swap the signs on the screen.[direction] = player.x [+/-] [direction variable thing] lines. That should work, I think. If not I'll have to fiddle with it a bit myself probably because that'd be kinda weird and dumb.
Just worth adding for the benefit of those who don't know that the SMBX screen is 800x600 pixels. When you're getting screen coordinates, the right side of the screen is the left side + 800, while it's +600 for the vertical. Using player.y instead of player.x just means you're getting the vertical (y) coordinate of the player, rather than the horizontal (x) coordinate.
Image
Image
Image
Image
Image
User avatar
WestonSmith
Bunny
Posts: 188
Joined: 11 years ago

Re: Lua help thread

Post by WestonSmith »

Fixed my problem. I had already switched the "x" and "y" notations, but I was subtracting from the bottom and adding to the top. It needs to be the opposite to function.

Thanks for the help!
User avatar
snoruntpyro
cutest girl 2023
Posts: 884
Joined: 9 years ago
Pronouns: she/her
Contact:

Re: Lua help thread

Post by snoruntpyro »

I've finally gotten around to updating my level, but when I implement the code that Hoeloe posted earlier

Code: Select all

function onLoopSection2(asdf)
   if asdf == 1 then
      screen = player.sectionObj.boundary
      screen.left = screen.left + speed
      screen.right = screen.right + speed
      player.sectionObj.boundary = screen
   end
   if(player:mem(0x122,FIELD_WORD) == 0 or player:mem(0x122,FIELD_WORD) == 7 or player:mem(0x122,FIELD_WORD) == 500) then
	  speed = 1
	end
	else
	  speed = 0
	end
end
the game gives me this error

Image

and I immediately die upon entering the first autoscroll section. What am I doing wrong?
Image
Image
Image
Image
Image
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 »

Pyro wrote:the game gives me this error
Sounds to me like like 67 of your code has "Player:mem" when it should have "player:mem". Capitalization matters.
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

Rednaxela wrote:
Pyro wrote:the game gives me this error
Sounds to me like like 67 of your code has "Player:mem" when it should have "player:mem". Capitalization matters.
Also you've written code with this structure:

Code: Select all

if _ then _end else _ end
Which is incorrect and won't work. You don't put an "end" in the middle of an if-else block. It should be:

Code: Select all

if _then _ else _ end
Image
Image
Image
Image
Image
User avatar
WestonSmith
Bunny
Posts: 188
Joined: 11 years ago

Re: Lua help thread

Post by WestonSmith »

I know lua is full of magic, but is this scenario plausible:

-You use the SMW switch block in Level A
-Level B recognizes that the switch block was pressed in Level A, and the blocks swap from solid to unsolid (or vice versa)
-The player saves and quits the game, but the game itself still recognizes that the switch was pressed and its affect remains global

It's my understanding that Raocoins can be collected and stored as collected, so I'm wondering if this scenario is doable.

~Thanks
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

WestonSmith wrote:I know lua is full of magic, but is this scenario plausible:

-You use the SMW switch block in Level A
-Level B recognizes that the switch block was pressed in Level A, and the blocks swap from solid to unsolid (or vice versa)
-The player saves and quits the game, but the game itself still recognizes that the switch was pressed and its affect remains global

It's my understanding that Raocoins can be collected and stored as collected, so I'm wondering if this scenario is doable.

~Thanks
Yes. The Data class can be used to store information between levels, and can keep data after you close the game.
Image
Image
Image
Image
Image
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 »

Is there any way of overcoming the 20k block limit for a level?
:pal: Image :pal:

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

Dog In Da Grass wrote:Is there any way of overcoming the 20k block limit for a level?
It's actually a 16383 block limit beyond which it'll crash. Apparently the limit used to be smaller, and at one point Regedit tried to make it 20k but made the mistake leaving it as a 16-bit signed integer, limiting it to 16383 actually.

There's not much of a way to overcome that with Lua code easily no.

I've looked into having LunaDLL make modifications to the SMBX code to make it actually 20k instead of 16.3k
I've already done a similar-ish (though slightly different) thing for the warp limit after all (from 200 to 2000)
It's quite a bit of work though, due to how the limiting factors are strewn about. In other words, it might happen eventually, but don't count on it being any time soon.

A level with >16383 blocks is kind of not the best idea anyway though IMO. For people running slower computers the risk of running into slowdown does increase as you push such limits. Besides, one can make use of either re-skinned 4x4 blocks or reskinned resizable blocks to massively cut down on block count in most cases.
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 »

Rednaxela wrote:
Dog In Da Grass wrote:Is there any way of overcoming the 20k block limit for a level?
It's actually a 16383 block limit beyond which it'll crash. Apparently the limit used to be smaller, and at one point Regedit tried to make it 20k but made the mistake leaving it as a 16-bit signed integer, limiting it to 16383 actually.

There's not much of a way to overcome that with Lua code easily no.

I've looked into having LunaDLL make modifications to the SMBX code to make it actually 20k instead of 16.3k
I've already done a similar-ish (though slightly different) thing for the warp limit after all (from 200 to 2000)
It's quite a bit of work though, due to how the limiting factors are strewn about. In other words, it might happen eventually, but don't count on it being any time soon.

A level with >16383 blocks is kind of not the best idea anyway though IMO. For people running slower computers the risk of running into slowdown does increase as you push such limits. Besides, one can make use of either re-skinned 4x4 blocks or reskinned resizable blocks to massively cut down on block count in most cases.
Thanks for the heads up! I've got myself in over my head again! I got way too ambitious on the current level I've been working on, I might need to cut it backsince I'm already at around 13k blocks
:pal: Image :pal:

Image
Image
Image
KingTwelveSixteen
? Title Title Title Title ?
Posts: 30
Joined: 14 years ago
Location: USA

Re: Lua help thread

Post by KingTwelveSixteen »

Dog In Da Grass wrote:Is there any way of overcoming the 20k block limit for a level?
Replace large groups of blocks with custom re-sizable ones, enormous background objects that look like the inside of blocks, or make custom versions of the provided 4x4 blocks.

That usually works, and if you have too many blocks in your level even after filling the inside of each big block-area up with those than there is a bigger problem with your level than block limits, I tell you what.
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 »

yeah, I shoulda done them as background tiles to begin with! ... well there is an even lower limit on background tiles! x3
:pal: Image :pal:

Image
Image
Image
User avatar
SAJewers
ASMBXT Level Wrangler/A2XT Project Coordinator /AAT Level Designer
Posts: 4200
Joined: 11 years ago
Location: Nova Scotia

Re: Lua help thread

Post by SAJewers »

1) I'm trying to convert the following autocode into LUA:

Code: Select all

#-1
LoadImage,1,0xDC00FF,0,0,0,cellophane.png
PlaceSprite,1,1,0,0,0,0
I have this, but it doesn't work. What am I doing wrong?

Code: Select all

Cellophane = {}
Cellophane.img = {}

function Cellophane:onLoad()
	loadImage("//cellophane.png", self.img[1], 0xFFFFFF);
	placeSprite(1,self.img[1],0,0);
end
2) In trying to figure this out, the following code now crashes SMBX with the error "Invalid Player-Pointer"

Code: Select all

function onLoad()
  player.powerup = PLAYER_SMALL;
  player:mem(0xF0, FIELD_WORD, 4);
  player:mem(0x108, FIELD_WORD, 0);

end
ImageImageImageImageImage | Image | Image
Sharenite | RetroAchievements | NameMC | IGDB
User avatar
Hoeloe
A2XT person
Posts: 1016
Joined: 12 years ago
Pronouns: she/her
Location: Spaaace

Re: Lua help thread

Post by Hoeloe »

SAJewers wrote:1) I'm trying to convert the following autocode into LUA:

Code: Select all

#-1
LoadImage,1,0xDC00FF,0,0,0,cellophane.png
PlaceSprite,1,1,0,0,0,0
I have this, but it doesn't work. What am I doing wrong?

Code: Select all

Cellophane = {}
Cellophane.img = {}

function Cellophane:onLoad()
	loadImage("//cellophane.png", self.img[1], 0xFFFFFF);
	placeSprite(1,self.img[1],0,0);
end
2) In trying to figure this out, the following code now crashes SMBX with the error "Invalid Player-Pointer"

Code: Select all

function onLoad()
  player.powerup = PLAYER_SMALL;
  player:mem(0xF0, FIELD_WORD, 4);
  player:mem(0x108, FIELD_WORD, 0);

end
The first one is because you're not actually using the "onLoad" function, but a function called "onLoad" which is part of an object called "Cellophane". If you look at the MAGLX relays, you'll see that, while they use that structure, all of those events are called from a root function which is just the basic "onLoad".

The second is because the variable "player" isn't guaranteed to exist when the level is loaded. Fortunately, the player has a validity check, so if you just check for whether the player is valid at the start of the onLoad function, and "return" if not, then that should fix it.
Image
Image
Image
Image
Image
User avatar
SAJewers
ASMBXT Level Wrangler/A2XT Project Coordinator /AAT Level Designer
Posts: 4200
Joined: 11 years ago
Location: Nova Scotia

Re: Lua help thread

Post by SAJewers »

Thanks. Ended up getting it work with this:

Code: Select all

local overlay = Graphics.loadImage("//cellophane.png");
function onLoad()
	if (player.isValid) then
		player.powerup = PLAYER_SMALL;
		player:mem(0xF0, FIELD_WORD, 4);
		player:mem(0x108, FIELD_WORD, 0);
	end
	Graphics.placeSprite(1,overlay,0,0);
--	Cellophane:onLoad()

end

--Cellophane = {}
--Cellophane.img = {}

--function Cellophane:onLoad()
	
	
--end

do	
	function onLoop ()
		-- Check if the player has invincibility frames
		if (player:mem(0x128, FIELD_FLOAT) ~= 0) then
			-- If so, freeze time
			mem(0xB2C8B4, FIELD_WORD, -1)
	  	else
			-- Otherwise, unfreeze time
		mem(0xB2C8B4, FIELD_WORD, 0)
		end
	end

end

ImageImageImageImageImage | Image | Image
Sharenite | RetroAchievements | NameMC | IGDB
User avatar
7NameSam
hey
Posts: 248
Joined: 9 years ago
Pronouns: they/them

Re: Lua help thread

Post by 7NameSam »

so, I wanna make a level with a potentially awful gimmick
7NameSam wrote:I want to make it so you can't move if you are standing on the ground, How do I do that?
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 »

7NameSam wrote:so, I wanna make a level with a potentially awful gimmick
7NameSam wrote:I want to make it so you can't move if you are standing on the ground, How do I do that?
So you mean something like

Code: Select all

function onLoop()
	-- If on the ground (0x146), or sloped ground (0x48)...
	if (player:mem(0x146, FIELD_WORD) ~= 0) or (player:mem(0x48, FIELD_WORD) ~= 0) then
		-- Disable horizontal and ducking (0x04)
		player:mem(0x04, FIELD_WORD, -1)
	end
end
?
User avatar
7NameSam
hey
Posts: 248
Joined: 9 years ago
Pronouns: they/them

Re: Lua help thread

Post by 7NameSam »

Rednaxela wrote:
7NameSam wrote:so, I wanna make a level with a potentially awful gimmick
7NameSam wrote:I want to make it so you can't move if you are standing on the ground, How do I do that?
So you mean something like

Code: Select all

function onLoop()
	-- If on the ground (0x146), or sloped ground (0x48)...
	if (player:mem(0x146, FIELD_WORD) ~= 0) or (player:mem(0x48, FIELD_WORD) ~= 0) then
		-- Disable horizontal and ducking (0x04)
		player:mem(0x04, FIELD_WORD, -1)
	end
end
?
yea, but I want the player to still slide around and stuff
This code makes it feel like the ground is covered in glue
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 »

7NameSam wrote:yea, but I want the player to still slide around and stuff
This code makes it feel like the ground is covered in glue
Try this one out then:

Code: Select all

-- Run during onInputUpdate, not onLoop
function onInputUpdate()
	-- If on the ground, or a slope...
	if (player:mem(0x146, FIELD_WORD) ~= 0) or (player:mem(0x48, FIELD_WORD) ~= 0) then
		-- Disable left/right controls
		player:mem(0xF6, FIELD_WORD, 0)
		player:mem(0xF8, FIELD_WORD, 0)
	end
end
Note, this only works with LunaLUA versions >= 0.7, as that's what added onInputUpdate
Post Reply