"Program Error: Stack Overflow" with no identifiable loops or arrays

I’m working on a game with a system where your character is thrown into a map to select levels, where you can press A to enter into one when the character is on top. However, I wanted to have a little preview of the level that appears on top, displaying the level’s name, collectibles, and a prompt to press A.

But, for whatever reason, when i reach the point where this happens the game almost immediately pops up with an error saying “Program Error: Stack Overflow”. From what i can find, the most common causes for these are recursion and large arrays, but the snippet of code causing this error has no loops or arrays, from what i can tell.

Here is the main culprit for the error(Converted to Javascript):
scene.onOverlapTile(SpriteKind.Player, assets.tileTutorial, function (sprite, location) {
Level_Preview.setText(“AISGXUFkuyg_PlaceHolderText_fdgsdfs”)
Level_Preview_Background.setPosition(Santa.x, Santa.y - 12)
characterAnimations.setCharacterState(Level_Preview_Background, characterAnimations.rule(Predicate.Moving))
timer.after(100, function () {
if (!(Santa.tileKindAt(TileDirection.Center, assets.tileTutorial))) {
characterAnimations.setCharacterState(Level_Preview_Background, characterAnimations.rule(Predicate.NotMoving))
}
})
})
Can anybody tell why this is happening? If so, can anybody think of a better way to do this?
Any help is greatly appreciated!

Could you share a link to the full game? That will make it a bit easier to debug.

From looking at the code you’ve got there, I’m guessing it has something to do with the timer.after being inside the overlap event. The overlap event fires once per frame (at 30 FPS) so that will be spawning 30 threads a second.

1 Like

I figured out a better approach without the timer a it seemed to have solved the problem!

Although one curious thing is that after i ADDED code but before i fixed it , it stopped giving the error? It would noticeably slow down and after a while practically froze, but would never give an error.

Well anyways here’s my fix(the game’s in blocks but this is easier to share):

scene.onOverlapTile(SpriteKind.Player, assets.tile`Tutorial`, function (sprite, location) {
    if (Santa.tileKindAt(TileDirection.Center, assets.tile`Tutorial`)) {
        Level_Preview_Background.setFlag(SpriteFlag.Invisible, false)
        Level_Preview.setText("AISGXUFkuyg_PlaceHolderText_fdgsdfs")
        Level_Preview_Background.setPosition(Santa.x, Santa.y - 12)
        characterAnimations.setCharacterState(Level_Preview_Background, characterAnimations.rule(Predicate.Moving))
    } else {
        characterAnimations.setCharacterState(Level_Preview_Background, characterAnimations.rule(Predicate.NotMoving))
    }
})

(If you’re wondering about entering the stage, i put that as a separate On A button pressed block so A presses carrying over from within the stage wouldn’t immediately make you go back.)