Page 1 of 1

How to make "One Hit Deadly" npc?

Posted: Mon Oct 04, 2021 1:02 pm
by Someone
I know this will be a stupid question probably, But i'm really interested. How?

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

Posted: Mon Oct 04, 2021 6:17 pm
by deice
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.

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

Posted: Tue Oct 05, 2021 11:07 am
by Emral
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

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

Posted: Tue Oct 19, 2021 12:07 pm
by deice
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

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

Posted: Tue Oct 19, 2021 2:53 pm
by Emral
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.

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

Posted: Tue Oct 19, 2021 6:03 pm
by deice
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.

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

Posted: Thu Nov 25, 2021 7:35 am
by Someone
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?

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

Posted: Thu Nov 25, 2021 9:12 am
by deice
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.

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

Posted: Sun Nov 28, 2021 3:49 pm
by Someone
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...