Page 76 of 76

Re: Need help with lua? - LunaLua General Help

Posted: Fri Mar 22, 2019 1:01 am
by Melko
Simple question for the brightest of you.
I'm trying to make a NPC split into two other NPCs when stomped but I have no idea how. (In this case, a huge goomba turning into SMB3 goombas.)
So far I've made this, but this makes ALL stomped NPCs spawn 2 goombas around ALL big goombas.

Code: Select all

function onNPCKill(eventObj,killedNPC,killReason)
	
	for k,v in pairs(NPC.get(71,player.section)) do
	
		a = NPC.spawn(1,v.x+32,v.y+15,player.section)
		b = NPC.spawn(1,v.x-26,v.y+15,player.section)
		a.direction = 1
		b.direction = 0
		a.speedY = -6
		b.speedY = -6
		
	end
	
end
Image

Re: Need help with lua? - LunaLua General Help

Posted: Wed Mar 27, 2019 12:22 pm
by Emral
Melko wrote:
Fri Mar 22, 2019 1:01 am
You're almost there! Just a little flaw in the logic.
OnNPCKill runs when any npc dies, so you need to perform further checks in order to get where you want to be.
OnNPCKill's arguments are very useful in this! Particularly killedNPC and killReason.
killedNPC contains the NPC object being killed, so you already know which NPC, or goomba, died! So the first check would be to see if the NPC that died actually is one of those big goombas, by checking "if killedNPC.id == BIG_GOOMBA_ID" before executing further code related to the goombas.

Another thing you could check for is killReason. You can find them on this page, all prefixed with HARM_TYPE
https://wohlsoft.ru/pgewiki/LunaLua_constants
Maybe you want to exclude certain harm types like lava from causing a split in the first place. Checking stuff like "if killReason == HARM_TYPE_LAVA" would be the way to go about that.
After those checks it's time to spawn the little goombas! Remember that killedNPC contains the goomba that died! So you already have the NPC object you want to spawn stuff around and don't need to use a for loop to get it. You can use values like killedNPC.x and killedNPC.y for the position to spawn at, and stuff like that!

Hope this helps.