Need help with lua? - LunaLua General Help

This is the place for discussion and support for LunaLua and related modifications and libraries.
Forum rules
Before you make a topic/post, consider the following:
-Is there a topic for this already?
-Is your post on topic/appropriate?
-Are you posting in the right forum/following the forum rules?
Melko
Cheep-Cheep
Cheep-Cheep
Posts: 11
Joined: Fri Mar 22, 2019 12:40 am
Flair: Cricket noises!

Re: Need help with lua? - LunaLua General Help

Postby Melko » Fri Mar 22, 2019 1:01 am

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

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Wed Mar 27, 2019 12:22 pm

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.


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 2 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari