"No event onNPCHarm was found!"

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

Moderator: Userbase Moderators

vee_Sleepy
Fighter Fly
Fighter Fly
Posts: 45
Joined: Tue Nov 29, 2022 7:15 pm
Flair: oh worm?
Pronouns: they/it

"No event onNPCHarm was found!"

Postby vee_Sleepy » Fri Sep 13, 2024 10:35 am

I'm trying to code a custom boss NPC, it's just a big Goomba with a spiked helmet that uses the golden Bowser statue jumping behavior, and I'm trying to figure out how to handle it taking damage and having health. Problem is, every time i go to test i get this error:

Image

Even though there is such an event!

Code: Select all

local damage = 1

local jumpInterval = 10


--Register events
function sampleNPC.onInitAPI()
	npcManager.registerEvent(npcID, sampleNPC, "onNPCHarm")
	npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
	npcManager.registerEvent(npcID, sampleNPC, "onNPCKill")
end

function sampleNPC.onNPCHarm(eventObj, npc, harmReason, culprit)
	if harmReason == HARM_TYPE_NPC then
		damage = damage + 1
	end
end
the "damage" variable is more so me trying to track that to alter the boss' behavior, making it move faster as it gets hurt, but its kinda beside the point
what do i do to the event or put in it that makes it work? is it just bad syntax somewhere? (i absolutely Do Not doubt that, given my skill level)

any help is appreciated! thanks in advance

DRACalgar Law
Monty Mole
Monty Mole
Posts: 135
Joined: Sun Oct 16, 2022 3:59 pm
Flair: King of the Boss Makers
Pronouns: he/him
Contact:

Re: "No event onNPCHarm was found!"

Postby DRACalgar Law » Fri Sep 13, 2024 12:54 pm

you would need to place eventObj.cancelled as true to negate from getting killed. you can put v:kill([harm type]) to lets say override it and have it get killed successfully. i may not know much of eventObj than i think. also the npc you put there is referring to the object it is doing, it can just be v or npc. generally, if i were you, I would look at other user's NPCs that have health such as my bosses in my Boss Army pack.

vee_Sleepy
Fighter Fly
Fighter Fly
Posts: 45
Joined: Tue Nov 29, 2022 7:15 pm
Flair: oh worm?
Pronouns: they/it

Re: "No event onNPCHarm was found!"

Postby vee_Sleepy » Fri Sep 13, 2024 1:48 pm

i forgot to mention that, like, it doesn't seem to take damage from thrown blue blocks but it dies instantly if a still held blue block touches it, which i found weird

Added in 39 minutes 47 seconds:
DRACalgar Law wrote:
Fri Sep 13, 2024 12:54 pm
you would need to place eventObj.cancelled as true to negate from getting killed. you can put v:kill([harm type]) to lets say override it and have it get killed successfully. i may not know much of eventObj than i think. also the npc you put there is referring to the object it is doing, it can just be v or npc. generally, if i were you, I would look at other user's NPCs that have health such as my bosses in my Boss Army pack.
i tried to copy some of the onNPCHarm code from your Aquamentus boss (i hope you don't mind) and its no longer giving me the error for the event, but i'm currently getting an error regarding this chunk:

Code: Select all

if data.hp >= settings.hp then
			eventObj.cancelled = true
			SFX.play(sfx_death)
			data.state = 3
			data.timer = 0
			v.speedX = 0
		end	
		if data.hp < settings.hp then
			eventObj.cancelled = true
		end
error says "attempt to compare two nil values"

EDIT: i'm getting this error when i hold the blue block into the boss, and then when i dismiss the error it dies instantly like i was describing before

i did also include the "local data" and "local settings" stuff above it, at the top of the onNPCHarm event, but it seems its not letting me do anything with the data values?

i hope i'm explaining well enough

Marioman2007
2025 Egg Hunter
2025 Egg Hunter
Posts: 527
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: "No event onNPCHarm was found!"

Postby Marioman2007 » Sat Sep 14, 2024 1:44 am

vee_Sleepy wrote:
Fri Sep 13, 2024 10:35 am

Code: Select all

local damage = 1

local jumpInterval = 10


--Register events
function sampleNPC.onInitAPI()
	npcManager.registerEvent(npcID, sampleNPC, "onNPCHarm")
	npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
	npcManager.registerEvent(npcID, sampleNPC, "onNPCKill")
end

function sampleNPC.onNPCHarm(eventObj, npc, harmReason, culprit)
	if harmReason == HARM_TYPE_NPC then
		damage = damage + 1
	end
end

onNPCHarm and onNPCKill cannot be registered by npcManager, you need to use registerEvent and an id check to make them work.

Code: Select all

function sampleNPC.onInitAPI()
	registerEvent(sampleNPC, "onNPCHarm")
	registerEvent(sampleNPC, "onNPCKill")
	npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
end

function sampleNPC.onNPCHarm(eventObj, npc, harmReason, culprit)
	-- don't run for other npcs
	if npc.id ~= npcID then return end
	
	-- do stuff here
end

vee_Sleepy
Fighter Fly
Fighter Fly
Posts: 45
Joined: Tue Nov 29, 2022 7:15 pm
Flair: oh worm?
Pronouns: they/it

Re: "No event onNPCHarm was found!"

Postby vee_Sleepy » Sat Sep 14, 2024 11:29 am

Marioman2007 wrote:
Sat Sep 14, 2024 1:44 am

onNPCHarm and onNPCKill cannot be registered by npcManager, you need to use registerEvent and an id check to make them work.
i already fixed that

Added in 15 minutes 30 seconds:
this is what the onNPCHarm function currently looks like

Code: Select all

function sampleNPC.onNPCHarm(eventObj, v, killReason, culprit)
		local data = v.data
		local settings = v.data._settings
	local config = NPC.config[v.id]
		if v.id ~= npcID then return end
		
	if v:mem(0x156,FIELD_WORD) <= 0 then
		if culprit then
			if (NPC.HITTABLE_MAP[culprit.id] or culprit.id == 45 and v:mem(0x138, FIELD_WORD) == 0) and culprit.id ~= 45 then
				culprit:kill(HARM_TYPE_NPC)
				SFX.play(sfx_hit)
				data.hp = data.hp + 1
				v:mem(0x156,FIELD_WORD,30)
				Animation.spawn(75, culprit.x, culprit.y)
			end
		end
	end
	
		if data.hp >= settings.hp then
			eventObj.cancelled = true
			SFX.play(sfx_death)
			data.state = 3
			data.timer = 0
			v.speedX = 0
		end	
		if data.hp < settings.hp then
			eventObj.cancelled = true
		end
end
and when i run into the boss NPC with an ice block held (thrown blocks do nothing to it, it seems) i get the "attempt to compare two nil values" error regarding the line that says "if data.hp >= settings.hp then", and then when i dismiss the error the boss just instantly dies

Image

idk what i did wrong here
(i hope the image is legible enough, postimg is garbling it pretty bad for some god damn reason)*
*my mistake, second monitor is just awful, don't mind that---

WildW
Rocky Wrench
Rocky Wrench
Posts: 676
Joined: Thu Dec 14, 2017 2:21 pm
Flair: C# more like Db
Pronouns: he/him

Re: "No event onNPCHarm was found!"

Postby WildW » Sat Sep 14, 2024 9:08 pm

vee_Sleepy wrote:
Sat Sep 14, 2024 11:44 am
Marioman2007 wrote:
Sat Sep 14, 2024 1:44 am

onNPCHarm and onNPCKill cannot be registered by npcManager, you need to use registerEvent and an id check to make them work.

Code: Select all

function sampleNPC.onNPCHarm(eventObj, v, killReason, culprit)
		local data = v.data
		local settings = v.data._settings
	local config = NPC.config[v.id]
		if v.id ~= npcID then return end
		
	if v:mem(0x156,FIELD_WORD) <= 0 then
		if culprit then
			if (NPC.HITTABLE_MAP[culprit.id] or culprit.id == 45 and v:mem(0x138, FIELD_WORD) == 0) and culprit.id ~= 45 then
				culprit:kill(HARM_TYPE_NPC)
				SFX.play(sfx_hit)
				data.hp = data.hp + 1
				v:mem(0x156,FIELD_WORD,30)
				Animation.spawn(75, culprit.x, culprit.y)
			end
		end
	end
	
		if data.hp >= settings.hp then
			eventObj.cancelled = true
			SFX.play(sfx_death)
			data.state = 3
			data.timer = 0
			v.speedX = 0
		end	
		if data.hp < settings.hp then
			eventObj.cancelled = true
		end
end
and when i run into the boss NPC with an ice block held (thrown blocks do nothing to it, it seems) i get the "attempt to compare two nil values" error regarding the line that says "if data.hp >= settings.hp then", and then when i dismiss the error the boss just instantly dies

Image
data.hp and settings.hp aren't automatically promulgated. You need to be setting them somewhere. Perhaps check out npc-351


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari