Need help with lua? - LunaLua General Help

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9708
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Tue Aug 30, 2022 12:10 pm

Marioman2007 wrote:
Tue Aug 30, 2022 12:02 pm
ohh, glad to know now lol
I thought it was just a function that sets the position for one frame
Nah, it's like attaching a magnet to your fridge. if you move the fridge, the magnet comes with.

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 Aug 30, 2022 1:24 pm

Thanks, I've been looking for the light guide or light documentation for a while now. Didn't think it'd be located in LunaLua section.

So, if one NPC has a variable in its data, is it possible to pass that variable onto a new NPC that this NPC is transforming into? Or is it better to define the NPC as a variable in the level script for stuff like that?

Added in 2 hours 10 minutes 30 seconds:
Hmmm... In regards to my NPC actually, I feel like I should private message an expert on the matter as it is a bit too complex to ask on a LunaLua Help board.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9708
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Tue Aug 30, 2022 8:40 pm

keeping a variable from the data table across transformation is simple.

local var = npc.data.myVariable
npc:transform(newID)
npc.data.myVariable = var

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 Aug 30, 2022 9:48 pm

Alright, I'll try that then.

I only ask because I'm doing something rather unorthodox. Basically, along with giving my NPC the ability to travel through door warps, I also wanted to give it an animation of it entering the door warp. I made a separate NPC that's an animation of it going into the door so whenever it decides to warp in a door, I just fix the NPC to the spot of the warp, completes its animation to the final time of its final frame, and it transforms back into the original NPC at the other side and continues its movement until it decides to warp through a door again.

I'm sure most would have probably made an effect or simply add some sprites onto the spritesheet but I'm not exactly skilled enough at LunaLua coding yet. Not to mention, it's a 96x128 monster becoming a harmless 32x64 animation of a monster entering a door and then back to the monster again.

So yeah, that's why I was asking but I think I have what I need to make do with.

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 » Wed Aug 31, 2022 10:29 am

I can use the OnStart() function for NPC codes, right?

Or should I just use v.spawnAI1 for variables upon spawning?

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9708
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Wed Aug 31, 2022 12:17 pm

LunarCatUSA wrote:
Wed Aug 31, 2022 10:29 am
I can use the OnStart() function for NPC codes, right?

Or should I just use v.spawnAI1 for variables upon spawning?
onStart only runs when the level starts.

spawnAi1 is merely what ai1 resets to when respawning.

Check out the npc-n.lua template if you want to reset custom variables. In its onTickNPC handler, it has a data.initialized variable which, if it is false, causes an if-statement of variable initialization to run. The NPC's despawning causes the initialized variable to return back to false, triggering re-initialization.
For convenience, the code snippit referenced:

Code: Select all

function sampleNPC.onTickNPC(v)
	--Don't act during time freeze
	if Defines.levelFreeze then return end
	
	local data = v.data
	
	--If despawned
	if v.despawnTimer <= 0 then
		--Reset our properties, if necessary
		data.initialized = false
		return
	end

	--Initialize
	if not data.initialized then
		--Initialize necessary data.
		data.initialized = true
	end
If you're talking about "NPC Codes" in the sense of "NPC Config" (they used to be referred to as NPC codes before actual scripting), you can edit those from anywhere via, for example, NPC.config[1].gfxwidth = 2

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 » Thu Sep 01, 2022 1:19 pm

Okay so here's the NPC code:

Code: Select all

	
	--Initialize
	if not data.initialized then
		--Initialize necessary data.
		data.initialized = true
		searching = false
		deciding = false
		harddeciding = false
	end
	
	v.despawnTimer = 10
	WarpsAroundHer = Warp.getIntersectingEntrance(v.x-16,v.y,v.x+v.width+16,v.y+v.height)
	WarpsInLevel = Warp.get()
	-- Identify all warps --
	-- Warps it already passed through --
	State = {"patrol","transition","search", "chase"}
		if v.isValid == true and searching == false and (deciding == false and harddeciding == false) then
			Actpattern = State[1]
		end
	-- Patrol, this enemy is searching rooms for the player, it is in the same section as the player --
		if Actpattern == State[1] then
			if v.direction == -1 then
				v.speedX = -1
			elseif v.direction == 1 then
				v.speedX = 1
			end
			for k, w in ipairs(WarpsAroundHer) do
				if v.direction == -1 and (w.entranceX == math.ceil(v.x) and w.entranceY+w.entranceHeight == math.ceil(v.y+v.height)) and (w.entranceX ~= OldWarpX and w.entranceY ~= OldWarpY) and w.exitSection == v.section then
					deciding = true
				elseif v.direction == 1 and (w.entranceX+w.entranceWidth == math.ceil(v.x+v.width) and w.entranceY+w.entranceHeight == math.ceil(v.y+v.height)) and (w.entranceX ~= OldWarpX and w.entranceY ~= OldWarpY) and w.exitSection == v.section then
					deciding = true
				elseif v.direction == -1 and (w.entranceX == math.ceil(v.x) and w.entranceY+w.entranceHeight == math.ceil(v.y+v.height)) and (w.entranceX == OldWarpX and w.entranceY == OldWarpY) and w.exitSection == v.section then
					harddeciding = true
				elseif v.direction == 1 and (w.entranceX+w.entranceWidth == math.ceil(v.x+v.width) and w.entranceY+w.entranceHeight == math.ceil(v.y+v.height)) and (w.entranceX == OldWarpX and w.entranceY == OldWarpY) and w.exitSection == v.section then
					harddeciding = true
				else
					deciding = false
					harddeciding = false
				end
				if deciding == true then
					NextRoomDec = RNG.randomInt(0,100)
					if NextRoomDec >= 25 then
						ThisDoorX = w.entranceX
						ThisDoorY = w.entranceY-32
						OldWarpX = w.exitX
						OldWarpY = w.exitY
						Actpattern = State[2]
					elseif NextRoomDec < 25 then
						deciding = false
						NextRoomDec = nil
					end
				elseif harddeciding == true then
					NextRoomDec = RNG.randomInt(0,100)
					if NextRoomDec < 25 then
						Actpattern = State[2]
						ThisDoorX = w.entranceX
						ThisDoorY = w.entranceY-32
						OldWarpX = w.exitX
						OldWarpY = w.exitY
						Actpattern = State[2]
					elseif NextRoomDec >= 25 then
						harddeciding = false
						NextRoomDec = nil
					end
				end
			end
		end
	-- Transition, this enemy has found a warp to go through and has decided to go through it, it stops moving --
		if Actpattern == State[2] then
			v.speedX = 0
			v.x = OldWarpX
			v.y = OldWarpY-96
			ThisDoorX = nil
			ThisDoorY = nil
			deciding = false
			harddeciding = false
			NextRoomDec = nil
			Actpattern = State[1]
		end
	-- Search, this enemy has suspected the presence of the player, it has NOT seen the player --
	
	-- Chase, this enemy is pursuing the player, it is has seen the player --
	
	--Execute main AI. This template just jumps when it touches the ground.
	if v.collidesBlockLeft == true or v.collidesBlockRight == true then
		v.direction = v.direction * -1
	end
end
The code seems to work for like the first door warp it encounters. After that, sometimes it goes into a door again, sometimes it doesn't. And the section has all two-way warps btw so any exit that it got when it decided to go through, is the entrance on the other side, so this should make it so that it has a harder time deciding to go through the door because it just came out of it. I'm not sure what's the issue.

Added in 1 hour 11 minutes 49 seconds:
Well, looking at my code, my check for the oldwarps needs an OR as opposed to an AND, otherwise it avoids ANY door warp on the same level.

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 » Fri Sep 02, 2022 1:56 pm

Is there any way I can make an NPC invisible for a short period of time? Would it be possible to set its animation frame to a nonexistent value or should I just make a blank space on its spritesheet or another way?

Added in 7 hours 51 minutes 43 seconds:
I've tried v:mem(0x138, FIELD_WORD, 8) but that isn't making it invisible.

Added in 1 hour 40 minutes 7 seconds:
Ah, alright, I got it working. Set x=0 and y=0 for a short time and then spawn it at the warp exit when the effects finish.

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 » Mon Sep 05, 2022 10:47 am

How do I make it so a collider Box faces one area to the other?

local LoS = Colliders.Box(v.x+48+416*v.direction,v.y,416,v.height)

Problem is that the way the width and height works has it always growing out to the right. So the box appears directly in front of my NPC when it faces left, but when it goes right, it goes the distance out just fine, but then the box expands towards the x+ direction and not back to the NPC itself.

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

Re: Need help with lua? - LunaLua General Help

Postby deice » Mon Sep 05, 2022 1:04 pm

LunarCatUSA wrote:
Mon Sep 05, 2022 10:47 am
How do I make it so a collider Box faces one area to the other?

local LoS = Colliders.Box(v.x+48+416*v.direction,v.y,416,v.height)

Problem is that the way the width and height works has it always growing out to the right. So the box appears directly in front of my NPC when it faces left, but when it goes right, it goes the distance out just fine, but then the box expands towards the x+ direction and not back to the NPC itself.
i believe you're looking for a solution to the wrong problem here - all you need to do is change your math up a bit.

Code: Select all

local xOffset = -416
if(v.direction == DIR_RIGHT) then xOffset = 0 end

local LoS = Colliders.Box(v.x+48+xOffset,v.y,416,v.height)

KlumTheHero
Koopa
Koopa
Posts: 19
Joined: Wed Jul 05, 2017 9:35 am
Contact:

Re: Need help with lua? - LunaLua General Help

Postby KlumTheHero » Thu Sep 08, 2022 10:02 am

How do I make Lakitu-like NPC and how do I make NPC that always rewards items once it's defeated using lua? Because I just using npc-45 to make Souflee from Kid Icarus (lakitu didn't throw spiny eggs if it's modified).

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 » Sat Sep 10, 2022 12:57 am

So my episode file has a boolean value called playerhiding.

How would I make an NPC check it using its lua file? Because I want to make it so the enemy can approach the player's hiding spot (plaerhiding = false) and pull them out of it to deal damage.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9708
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat Sep 10, 2022 1:45 am

LunarCatUSA wrote:
Sat Sep 10, 2022 12:57 am
So my episode file has a boolean value called playerhiding.

How would I make an NPC check it using its lua file? Because I want to make it so the enemy can approach the player's hiding spot (plaerhiding = false) and pull them out of it to deal damage.
The general workflow for this is to make the code that handles player hiding into a library which both the episode and the npc script can load/interact with.
https://docs.codehaus.moe/#/concepts/li ... -libraries

DRACalgar Law
Hoopster
Hoopster
Posts: 112
Joined: Sun Oct 16, 2022 3:59 pm
Flair: King of the Boss Makers
Pronouns: he/him
Contact:

Re: Need help with lua? - LunaLua General Help

Postby DRACalgar Law » Sat Oct 22, 2022 1:05 am

How do I make a projectile npc disappear in quick seconds after it has been spawned?

KnownStranger
Monty Mole
Monty Mole
Posts: 147
Joined: Fri Jun 18, 2021 4:36 pm

Re: Need help with lua? - LunaLua General Help

Postby KnownStranger » Wed Nov 02, 2022 10:50 pm

I'm thinking of doing an episode that will use a smbx2 character (no Mario, Luigi, Peach, Toad or Link), but there are not settings to use that character. How do I force the game to use the character throughout the episode?

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

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Thu Nov 03, 2022 1:00 am

Twisted4932 wrote:
Wed Nov 02, 2022 10:50 pm
I'm thinking of doing an episode that will use a smbx2 character (no Mario, Luigi, Peach, Toad or Link), but there are not settings to use that character. How do I force the game to use the character throughout the episode?

you can put this code in luna.lua and map.lua

Code: Select all

function onStart()
    player.character = CHARACTER_#
end
replace CHARACTER_# with your desired character's constant.
https://docs.codehaus.moe/#/constants/characters

KnownStranger
Monty Mole
Monty Mole
Posts: 147
Joined: Fri Jun 18, 2021 4:36 pm

Re: Need help with lua? - LunaLua General Help

Postby KnownStranger » Thu Nov 03, 2022 11:20 am

Marioman2007 wrote:
you can put this code in luna.lua and map.lua

Code: Select all

function onStart()
    player.character = CHARACTER_#
end
replace CHARACTER_# with your desired character's constant.
https://docs.codehaus.moe/#/constants/characters
Thank you!

MarioChallengerX2
Bit
Bit
Posts: 77
Joined: Sat Dec 31, 2022 4:34 pm
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby MarioChallengerX2 » Mon Jan 30, 2023 1:24 pm

How do I make a checkpoint not give you any powerup when you collect it?

Torterra18
Koopa
Koopa
Posts: 19
Joined: Fri Feb 03, 2023 9:43 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Torterra18 » Fri Feb 17, 2023 4:16 pm

Umm, I'm kinda new to this lua thing, and please I need someone to answer me, how do I make a loot table for the Box NPC? The documentation doesn't talk about that...

ditditdit
Dolphin
Dolphin
Posts: 92
Joined: Sun Jan 02, 2022 4:25 pm
Flair: e
Pronouns: she/they
Contact:

Re: Need help with lua? - LunaLua General Help

Postby ditditdit » Mon Mar 27, 2023 1:08 am

noobiest question ever: how do i trigger an event with lua?


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari