Page 1 of 1

NPC acts strangely when going offscreen

Posted: Mon Aug 18, 2025 9:57 am
by BockyBlock
Hi! I'm currently porting an NPC from a game called Garbanzo Quest, with some code copied from the Terissa NPC. In the original game, they continue moving when offscreen. However, in my SMBX2 implementation, they return to spawn once they go offscreen. To make things even more confusing, if they go offscreen while their spawnpoint is ONscreen, they render but DON'T have a hitbox.

https://file.garden/ZqehsES-MSMCnVGd/do ... Sphere.zip
Let me know if this ^ zip is missing anything. I thiiiink it's got everything it needs?

PS: This is a bit of a spaghetti code. If you want to make other changes to it, besides fixing the problem I have, I'd be okay with that. I'd also like the gray one (the one that shoots projectiles) to stop making noise if it's offscreen, but I'll cross that bridge when I come to it.

Good luck!

Re: NPC acts strangely when going offscreen

Posted: Mon Aug 18, 2025 12:19 pm
by deice
first off, you should know that something despawning when it goes off screen long enough is standard smbx behavior. the reason for the oddities is that there's no code in the lua file that handles the npc despawning gracefully

i'm not 100% sure of all the behaviors you need for the npc, but adding the following code should fix the issues you're having
first, add this in sphero.lua before the isHidden check in the onTickNPC function:

Code: Select all

if(v.despawnTimer == 1) then
    for _, p in ipairs(Player.get()) do
        if(v.section == p.section) then
            v.despawnTimer = 2
        end
    end
end
	
if(v.despawnTimer <= 0) then
    data.initialized = false
    return
end
this prevents the npc from despawning as long as there's a player in the same section as itself, and reinitializes its data in the event that it does despawn

then, also in sphero.lua, add another check to the onDrawNPC early return, making it look like this:

Code: Select all

if (v.isHidden) or data.hidden or v.despawnTimer <= 0 then return end

Re: NPC acts strangely when going offscreen

Posted: Mon Aug 18, 2025 1:16 pm
by BockyBlock
Thanks a ton! It worked! I guess I completely forgot about how DespawnTimer is vanilla behavior