Page 1 of 1

Custom NPC Boss Exit

Posted: Mon Nov 08, 2021 9:47 pm
by Arco
I am working on a concept level in which ends with a boss fight with a Big Boo. I am brand new to LunaLua, heck, even new to Lua, but I whipped up the following code to try to summon a "? Ball" NPC when my Big Boo boss dies;

Code: Select all

boo = 1

function onLoadSection1()
	boo = NPC.get(44, 1)
end

function onTick()
	while( boo == 1 )
	do
		boo = NPC.get(44, 1)
		
		if( boo == 0 )
		then
			NPC.spawn(16, -155232, 160256, 1)
			
			break
		else
			boo = NPC.get(44, 1)
		end
	end
end
I tried to use the NPC.get function to set variable 'boo' to the amount of big boos exist, which is one, and when that one dies, I have the IF statement summon the ? Ball with NPC.spawn. I am in section 2. Could someone please help me, currently there is no output, no errors and no results.

Re: Custom NPC Boss Exit

Posted: Tue Nov 09, 2021 8:43 am
by DrMekar
Not gonna be nitpicky, but why do you use Luna Lua for this? You can just do this with SMBX Events.

Re: Custom NPC Boss Exit

Posted: Tue Nov 09, 2021 10:10 am
by deice
Arco wrote:
Mon Nov 08, 2021 9:47 pm
I am working on a concept level in which ends with a boss fight with a Big Boo. I am brand new to LunaLua, heck, even new to Lua, but I whipped up the following code to try to summon a "? Ball" NPC when my Big Boo boss dies;

Code: Select all

boo = 1

function onLoadSection1()
	boo = NPC.get(44, 1)
end

function onTick()
	while( boo == 1 )
	do
		boo = NPC.get(44, 1)
		
		if( boo == 0 )
		then
			NPC.spawn(16, -155232, 160256, 1)
			
			break
		else
			boo = NPC.get(44, 1)
		end
	end
end
I tried to use the NPC.get function to set variable 'boo' to the amount of big boos exist, which is one, and when that one dies, I have the IF statement summon the ? Ball with NPC.spawn. I am in section 2. Could someone please help me, currently there is no output, no errors and no results.
NPC.get() returns a table of NPCs. trying to compare that with a number will always yield false, and thus the loop will never execute. if you want to try checking whether or not an NPC is valid, do something like

Code: Select all

while(boo[1] ~= nil)
instead of

Code: Select all

while(boo == 1)
and vice-versa (of course, this is assuming there can only be one big boo present at one time in the section)

though strictly speaking, i think that you'd want to refactor the code in general because if the boo isn't immediately dead when the loop begins to execute, it will never reach it's exit condition and will lock your game up.

Re: Custom NPC Boss Exit

Posted: Wed Nov 10, 2021 2:25 am
by Arco
deice wrote:
Tue Nov 09, 2021 10:10 am
Arco wrote:
Mon Nov 08, 2021 9:47 pm
I am working on a concept level in which ends with a boss fight with a Big Boo. I am brand new to LunaLua, heck, even new to Lua, but I whipped up the following code to try to summon a "? Ball" NPC when my Big Boo boss dies;

Code: Select all

boo = 1

function onLoadSection1()
	boo = NPC.get(44, 1)
end

function onTick()
	while( boo == 1 )
	do
		boo = NPC.get(44, 1)
		
		if( boo == 0 )
		then
			NPC.spawn(16, -155232, 160256, 1)
			
			break
		else
			boo = NPC.get(44, 1)
		end
	end
end
I tried to use the NPC.get function to set variable 'boo' to the amount of big boos exist, which is one, and when that one dies, I have the IF statement summon the ? Ball with NPC.spawn. I am in section 2. Could someone please help me, currently there is no output, no errors and no results.
NPC.get() returns a table of NPCs. trying to compare that with a number will always yield false, and thus the loop will never execute. if you want to try checking whether or not an NPC is valid, do something like

Code: Select all

while(boo[1] ~= nil)
instead of

Code: Select all

while(boo == 1)
and vice-versa (of course, this is assuming there can only be one big boo present at one time in the section)

though strictly speaking, i think that you'd want to refactor the code in general because if the boo isn't immediately dead when the loop begins to execute, it will never reach it's exit condition and will lock your game up.
How would I do that?

Re: Custom NPC Boss Exit

Posted: Wed Nov 10, 2021 5:38 am
by Emral
deice wrote:
Tue Nov 09, 2021 10:10 am
NPC.get() returns a table of NPCs. trying to compare that with a number will always yield false, and thus the loop will never execute. if you want to try checking whether or not an NPC is valid, do something like

Code: Select all

while(boo[1] ~= nil)
instead of

Code: Select all

while(boo == 1)
and vice-versa (of course, this is assuming there can only be one big boo present at one time in the section)

though strictly speaking, i think that you'd want to refactor the code in general because if the boo isn't immediately dead when the loop begins to execute, it will never reach it's exit condition and will lock your game up.
Using a while loop for this is indeed not a good way. This is in onTick, so all you'd accomplish is a hard lock, since this code will never finish while in the while loop.
While it's true that "boo" is a list of NPCs, and thus cannot be compared with a number, "#boo" IS the number of boos in that list, so...

Code: Select all

local hasSpawnedExit = false
function onTick()
	if not hasSpawnedExit then
		local boo = NPC.get(44, 1)
		if #boo == 0 then
			NPC.spawn(16, -155232, 160256, 1)
			hasSpawnedExit = true
		end
	end
end
This conditional executes when no boos were found by the NPC.get.

Re: Custom NPC Boss Exit

Posted: Fri Mar 11, 2022 7:30 pm
by Arco
DrMekar wrote:
Tue Nov 09, 2021 8:43 am
Not gonna be nitpicky, but why do you use Luna Lua for this? You can just do this with SMBX Events.
I don't know how to do that either

Re: Custom NPC Boss Exit

Posted: Wed Mar 16, 2022 3:45 pm
by DrMekar
Arco wrote:
Fri Mar 11, 2022 7:30 pm
DrMekar wrote:
Tue Nov 09, 2021 8:43 am
Not gonna be nitpicky, but why do you use Luna Lua for this? You can just do this with SMBX Events.
I don't know how to do that either
You select the Boss and open the NPC Settings. There you select an Event, you created before in 'Events', that
gets triggered when the NPC dies.