Page 1 of 1

My New code in Progresss

Posted: Thu Jan 06, 2022 3:22 pm
by SuperAlex
Hi, I'm working on a level where pressing alt run clones an NPC but I have a problem when the NPC dies out of the camera the alt run button doesn't work.
Image
Here's the Code:

Code: Select all

local cloningdespawn = true
local advent = false
adventimer = 0
function onKeyDown(keycode)
	if keycode == KEY_RUN and cloningdespawn == true then
        local clonation = NPC.spawn(19,player.x + 0.5 * player.width, player.y + 0.5 * player.height + 8, player.section, false, true)
        clonation.speedX = 1 * player.direction
        SFX.play(9)
		advent = true
    end

    if cloningdespawn == false and keycode == KEY_RUN then
        for _,eso in ipairs(NPC.get(19, player.section)) do
            eso:kill(8)
			advent = false
        end
    end
end
function onTick()
	if advent == true then
		adventimer = adventimer + 1
		if adventimer > 2 then
			adventimer = 0
			cloningdespawn = false
		end
	else
		adventimer = adventimer + 1
		if adventimer > 2 then
			adventimer = 0
			cloningdespawn = true
		end
	end
end
function onNPCKill(eventObj, killedNPC, killReason)
	if killedNPC.id == 19 then
		cloningdespawn = true
	end
end


Ummm I need a bit of help pls

Re: My New code in Progresss

Posted: Sat Jan 08, 2022 9:52 am
by deice
this is because the npc doesn't actually die in that gif when it goes off camera. smbx keeps an internal timer for each npc when it goes off camera, and when around 3 seconds pass, only then does the npc actually despawn (as far as i know, this even applies to npcs that leave the level boundary though someone may be able to correct me there). to circumvent this you could just make them despawn the moment they're out of sight, though this would restrict the setups you could actually make, so it's probably better to check their y coordinate to see if they've fallen into a pit specifically, and then just manually despawn them.

(also i believe the reason it doesn't manually die when you re-press alt run is that the NPC.get() call doesn't return npcs outside the section boundaries)