Page 1 of 1

Detecting multiple enemy deaths

Posted: Mon Mar 11, 2019 4:59 pm
by Dinoman972
Hi, everyone! I got started in SMBX2 recently and I'm working on an episode, but I have a little problem. Basically, I want to make a boss fight where the player has to fight two enemies at once, which would end after both are defeated. This should be possible if both enemies are sharing a layer, but I have to use separate layers for each in order for their "hurt states" (you know, those short invicibility frames after you damage them that are seen in bosses such as Boom Boom and Birdo) to work. My question is: is there any other way to trigger an event when both enemies are defeated?

Thanks in advance.

Re: Detecting multiple enemy deaths

Posted: Mon Mar 11, 2019 5:44 pm
by Emral
You can use a little lua workaround. Make a file called lunadll.lua inside your level folder (the folder with the name of the level, where you put custom graphics in), and paste the following inside:

Code: Select all

--Change the variables here so that the text in quotation marks matches the names of the "Death" events of the boss NPCs
local firstLayerDeathEvent = "REPLACE THIS"
local secondLayerDeathEvent = "REPLACE THIS TOO"

--Change the variable here so that the text in quotation marks matches the name of the event you would like to be executed when both NPCs died.
local bothDeadEvent = "REPLACE THIS AS WELL"

local firstEnemyDefeated = false
local secondEnemyDefeated = false

function onEvent(eventname)
	if eventname == firstLayerDeathEvent then
		firstEnemyDefeated = true
	elseif eventname == secondLayerDeathEvent then
		secondEnemyDefeated = true
	end
	
	if firstEnemyDefeated and secondEnemyDefeated then
		firstEnemyDefeated = false
		secondEnemyDefeated = false
		triggerEvent(bothDeadEvent)
	end
end
Make sure to replace the marked sections, too.

What this code does is keep track of which enemies have been defeated, and triggers an event as soon as both are defeated. OnEvent is a function that runs every time any SMBX event is called.

Re: Detecting multiple enemy deaths

Posted: Tue Mar 12, 2019 11:42 am
by Dinoman972
Alright, I'll try. Thank you very much for the help!

Re: Detecting multiple enemy deaths

Posted: Tue Mar 12, 2019 11:46 am
by Emral
I just noticed the forum formatting has broken the newlines on the default theme. For safety, I have copied it to a hastebin link where it's formatted properly.
https://hastebin.com/xesasusoje.lua

Re: Detecting multiple enemy deaths

Posted: Sat Mar 16, 2019 5:26 am
by Dinoman972
Alright, it works now! It kinda messed up the dialogue, for some reason, but I think I'll be able to fix that. Thanks, again!

Re: Detecting multiple enemy deaths

Posted: Fri Aug 14, 2020 7:47 am
by Theopold1
Dinoman972 wrote:
Mon Mar 11, 2019 4:59 pm
"...This should be possible if both enemies are sharing a layer..."
I want to know how to make a door spawn when all enemies are killed on one layer, myself! By the way, The enemies are Bot(Cyan), Charging Chuck, Wosu, and Hammer Bro, and I want to use characters such as Link, Bowser, Samus, and Mega.
Edit: There's nothing else on that layer, by the way.

Re: Detecting multiple enemy deaths

Posted: Fri Aug 14, 2020 4:46 pm
by Emral
Set each enemy's "no more objects on layer" event to an event that shows the door layer. An enemy carries out that event if it detects no more objects being on its layer.