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

**URL:** <https://forum.makecode.com/t/program-error-stack-overflow-with-no-identifiable-loops-or-arrays/16859>\
**Category:** Help\
**Created:** [December 12, 2022, 5:41pm UTC](https://forum.makecode.com/t/program-error-stack-overflow-with-no-identifiable-loops-or-arrays/16859 "2022-12-12T17:41:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JorginLegal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jorginlegal/32/7262_2.png) [@JorginLegal](https://forum.makecode.com/u/JorginLegal)\
**Post date:** [December 12, 2022, 5:41pm UTC](https://forum.makecode.com/t/program-error-stack-overflow-with-no-identifiable-loops-or-arrays/16859/1 "2022-12-12T17:41:09Z")

</div>

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.tile`Tutorial`, 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.tile`Tutorial`))) {  
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!

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [December 12, 2022, 5:47pm UTC](https://forum.makecode.com/t/program-error-stack-overflow-with-no-identifiable-loops-or-arrays/16859/2 "2022-12-12T17:47:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JorginLegal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jorginlegal/32/7262_2.png) [@JorginLegal](https://forum.makecode.com/u/JorginLegal)\
**Post date:** [December 12, 2022, 10:16pm UTC](https://forum.makecode.com/t/program-error-stack-overflow-with-no-identifiable-loops-or-arrays/16859/3 "2022-12-12T22:16:16Z")

</div>

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):

```auto
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.)
