My New code in Progresss

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

Moderator: Userbase Moderators

SuperAlex
2025 Egg Hunter
2025 Egg Hunter
Posts: 226
Joined: Mon Nov 02, 2020 3:02 pm
Flair: Spriting
Pronouns: He/Him
Contact:

My New code in Progresss

Postby SuperAlex » Thu Jan 06, 2022 3:22 pm

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

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

Re: My New code in Progresss

Postby deice » Sat Jan 08, 2022 9:52 am

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)


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 5 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari