Explosion.spawn doesn't work

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

Moderator: Userbase Moderators

Gabriel_2004
Cheep-Cheep
Cheep-Cheep
Posts: 10
Joined: Sat Feb 20, 2021 11:04 am
Pronouns: he/him

Explosion.spawn doesn't work

Postby Gabriel_2004 » Tue Apr 05, 2022 7:24 pm

When I was testing the custom npc that I was doing I ended up coming across this error, from what I understand it is related to "Explosion.spawn" , but I didn't understand how to fix this error

Code: Select all

--NPCManager is required for setting basic NPC properties
local npcManager = require("npcManager")

--Create the library table
local GiantTorpedoTed = {}
--NPC_ID is dynamic based on the name of the library file
local npcID = NPC_ID

--Defines NPC config for our NPC. You can remove superfluous definitions.
local GiantTorpedoTedSettings = {
	id = npcID,
	--Sprite size
	gfxheight = 72,
	gfxwidth = 150,
	--Hitbox size. Bottom-center-bound to sprite size.
	width = 150,
	height = 72,
	--Sprite offset from hitbox for adjusting hitbox anchor on sprite.
	gfxoffsetx = 0,
	gfxoffsety = 0,
	--Frameloop-related
	frames = 4,
	framestyle = 1,
	framespeed = 8, --# frames between frame change
	--Movement speed. Only affects speedX by default.
	speed = 1.5,
	--Collision-related
	npcblock = false,
	npcblocktop = false, --Misnomer, affects whether thrown NPCs bounce off the NPC.
	playerblock = false,
	playerblocktop = false, --Also handles other NPCs walking atop this NPC.

	nohurt=true,
	nogravity = true,
	noblockcollision = true,
	nofireball = true,
	noiceball = true,
	noyoshi= true,
	nowaterphysics = false,
	--Various interactions
	jumphurt = false, --If true, spiny-like
	spinjumpsafe = false, --If true, prevents player hurt when spinjumping
	harmlessgrab = false, --Held NPC hurts other NPCs if false
	harmlessthrown = false, --Thrown NPC hurts other NPCs if false

	grabside=false,
	grabtop=false,

	--Identity-related flags. Apply various vanilla AI based on the flag:
	--iswalker = false,
	--isbot = false,
	--isvegetable = false,
	--isshoe = false,
	--isyoshi = false,
	--isinteractable = false,
	--iscoin = false,
	--isvine = false,
	--iscollectablegoal = false,
	--isflying = false,
	--iswaternpc = false,
	--isshell = false,

	--Emits light if the Darkness feature is active:
	--lightradius = 100,
	--lightbrightness = 1,
	--lightoffsetx = 0,
	--lightoffsety = 0,
	--lightcolor = Color.white,

	--Define custom properties below
}

--Applies NPC settings
npcManager.setNpcSettings(GiantTorpedoTedSettings)

--Register the vulnerable harm types for this NPC. The first table defines the harm types the NPC should be affected by, while the second maps an effect to each, if desired.
npcManager.registerHarmTypes(npcID,
	{
		--HARM_TYPE_JUMP,
		--HARM_TYPE_FROMBELOW,
		HARM_TYPE_NPC,
		HARM_TYPE_PROJECTILE_USED,
		--HARM_TYPE_LAVA,
		--HARM_TYPE_HELD,
		HARM_TYPE_TAIL,
		--HARM_TYPE_SPINJUMP,
		--HARM_TYPE_OFFSCREEN,
		HARM_TYPE_SWORD
	}, 
	{
		--[HARM_TYPE_JUMP]=10,
		--[HARM_TYPE_FROMBELOW]=10,
		[HARM_TYPE_NPC]=10,
		[HARM_TYPE_PROJECTILE_USED]=10,
		--[HARM_TYPE_LAVA]={id=13, xoffset=0.5, xoffsetBack = 0, yoffset=1, yoffsetBack = 1.5},
		--[HARM_TYPE_HELD]=10,
		[HARM_TYPE_TAIL]=10,
		--[HARM_TYPE_SPINJUMP]=10,
		--[HARM_TYPE_OFFSCREEN]=10,
		[HARM_TYPE_SWORD]=10,
	}
);

--Custom local definitions below


--Register events
function GiantTorpedoTed.onInitAPI()
	npcManager.registerEvent(npcID, GiantTorpedoTed, "onTickNPC")
	--npcManager.registerEvent(npcID, GiantTorpedoTed, "onTickEndNPC")
	--npcManager.registerEvent(npcID, GiantTorpedoTed, "onDrawNPC")
	registerEvent(GiantTorpedoTed, "onNPCHarm")
end

function GiantTorpedoTed.onTickNPC(v)
	--Don't act during time freeze
	if Defines.levelFreeze then return end
	
	local data = v.data
	
	--If despawned
	if v.despawnTimer <= 0 then
		--Reset our properties, if necessary
		data.initialized = false
		return
	end

	--Initialize
	if not data.initialized then
		--Initialize necessary data.
		data.initialized = true
	end

	--Depending on the NPC, these checks must be handled differently
	if v:mem(0x12C, FIELD_WORD) > 0    --Grabbed
	or v:mem(0x136, FIELD_BOOL)        --Thrown
	or v:mem(0x138, FIELD_WORD) > 0    --Contained within
	then
		--Handling
	end
	
	--Execute main AI. This template just jumps when it touches the ground.
	v.speedX = v.direction * 2.5
	if Colliders.collide(v, player) then
		Explosion.spawn(v.x, v.y, 3)
		v:kill()
	end
end

function GiantTorpedoTed.onNPCHarm(v)
	    Explosion.spawn(v.x, v.y, 3)
		v:kill()
end

--Gotta return the library table!
return GiantTorpedoTed
Image

deice
Rocky Wrench
Rocky Wrench
Posts: 638
Joined: Fri Jul 23, 2021 7:35 am

Re: Explosion.spawn doesn't work

Postby deice » Wed Apr 06, 2022 3:54 pm

first, your signature for onNPCHarm() is incorrect. second, the logic in general is a bit redundant.
if you want the npc to explode when killed in any fashion while also exploding on collision with the player, it's better to do the following:

1. change onNPCHarm() into onNPCKill():

Code: Select all

registerEvent(GiantTorpedoTed, "onNPCKill")
2. change the collision checking loop so that it simply kills the npc and nothing more:

Code: Select all

if Colliders.collide(v, player) then
	v:kill()
end
3. move the explosion logic into onNPCKill() and change the signature:

Code: Select all

function GiantTorpedoTed.onNPCKill(eventToken, harmedNpc)
	Explosion.spawn(harmedNpc.x, harmedNpc.y, 3)
end


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari