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?
User avatar
IttaBaby
Koopa Troopa
Koopa Troopa
Posts: 25
Joined: Sat Apr 08, 2017 8:19 pm
Chat Username: IttaBaby

Re: Need help with lua? - LunaLua General Help

Postby IttaBaby » Sun Mar 17, 2019 2:19 am

feel free to get upset with me for this following outburst:
Why am I so stupid? I can't grasp even the basics no matter how hard I try. I've gone through programming classes at the local college, I barely passed and still everything I touch stops working and I can't understand anything. Every time I get help I can't understand what the advice I've been given even means. I've tried and tried for years now, but I can't grasp this. Should I even keep trying? Should I just vanish?

User avatar
Melko
Goomba
Goomba
Posts: 2
Joined: Fri Mar 22, 2019 12:40 am
Flair: I'm da best, except when I'm not
Current Project: Random levels/Learning Lua
Chat Username: Melko!

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

User avatar
Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 7325
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » 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.
ImageImageImageImage
ImageImageImageImageImageImage


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 7 guests