HealthPoint is very unreliable, as Quantix said. Pnpc doesn't do the HP stuff out of the box, but it helps you set them up per-npc.
Using pnpc is simple:
Code: Select all
local pnpc = API.load("pnpc")
local sturdyNPCTable = {
[1] = 3,
[2] = 4
}
function onNPCKill(killObj, killedNPC, killReason)
if sturdyNPCTable[killedNPC.id] then
killedNPC = pnpc.wrap(killedNPC)
if killedNPC.data.hp == nil then
killedNPC.data.hp = sturdyNPCTable[killedNPC.id]
end
killedNPC.data.hp = killedNPC.data.hp - 1
if killedNPC.data.hp ~= 0 then
killObj.cancelled = true
end
end
end
This won't cancel out the score you get from bouncing on NPCs, there is currently no way to do that. However, this does the following:
-onNPCKill runs whenever ANY NPC dies. The sturdyNPCTable narrows it down to only the NPC IDs you want checked. In this example, I check for IDs 1 and 2, which are SMB3 goombas.
-pnpc.wrap gives the NPC a unique identifier which pnpc uses to find the NPC across ticks. This can be used to attach variables to NPCs, like a HP variable which we will attach here.
-if we haven't already, we then initialise the HP variable, count down by 1 every time the NPC is killed, and check if the value is not 0. If the value is not 0 we then cancel the killObj, which makes it so that the NPC does not die.
-You will notice that if we set the initial HP value to 0 or lower, the NPC will never die. The HP variable will count down, but never reach 0. This is a way to make the NPC invincible/unkillable.
Since onNPCKill is called whenever an NPC is killed and pnpc remembers which NPC has how much HP, we can count down the HP value bit by bit every time the NPC dies.
You might have noticed the "killReason" variable which we haven't used. This is the kill type performed on the NPC. You can perform additional checks to exclude certain kill types. Further down on this page is a list for reference:
http://wohlsoft.ru/pgewiki/LunaLua_events
Hope this helped!