Page 1 of 2

I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 7:17 pm
by dragon3025
I only need it to effect a single level in my episode. So do I place the healthpoint.lua file in my level folder, then make a lunadll.lua file in the same level folder, then in the lunadll.lua file I put in:

Code: Select all

HealthPoint.setNPCHealth(168, 5)

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 7:19 pm
by Emral
Load APIs with
API.load("apiname")
Then use functions from here:
http://wohlsoft.ru/pgewiki/HealthPoint

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 7:32 pm
by dragon3025
Enjl wrote:Load APIs with
API.load("apiname")
Then use functions from here:
http://wohlsoft.ru/pgewiki/HealthPoint
It didn't work, so I must be doing it wrong. I kept the healthpoint.lua in the level folder (that's still needed right?).

I changed the code lunadll.lua to:

Code: Select all

API.load(healthpoint.lua)

HealthPoint.setNPCDamage(168, 1, 1)
HealthPoint.setNPCDamage(168, 4, 0)
HealthPoint.setNPCHealth(168, 5)
For API.load("healthpoint") I tried it with and without qoutation marks and with and without .lua at the end.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 7:37 pm
by Emral
oh my bad you need to do
local HealthPoint = API.load("healthpoint")
unsure about the capital letters in the quotation mark.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 8:25 pm
by dragon3025
Enjl wrote:oh my bad you need to do
local HealthPoint = API.load("healthpoint")
unsure about the capital letters in the quotation mark.
That didn't work either. I also tried following this link: http://www.smbxgame.com/forums/v ... 69&t=12546 and doing the same way:

Code: Select all

local HealthPoint = loadAPI("healthpoint")

HealthPoint.setNPCDamage(168, 1, 1)
HealthPoint.setNPCDamage(168, 4, 0)
HealthPoint.setNPCHealth(168, 5)
But that didn't work either

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 8:35 pm
by PixelPest
You'll want to use API.load() and make sure that the capitalization of healthcount (in the string) matches that of your file

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 8:40 pm
by Emral
what error is it giving you? Make sure your file is called lunadll.lua, not lunadll.lua.txt. Also make sure your .lvl is one folder above the lunadll.lua.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 8:55 pm
by dragon3025
Enjl wrote:what error is it giving you? Make sure your file is called lunadll.lua, not lunadll.lua.txt. Also make sure your .lvl is one folder above the lunadll.lua.
Okay so I have it set up this way:

Code: Select all

local HealthPoint = API.load("healthpoint")

HealthPoint.setNPCDamage(168, 1, 1)
HealthPoint.setNPCDamage(168, 4, 0)
HealthPoint.setNPCHealth(168, 5)
and I checked and it matches the capitalization, and all of the files lua files and not txt files. I'm not sure where to place the files, I have a both of the files inside: SMBX 2.0 Open Beta-Release 2\worlds\sandbox\test (they're both in the level folder and not the world folder). The test.lvl file is in the world folder.

I got this error:
...2.0 Open Beta-Release 2\worlds\sandbox\test\lunadll.lua:1 attempt to index
global 'API' (a nil value)

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 9:00 pm
by underFlo
Your LunaLua is outdated.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 9:01 pm
by Emral
your lunalua is outdated. Try loadSharedAPI because updating 2.0's lua is a pain and not worth it with the next beta coming soon

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 9:11 pm
by underFlo
Enjl wrote:your lunalua is outdated. Try loadSharedAPI because updating 2.0's lua is a pain and not worth it with the next beta coming soon
Oh right, forgot that method existed.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 9:23 pm
by dragon3025
Enjl wrote:your lunalua is outdated. Try loadSharedAPI because updating 2.0's lua is a pain and not worth it with the next beta coming soon
Dang, I can't wait for the next beta then.

Well that almost worked. The hammers aren't hurting it, but jumping on it doesn't. It is the bully npc, so I'm sure that's why. I tried switching the ID to the ID of hammer bro, then I tested on a hammer bro and it worked completely (hammers didn't hurt it and it takes 5 jumps to kill it). I was actually making a Chargin chuck out of Bully and I gave it this npc-168.txt:

Code: Select all

width=52
height=54
gfxwidth=52
gfxheight=54
frames=2
framestyle=1
framespeed=2
score=5
noyoshi=1
Either using lunalua or the npc txt file, I need to make it so jumping on chargin chuck damages it. I though that this part in my lunadll.lua would work, but it didn't since jumping does kill it:

Code: Select all

HealthPoint.setNPCDamage(29, 1, 1)
EDIT: I just discovered other enemies now are taking more jumps to kill.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 9:40 pm
by Emral
Healthpoint is weird. It automatically gives properties to all NPCs. There's an onNPCKill event in lunalua which allows you to hook right into what happens BEFORE an npc dies and even cancel the death. I suggest looking into that, in conjunction with pnpc.lua to save npc-specific variables for HP

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Tue Aug 16, 2016 10:21 pm
by dragon3025
Enjl wrote:Healthpoint is weird. It automatically gives properties to all NPCs. There's an onNPCKill event in lunalua which allows you to hook right into what happens BEFORE an npc dies and even cancel the death. I suggest looking into that, in conjunction with pnpc.lua to save npc-specific variables for HP
So I'm creating a new variable for the npc which goes down every time I hit it, and if it hasn't reached 0, then I cancel the death?

I got this so far:

Code: Select all

function onNPCKill(killObj, killedNPC, killReason)
	if killedNPC.id == 168
		if killReason == 3 then --Here's where'd I'd also test if the custom variable equals 0.
			--Variable goes down by one here
		else
			Event.cancelled --Am I canceling the event correctly?
	end
end
I looked through all the functions in pnpc and I can't tell which function allows me to give it a variable. I'm also not sure if my code cancels the event correctly.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Wed Aug 17, 2016 4:33 am
by Emral
You're missing a "then" in the ID check.
To cancel the event, use killObj.cancelled=true
Make sure to also allow death by reason 9 if you want despawning to be possible.

Alright onto pnpc.
Pnpc is an api which lets you wrap a specific npc object with
local w (or whatever you want it to be) = pnpc.wrap(npcObj) (in your case, killedNPC.
If the wrapper doesn't exist yet, it'll be initialised. If it exists, it will be returned.
w can access everything a npc can, but also has access to a few more things, most notable, pnpcObj.data

w.data is an empty table and this is where you can initialise your hp and keep track of it by creating a new variable in this table.

Sorry for the kinda hacky explanation. I'm a bit in a hurry >.< Hope that clears it up for you.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Wed Aug 17, 2016 4:29 pm
by dragon3025
Enjl wrote:You're missing a "then" in the ID check.
To cancel the event, use killObj.cancelled=true
Make sure to also allow death by reason 9 if you want despawning to be possible.

Alright onto pnpc.
Pnpc is an api which lets you wrap a specific npc object with
local w (or whatever you want it to be) = pnpc.wrap(npcObj) (in your case, killedNPC.
If the wrapper doesn't exist yet, it'll be initialised. If it exists, it will be returned.
w can access everything a npc can, but also has access to a few more things, most notable, pnpcObj.data

w.data is an empty table and this is where you can initialise your hp and keep track of it by creating a new variable in this table.

Sorry for the kinda hacky explanation. I'm a bit in a hurry >.< Hope that clears it up for you.
Here's my code:

Code: Select all

local w = pnpc.wrap(npcObj)

function onNPCKill(killObj, killedNPC, killReason)
	if killedNPC.id == 168 then
		if killReason == 9 then
			if w < 2 then
				w = w + 1
			else
				killObj.cancelled=true
			end
		end
	end
end
I wasn't sure if you meant I should put pnpc.wrap(npcObj) or pnpc.wrap(killedNPC), so I tried them both, but both times I got this error:
2.0 Open Beta-Release 2\worlds\sandbox\test\lunadll.lua:1: attempt to index
global 'pnpc' (a nil value)
Also it looks like I'm initializing the variable w, do I put a w = 0 after that line to give it a value?

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Wed Aug 17, 2016 5:06 pm
by Emral
pnpc is an API, just like healthPoint. You load it with

Code: Select all

local pnpc = loadSharedAPI("pnpc")
Afterward you can wrap your NPC object (killedNPC) into a pnpc object with

Code: Select all

w = pnpc.wrap(killedNPC)
w has all properties of killedNPC. There's w.x, w.height and you can also set memory offsets of the npc with w:mem().
The reason we're using pnpc is so that we can store the HP variable for each specific enemy and keep them consistent across ticks. That's what pnpc was made for and it's super handy.
But what you're interested in is the w.data table.
You can initialise a variable with:

Code: Select all

if w.data.hp == nil then
  w.data.hp = 0
end

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Wed Aug 17, 2016 5:30 pm
by dragon3025
Enjl wrote:pnpc is an API, just like healthPoint. You load it with

Code: Select all

local pnpc = loadSharedAPI("pnpc")
Afterward you can wrap your NPC object (killedNPC) into a pnpc object with

Code: Select all

w = pnpc.wrap(killedNPC)
w has all properties of killedNPC. There's w.x, w.height and you can also set memory offsets of the npc with w:mem().
The reason we're using pnpc is so that we can store the HP variable for each specific enemy and keep them consistent across ticks. That's what pnpc was made for and it's super handy.
But what you're interested in is the w.data table.
You can initialise a variable with:

Code: Select all

if w.data.hp == nil then
  w.data.hp = 0
end
I changed my code to:

Code: Select all

local pnpc = loadSharedAPI("pnpc")

w = pnpc.wrap(killedNPC)

if w.data.hp == nil then
  w.data.hp = 0
end

function onNPCKill(killObj, killedNPC, killReason)
	if killedNPC.id == 168 then
		if killReason == 9 then
			if w.data.hp < 2 then
				w.data.hp = w.data.hp + 1
			else
				killObj.cancelled=true
			end
		end
	end
end
It said
...king\SMBX 2.0 Open Beta-Release 2\LuaScriptsLib\pnpc.lua:142: attempt to
index local 'npc' (a nil value)

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Wed Aug 17, 2016 5:42 pm
by Emral
You have to wrap killedNPC and start doing stuff with it in a scope where it's actually defined. I suggest after the killedNPC.id check. Also I noticed that your HP only counts down on killReason 9 which is equivalent to despawn or being collected.

Re: I'm trying to set up health points for an enemy using healthpoint.lua

Posted: Wed Aug 17, 2016 6:32 pm
by dragon3025
Enjl wrote:You have to wrap killedNPC and start doing stuff with it in a scope where it's actually defined. I suggest after the killedNPC.id check. Also I noticed that your HP only counts down on killReason 9 which is equivalent to despawn or being collected.
You posted eariler to make sure to also allow death by reason 9 if you want despawning to be possible. I was using 3 to make it so stomping on it 3 times kills it, did you mean use 9 along with 3? My code is now:

Code: Select all

local pnpc = loadSharedAPI("pnpc")

function onNPCKill(killObj, killedNPC, killReason)

	w = pnpc.wrap(killedNPC)

	if w.data.hp == nil then
	  w.data.hp = 0
	end

	if killedNPC.id == 168 then
		if killReason == 9 then
			if w.data.hp < 2 then
				w.data.hp = w.data.hp + 1
			else
				killObj.cancelled=true
			end
		end
	end
end
I didn't get any errors, but I still couldn't kill it by jumping on it and hitting it with a hammer killed it in one hit.