Page 1 of 1

HealthPoint.lua Help

Posted: Mon Dec 04, 2017 9:26 pm
by fern
Is HealthPoint broken? Or am I using it wrong?

I have this code:

Code: Select all

HealthPoint.makeNPCInvincible(23);
HealthPoint.setNPCHealth(24, 10);
For the invincible NPC, I believe it only adds three hitpoints, because I get three hits for 8000 points.

For the other one it takes three hits instead of ten. No matter what value I input it takes three hits. Also, it changes the health for all NPCs of that type.

EDIT: It actually makes EVERY NPC take three hits. Is there an alternative or am I using it wrong?

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 7:11 am
by Quantix
HealthPoint.lua has been deprecated for a while now. You're better off using pnpc, but I can't go over why right now.

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 1:55 pm
by fern
Quantix wrote:HealthPoint.lua has been deprecated for a while now. You're better off using pnpc, but I can't go over why right now.
Thanks, I looked into it and have no idea how it works, I'm new to Lua.

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 3:33 pm
by Emral
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!

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 3:39 pm
by PixelPest
You can also set the memory field for HP (may only work for bosses) to a lower number when the NPC is spawned

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 5:59 pm
by fern
Enjl wrote: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!
You have no idea how much this helped! Just one more question:

Is there a way to "force" a kill animation? I tried forcing it by changing killReason to the specific one I wanted but it didn't work. I also tried setting kill flag off because I didn't know what that meant but it causes the goomba to fly as if was killed with a shell. I'm trying to force the Link sword kill effect when they get stomped.

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 6:00 pm
by The0x539
fern wrote:
Enjl wrote: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!
You have no idea how much this helped! Just one more question:

Is there a way to "force" a kill animation? I tried forcing it by changing killReason to the specific one I wanted but it didn't work. I also tried setting kill flag off because I didn't know what that meant but it causes the goomba to fly as if was killed with a shell. I'm trying to force the Link sword kill effect when they get stomped.
cancel the kill event and call the npc's kill method yourself

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 6:10 pm
by fern
The0x539 wrote: cancel the kill event and call the npc's kill method yourself
Here's what I tried writing inside onNPCKill():

Code: Select all

if killReason ~= 10 and killedNPC.data.hp == 0 then
	killObj.cancelled = true
	killReason = 10
	killObj.cancelled = false
end

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 6:14 pm
by The0x539
fern wrote:
The0x539 wrote: cancel the kill event and call the npc's kill method yourself
Here's what I tried writing inside onNPCKill():

Code: Select all

if killReason ~= 10 and killedNPC.data.hp == 0 then
	killObj.cancelled = true
	killReason = 10
	killObj.cancelled = false
end
the value of killObj.cancelled doesn't matter until the function is done doing its thing

Code: Select all

if killReason ~= 10 and killedNPC.data.hp == 0 then
	killObj.cancelled = true
	killedNPC:harm(10)
end

Re: HealthPoint.lua Help

Posted: Tue Dec 05, 2017 6:54 pm
by fern
The0x539 wrote:
fern wrote:
The0x539 wrote: cancel the kill event and call the npc's kill method yourself
Here's what I tried writing inside onNPCKill():

Code: Select all

if killReason ~= 10 and killedNPC.data.hp == 0 then
	killObj.cancelled = true
	killReason = 10
	killObj.cancelled = false
end
the value of killObj.cancelled doesn't matter until the function is done doing its thing

Code: Select all

if killReason ~= 10 and killedNPC.data.hp == 0 then
	killObj.cancelled = true
	killedNPC:harm(10)
end
Ah, I see. Thanks a lot!

EDIT: I tested this and I get the same as before, which is the NPC skipping that last hit.

Re: HealthPoint.lua Help

Posted: Wed Dec 06, 2017 12:18 am
by fern
Ok, I fixed it by using killedNPC:kill(10) instead of harm and not cancelling the death. Thanks for your help guys!

Also, I feel like I've been making too many threads asking for help, should I be using the LunaLua Help thread instead?