Need help with lua? - LunaLua General Help

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
mariobrigade2018
Spike
Spike
Posts: 275
Joined: Wed May 24, 2023 7:00 pm
Flair: Normie in coding who dreams of making a Mario game
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby mariobrigade2018 » Tue Dec 05, 2023 3:18 pm

deice wrote:
Sun Dec 03, 2023 7:24 am
mariobrigade2018 wrote:
Sat Dec 02, 2023 9:51 pm
Does any know what this error code is?
you've most likely got a malformed npc config txt file. check any txt files in your level/episode folder for typos or other errors
Yep. npc-262.txt was corupted or something. Thanks!

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: Need help with lua? - LunaLua General Help

Postby LunarCatUSA » Tue Dec 26, 2023 4:35 pm

Question, I'm trying to give an enemy a two-frame idle animation. The way it works is that the enemy will be standing still in idle in which 2 frames are active. (For both left and right directions) Then when moving, their animation frames become another two-frame idle animation. (Again, for both left and right directions)
The way the 8 frames are set on the png is:
<Move1
<Move2
<Idle1
<Idle2
Move1>
Move2>
Idle1>
Idle2>
I have the frames number set to 4 and framestyle set to 1. I don't if this is how I should be doing it.
See I have the code going like (Status 0 is idle, Status 1 is moving):
Frame = (math.floor(lunatime.tick() * 0.125) % 2)
if status == 0 and v.direction == -1 then
v.animationFrame = 3 + Frame
elseif status == 0 and v.direction ~= -1 then
v.animationFrame = 7 + Frame
end
if status ~= 0 and v.direction == -1 then
v.animationFrame = 1 + Frame
elseif status ~= 0 and v.direction == 1 then
v.animationFrame = 5 + Frame
end
The problem is the moving status work just fine alternating between 1,2 for left and 5,6 for right. But not for the idle state as it alters between frame 3 and 7, does anyone know the issue or have an easier solution?

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Wed Jan 17, 2024 10:17 am

Is there a legit way to force move NPCs like the Mushroom, 1up, starman etc. ALWAYS to the right direction when they come out of a box/block (of course they are supposed to switch direction if bumped from below or if they reach a wall etc.). The SMB3 behaviour is actually a little bit problematic if you want to create levels in SMB1 style (without turning back etc.).
I found this here
https://docs.codehaus.moe/#/constants/direction

I checked the SML2 blocks and I'm not sure whether this can be integrated in any way.
local function SpawnNPC(v)
and NPC.spawn and "spawnDirection. The NPC's facing direction when spawning."
under Instance Fields.
https://docs.codehaus.moe/#/reference/npc
So, even if it would be something simple, but without an existing lua file for the old npc, how to replace existing behaviour?
Does somebody know?

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Thu Jan 18, 2024 7:03 am

Just_Thomas wrote:
Wed Jan 17, 2024 10:17 am
Is there a legit way to force move NPCs like the Mushroom, 1up, starman etc. ALWAYS to the right direction when they come out of a box/block (of course they are supposed to switch direction if bumped from below or if they reach a wall etc.)
putting something like this in your onTick should work:

Code: Select all

for _, v in NPC.iterate() do
	if(v:mem(0x138, FIELD_WORD) == 1 and v:mem(0x13C, FIELD_DFLOAT) == 0) then
		v.direction = DIR_RIGHT
	end
end
what this does is it triggers on the exact tick that an npc starts coming out of a block and resets its direction manually
if you want to use this for only certain npcs you can use a filter with the iterate() call

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Thu Jan 18, 2024 8:12 am

deice wrote:
Thu Jan 18, 2024 7:03 am
Just_Thomas wrote:
Wed Jan 17, 2024 10:17 am
Is there a legit way to force move NPCs like the Mushroom, 1up, starman etc. ALWAYS to the right direction when they come out of a box/block (of course they are supposed to switch direction if bumped from below or if they reach a wall etc.)
putting something like this in your onTick should work:

Code: Select all

for _, v in NPC.iterate() do
	if(v:mem(0x138, FIELD_WORD) == 1 and v:mem(0x13C, FIELD_DFLOAT) == 0) then
		v.direction = DIR_RIGHT
	end
end
what this does is it triggers on the exact tick that an npc starts coming out of a block and resets its direction manually
if you want to use this for only certain npcs you can use a filter with the iterate() call
Works like a charm. Thanks a lot. Once again not something I would have been able to figure out.
But I do actually hope something like that can be selected in an smbx update in the future (maybe the world options).

vee_Sleepy
Fighter Fly
Fighter Fly
Posts: 40
Joined: Tue Nov 29, 2022 7:15 pm
Flair: oh worm?
Pronouns: they/it

Re: Need help with lua? - LunaLua General Help

Postby vee_Sleepy » Sun Feb 25, 2024 8:20 pm

how can I edit the player physics (speed, gravity, whatever) to simulate wind in a level? I can't seem to find any .lua script for actual wind and i'm bad at coding

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Mon Feb 26, 2024 2:32 am

vee_Sleepy wrote:
Sun Feb 25, 2024 8:20 pm
how can I edit the player physics (speed, gravity, whatever) to simulate wind in a level? I can't seem to find any .lua script for actual wind and i'm bad at coding
viewtopic.php?t=27266
...\Extension Pack 2.1\SMB1\Wind

This pack actually has the Lost Levels Wind. The class has a manageable number of lines of code. Perhaps you can find useful and helpful stuff there.

Apart from that I can suggest you this here as well:
viewtopic.php?t=28422
...\The Ultimate SML1 + 2 NPC Pack\Super Mario Land 2\Fields
These fields are designed to emulate the various forms of gravity changing elements of some of the levels in Super Mario Land 2.
These include Sap, which makes the player stall briefly while being able to swim,
Space, which lowers the player's Y-Speed (also with the option to jump in mid-air),
and Zero-G, which causes a player to rise indefinately if they are to hold the jump button.

All of these options are able to be changed via extra-settings to suit your liking as well.

Block made by 9thCore

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Fri Mar 15, 2024 1:02 pm

I could not find anything in the warp class of the wiki.
Is it possible to set up if player exits from warp X (for example warp exit 8, as a door exit) then ALWAYS face right?

Code: Select all

player:mem(0x106, FIELD_WORD, 1)
https://docs.codehaus.moe/#/reference/warp
I miss something like a "warp ID" or anything like that?!
This is really just a third world problem and nothing that would make a level impossible or unplayable, but it would still be better for the end result.

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Sat Mar 16, 2024 7:45 am

Just_Thomas wrote:
Fri Mar 15, 2024 1:02 pm
I could not find anything in the warp class of the wiki.
Is it possible to set up if player exits from warp X (for example warp exit 8, as a door exit) then ALWAYS face right?
while i don't think warps expose their own internal index, you should be able to uniquely determine a warp by its entrance and exit coordinates. and since i'm fairly certain Warp.get() will always return the warps in the correct order, something like this:

Code: Select all

local targetWarpIndex = 8 -- the warp in question

function onWarp(w, p)
	local checkWarp = Warp.get()[targetWarpIndex]
	if(w.entranceX == checkWarp.entranceX and w.entranceY == checkWarp.entranceY and w.exitX == checkWarp.exitX and w.exitY == checkWarp.exitY) then
		p.direction = DIR_RIGHT
	end
end
might work.

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Sat Mar 16, 2024 9:07 am

deice wrote:
Sat Mar 16, 2024 7:45 am
Just_Thomas wrote:
Fri Mar 15, 2024 1:02 pm
I could not find anything in the warp class of the wiki.
Is it possible to set up if player exits from warp X (for example warp exit 8, as a door exit) then ALWAYS face right?
while i don't think warps expose their own internal index, you should be able to uniquely determine a warp by its entrance and exit coordinates. and since i'm fairly certain Warp.get() will always return the warps in the correct order, something like this:

Code: Select all

local targetWarpIndex = 8 -- the warp in question

function onWarp(w, p)
	local checkWarp = Warp.get()[targetWarpIndex]
	if(w.entranceX == checkWarp.entranceX and w.entranceY == checkWarp.entranceY and w.exitX == checkWarp.exitX and w.exitY == checkWarp.exitY) then
		p.direction = DIR_RIGHT
	end
end
might work.
It DOES work, can I add more warp exits to it, like that?
BTW: I admit, I would probably never have thought of your working solution.

local targetWarpIndex --do I have to leave this empty now?

function onWarp(w, p)
local checkWarp = Warp.get()(targetWarpIndex[1], 2, 7, 8) --I looked for tables and thought [1] = numbers of the warp and 2, 7 and 8 are the checkpoints where the player has to look right
...

Or is this a better start?
local targetWarpIndex = {}

targetWarpIndex = 2
targetWarpIndex = 7
targetWarpIndex = 8


So I'm just showing you this so that you can see what I think I understand (which probably won't be much).
You can probably see that I'm trying to fight my way through like a blind chicken.

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Sun Mar 17, 2024 10:31 am

Just_Thomas wrote:
Sat Mar 16, 2024 9:07 am
can I add more warp exits to it, like that?
neither of your proposed solutions would work. the first one indexes an uninitialized value, and the second one simply reassigns the index (which means only the last number will be checked)
if you want it to work with multiple warps, the code would probably go something like this:

Code: Select all

local targetWarpIndices = { 2, 7, 8 } -- the warps in question

function onWarp(w, p)
	local warps = Warp.get()
	for _, targetWarpIndex in ipairs(targetWarpIndices) do
		local checkWarp = warps[targetWarpIndex]
		if(w.entranceX == checkWarp.entranceX and w.entranceY == checkWarp.entranceY and w.exitX == checkWarp.exitX and w.exitY == checkWarp.exitY) then
			p.direction = DIR_RIGHT
			break
		end
	end
end

Marioman2007
Lakitu
Lakitu
Posts: 466
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Sun Mar 17, 2024 11:19 am

deice wrote:
Sun Mar 17, 2024 10:31 am

Code: Select all

local targetWarpIndices = { 2, 7, 8 } -- the warps in question

function onWarp(w, p)
	local warps = Warp.get()
	for _, targetWarpIndex in ipairs(targetWarpIndices) do
		local checkWarp = warps[targetWarpIndex]
		if(w.entranceX == checkWarp.entranceX and w.entranceY == checkWarp.entranceY and w.exitX == checkWarp.exitX and w.exitY == checkWarp.exitY) then
			p.direction = DIR_RIGHT
			break
		end
	end
end

Cmiiw, but can't we just compare the two warp objects?

Code: Select all

local targetWarpIndices = {2, 7, 8}

function onWarp(w, p)
	local warps = Warp.get()
	
	for _, targetWarpIndex in ipairs(targetWarpIndices) do		
		if w == warps[targetWarpIndex] then
			p.direction = DIR_RIGHT
			break
		end
	end
end

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Sun Mar 17, 2024 2:15 pm

Marioman2007 wrote:
Sun Mar 17, 2024 11:19 am
deice wrote:
Sun Mar 17, 2024 10:31 am
Just_Thomas wrote:
Sat Mar 16, 2024 9:07 am
Gentleman, please don't argue. All joking aside. I really appreciate the fact that you have given me - how else can I put it - the solution to the problem.

I actually tried to understand what you did.
So if I understand correctly sort ipairs() or the variable in there - here the table with the multiple values, that this is packed into a new variable, which can then compare the values specifically or not?
Then I did read that "_" is often used as a placeholder which do not get used anymore after that or so. However I do not understand "for _," as I only learned that i=1 stuff many years ago.
I can only guess it simply has to be there and is not really needed.

Even though I managed to do one or two things in programming with a lot of effort, it never became anything serious and did remain a weak point unfortunately.

deice
Volcano Lotus
Volcano Lotus
Posts: 549
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Sun Mar 17, 2024 4:12 pm

Marioman2007 wrote:
Sun Mar 17, 2024 11:19 am
Cmiiw, but can't we just compare the two warp objects?
intuitively, yes (warp.get using the same object pool as the warp passed to the event makes sense), but i'm not sure if it's guaranteed to work in certain edge cases (e.g. two-way warps) so someone with more insight into the api itself would probably have to chime in
Just_Thomas wrote: So if I understand correctly sort ipairs() or the variable in there - here the table with the multiple values, that this is packed into a new variable, which can then compare the values specifically or not?
Then I did read that "_" is often used as a placeholder which do not get used anymore after that or so. However I do not understand "for _," as I only learned that i=1 stuff many years ago.
ipairs (and other similar functions) return an iterator that lets you loop through each entry of a table with a key assigned. ipairs specifically generates numeric keys, but since we don't need those we use a discard ("_") in place of where the name of the key would usually go

Amine1542
Cheep-Cheep
Cheep-Cheep
Posts: 11
Joined: Thu Nov 02, 2017 8:08 pm

Re: Need help with lua? - LunaLua General Help

Postby Amine1542 » Mon Mar 18, 2024 1:07 pm

Yo guys!
I hope everyone is doing well? I was wondering... Do you know how to extend the duration of the invincibility of the star and its music? I think I know it's in the "starman" file but nothing to do, I can't modify :/

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Mon Mar 18, 2024 1:52 pm

Amine1542 wrote:
Mon Mar 18, 2024 1:07 pm
Yo guys!
I hope everyone is doing well? I was wondering... Do you know how to extend the duration of the invincibility of the star and its music? I think I know it's in the "starman" file but nothing to do, I can't modify :/
Copy npc-293.lua from "\data\scripts\npcs" (SMW starman version) into your project / levelfolder, open it:
duration = 13
This is the value you are looking for.
To change the music, the new music (in your project / levelfolder) file has to be named "starman".

Amine1542
Cheep-Cheep
Cheep-Cheep
Posts: 11
Joined: Thu Nov 02, 2017 8:08 pm

Re: Need help with lua? - LunaLua General Help

Postby Amine1542 » Mon Mar 18, 2024 3:46 pm

Just_Thomas wrote:
Mon Mar 18, 2024 1:52 pm
Amine1542 wrote:
Mon Mar 18, 2024 1:07 pm
Yo guys!
I hope everyone is doing well? I was wondering... Do you know how to extend the duration of the invincibility of the star and its music? I think I know it's in the "starman" file but nothing to do, I can't modify :/
Copy npc-293.lua from "\data\scripts\npcs" (SMW starman version) into your project / levelfolder, open it:
duration = 13
This is the value you are looking for.
To change the music, the new music (in your project / levelfolder) file has to be named "starman".
Fast, simple and effective, thank you very much!!!! :D :D

mariobrigade2018
Spike
Spike
Posts: 275
Joined: Wed May 24, 2023 7:00 pm
Flair: Normie in coding who dreams of making a Mario game
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby mariobrigade2018 » Mon Mar 18, 2024 4:06 pm

Just_Thomas wrote:
Mon Mar 18, 2024 1:52 pm
Amine1542 wrote:
Mon Mar 18, 2024 1:07 pm
Yo guys!
I hope everyone is doing well? I was wondering... Do you know how to extend the duration of the invincibility of the star and its music? I think I know it's in the "starman" file but nothing to do, I can't modify :/
Copy npc-293.lua from "\data\scripts\npcs" (SMW starman version) into your project / levelfolder, open it:
duration = 13
This is the value you are looking for.
To change the music, the new music (in your project / levelfolder) file has to be named "starman".
You could also put duration=13 in a created npc-293.txt file.

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Tue Mar 19, 2024 3:26 am

mariobrigade2018 wrote:
Mon Mar 18, 2024 4:06 pm
Just_Thomas wrote:
Mon Mar 18, 2024 1:52 pm
Amine1542 wrote:
Mon Mar 18, 2024 1:07 pm
Yo guys!
I hope everyone is doing well? I was wondering... Do you know how to extend the duration of the invincibility of the star and its music? I think I know it's in the "starman" file but nothing to do, I can't modify :/
Copy npc-293.lua from "\data\scripts\npcs" (SMW starman version) into your project / levelfolder, open it:
duration = 13
This is the value you are looking for.
To change the music, the new music (in your project / levelfolder) file has to be named "starman".
You could also put duration=13 in a created npc-293.txt file.
You are right, this has been fixed with beta 5. In beta 4 I tried it back then (and now for comparison) and there it did not work.

Just_Thomas
Snifit
Snifit
Posts: 234
Joined: Sat Dec 16, 2023 3:32 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Just_Thomas » Sun Mar 24, 2024 6:44 am

I simply have great difficulty understanding codehaus or rather the syntax (as a non-coder that is). It almost never clicks for me before I at least see a practical example. I don't think I need to explain what I want to do here. This is like officialese, for real. There are a few small examples here and there and I would really like to see more of them.

Code: Select all

function onSectionChange()
	if (sectionIdx == 0) then
	Defines.earthquake = 20
	Audio.playSFX(37)
	end
end

Code: Select all

function onSectionChange(sectionIdx, playerIdx)
	if (sectionIdx == 0 and playerIdx ==1) then
	Defines.earthquake = 20
	Audio.playSFX(37)
	end
end
Does not work.

Code: Select all

function onSectionChange()
	Defines.earthquake = 20
	Audio.playSFX(37)
end
Does work, but then for every section change, which I do not want. I owe it largely to Emral and deice that I still understand anything at all.
For a new level (BTW: I actually ... finished one 100 %, hardly to believe but yes, this is for an other one) I actually need something to check, if a player enters a section and whatever happens then should only happen, once the player enters the section (can happen again, if (s)he re-enters later).
Yes I could very likely with something like
if player.x == blabla and player.section == blabla then
but at the end I would need a variable then which I could reset so it would work again if player enters the section again. onSectionChange seems to look more promising for what I want to do.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 2 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari