How to make "One Hit Deadly" npc?

Post here for help and support regarding LunaLua and SMBX2's libraries and features.

Moderator: Userbase Moderators

Someone
Koopa
Koopa
Posts: 17
Joined: Tue Dec 15, 2020 8:40 am

How to make "One Hit Deadly" npc?

Postby Someone » Mon Oct 04, 2021 1:02 pm

I know this will be a stupid question probably, But i'm really interested. How?

deice
Rocky Wrench
Rocky Wrench
Posts: 638
Joined: Fri Jul 23, 2021 7:35 am

Re: How to make "One Hit Deadly" npc?

Postby deice » Mon Oct 04, 2021 6:17 pm

the two simple ways to go about it are:

- inside the level's lua file, pick out the npc you want to perform the insant kill, and on each tick iterate through instances of it and check for collision with the player. if it's found, kill the player. (just make sure to verify the player isn't already dead, as not doing so can lead to an infinite loop of the player dying over and over)
- create a custom npc and perform the same checks in their code

intuitively, both of these should be around the same speed, and if i recall correctly, npcmanager or the like don't have any specific events for npc and player collision, so they're also likely the fastest options.

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

Re: How to make "One Hit Deadly" npc?

Postby Emral » Tue Oct 05, 2021 11:07 am

Instead of performing those checks in the onTickNPC of any given NPC, it might make more sense to put them into onPostPlayerHarm, to ensure that they don't fire/misfire as often.

Code: Select all

-- IDs 1 and 2 instakill. Edit the numbers in here to match the IDs you want. Separated by comma.
local INSTAKILLNPCS = {1, 2}

function onPostPlayerHarm(hurtPlayer)
	for k,v in ipairs(Colliders.getColliding{a = hurtPlayer, b = INSTAKILLNPCS, btype=Colliders.NPC} do
		hurtPlayer:kill()
	end
end

deice
Rocky Wrench
Rocky Wrench
Posts: 638
Joined: Fri Jul 23, 2021 7:35 am

Re: How to make "One Hit Deadly" npc?

Postby deice » Tue Oct 19, 2021 12:07 pm

Enjl wrote:
Tue Oct 05, 2021 11:07 am
Instead of performing those checks in the onTickNPC of any given NPC, it might make more sense to put them into onPostPlayerHarm, to ensure that they don't fire/misfire as often.
sorry for not noticing this reply earlier, but i wanted to ask under which conditions would these misfires happen? according to the documentation, triggered events happen after onTick (and conversely, i assume this means they also execute after onTickNPC) which executes before SMBX's game logic, and i don't intuitively see what could (within that game logic) disturb the collision checking (as a collision check within the game's code surely has to be performed anyways for onPlayerHarm to trigger).
i'd like this cleared up as it would be useful to know when making an NPC and because the documentation (both new and old) don't seem to cover nuances like this and i don't feel like sifting through a bulk of the 1.3 source code

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

Re: How to make "One Hit Deadly" npc?

Postby Emral » Tue Oct 19, 2021 2:53 pm

deice wrote:
Tue Oct 19, 2021 12:07 pm
Enjl wrote:
Tue Oct 05, 2021 11:07 am
Instead of performing those checks in the onTickNPC of any given NPC, it might make more sense to put them into onPostPlayerHarm, to ensure that they don't fire/misfire as often.
sorry for not noticing this reply earlier, but i wanted to ask under which conditions would these misfires happen? according to the documentation, triggered events happen after onTick (and conversely, i assume this means they also execute after onTickNPC) which executes before SMBX's game logic, and i don't intuitively see what could (within that game logic) disturb the collision checking (as a collision check within the game's code surely has to be performed anyways for onPlayerHarm to trigger).
i'd like this cleared up as it would be useful to know when making an NPC and because the documentation (both new and old) don't seem to cover nuances like this and i don't feel like sifting through a bulk of the 1.3 source code
Ah, sorry, must've worded that badly. I just meant avoiding doing collision checks on frames where you're not sure there's anything to check for. Think running the getColliding once every couple seconds rather than once every frame.

deice
Rocky Wrench
Rocky Wrench
Posts: 638
Joined: Fri Jul 23, 2021 7:35 am

Re: How to make "One Hit Deadly" npc?

Postby deice » Tue Oct 19, 2021 6:03 pm

Enjl wrote:
Tue Oct 19, 2021 2:53 pm
Ah, sorry, must've worded that badly. I just meant avoiding doing collision checks on frames where you're not sure there's anything to check for. Think running the getColliding once every couple seconds rather than once every frame.
oh, i see! thank you very much for clearing it up. looking at it logically, it is certainly faster to just let the game do it's thing rather than performing a potential myriad of unnecessary checks.

Someone
Koopa
Koopa
Posts: 17
Joined: Tue Dec 15, 2020 8:40 am

Re: How to make "One Hit Deadly" npc?

Postby Someone » Thu Nov 25, 2021 7:35 am

Thanks for answer! although it seems there's a problem with the code. i tried to copy the code at the end of level Luna. And then into world luna, but every time there's the same error... " ')' expected near 'do' ".. does somebody know how to remove that error? should i copy the code somewhere else?

deice
Rocky Wrench
Rocky Wrench
Posts: 638
Joined: Fri Jul 23, 2021 7:35 am

Re: How to make "One Hit Deadly" npc?

Postby deice » Thu Nov 25, 2021 9:12 am

Someone wrote:
Thu Nov 25, 2021 7:35 am
Thanks for answer! although it seems there's a problem with the code. i tried to copy the code at the end of level Luna. And then into world luna, but every time there's the same error... " ')' expected near 'do' ".. does somebody know how to remove that error? should i copy the code somewhere else?
looking at enjl's code it seems he did indeed forget to put a parenthesis to close the ipairs() call.
so, replace

Code: Select all

for k,v in ipairs(Colliders.getColliding{a = hurtPlayer, b = INSTAKILLNPCS, btype=Colliders.NPC} do
with

Code: Select all

for k,v in ipairs(Colliders.getColliding{a = hurtPlayer, b = INSTAKILLNPCS, btype=Colliders.NPC}) do
and it should work fine.

Someone
Koopa
Koopa
Posts: 17
Joined: Tue Dec 15, 2020 8:40 am

Re: How to make "One Hit Deadly" npc?

Postby Someone » Sun Nov 28, 2021 3:49 pm

deice wrote:
Thu Nov 25, 2021 9:12 am
Someone wrote:
Thu Nov 25, 2021 7:35 am
Thanks for answer! although it seems there's a problem with the code. i tried to copy the code at the end of level Luna. And then into world luna, but every time there's the same error... " ')' expected near 'do' ".. does somebody know how to remove that error? should i copy the code somewhere else?
looking at enjl's code it seems he did indeed forget to put a parenthesis to close the ipairs() call.
so, replace

Code: Select all

for k,v in ipairs(Colliders.getColliding{a = hurtPlayer, b = INSTAKILLNPCS, btype=Colliders.NPC} do
with

Code: Select all

for k,v in ipairs(Colliders.getColliding{a = hurtPlayer, b = INSTAKILLNPCS, btype=Colliders.NPC}) do
and it should work fine.
Okay. so uh... Error message gone. that's good. but it seems like code didn't really work. The NPC's which were put at "local INSTAKILLNPCS" are working as regular enemy's. I tried once again putting it at the end of Level luna (and after that didn't work. at world luna) but it didn't work...


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari