Sim error: calling non-function at inline (line 102)

Hi! This code returns an error when run inside of a function. Whatever i do i cannot seem to fix it. Maybe someone out there knows? The code has to be run inside of a function, and runs fine if the function is removed.

wormGame()

function wormGame() {

    basic.clearScreen()
    enum Dir { Up = 0, Down, Left, Right }
    let gameSpeed = 800
    let gameRunning = true
    let currentDirection: Dir
    currentDirection = Dir.Right
    let currentX = 0
    let currentY = 2
    let dotX: number = 0
    let dotY: number = 0
    let score = 0
    let snakePartsX: number[]
    snakePartsX = [0]
    let snakePartsY: number[]
    snakePartsY = [2]
    led.plot(currentX, currentY)
    getNewDot()


    loops.everyInterval(gameSpeed, () => {
        if (gameRunning) {
            let nextX = currentX
            let nextY = currentY
            switch (currentDirection) {
                case Dir.Up:
                    nextY--; break
                case Dir.Right:
                    nextX++; break
                case Dir.Down:
                    nextY++; break
                case Dir.Left:
                    nextX--; break
            }
            if (!hitDot(nextX, nextY) && led.point(nextX, nextY)) {
                loseGame()
            } else {
                currentX = nextX
                currentY = nextY
                if (currentX == 5) {
                    currentX = 0
                }
                if (currentY == 5) {
                    currentY = 0
                }
                if (currentX == -1) {
                    currentX = 4
                }
                if (currentY == -1) {
                    currentY = 4
                }
                led.plot(currentX, currentY)
                snakePartsX.push(currentX);
                snakePartsY.push(currentY);
                if (hitDot(currentX, currentY)) {
                    score++
                    getNewDot()
                    if (score % 1 == 0 && gameSpeed > 500) {
                        gameSpeed = gameSpeed - 25
                    } else {
                        dropTail()
                    }
                } else {
                    dropTail()
                }
            }
        }
        basic.pause(gameSpeed)
    })

    input.onButtonPressed(Button.A, () => {
        switch (currentDirection) {
            case Dir.Up:
                currentDirection = Dir.Left; break
            case Dir.Right:
                currentDirection = Dir.Up; break
            case Dir.Down:
                currentDirection = Dir.Right; break
            case Dir.Left:
                currentDirection = Dir.Down; break
        }
    })

    input.onButtonPressed(Button.B, () => {
        switch (currentDirection) {
            case Dir.Up:
                currentDirection = Dir.Right; break
            case Dir.Right:
                currentDirection = Dir.Down; break
            case Dir.Down:
                currentDirection = Dir.Left; break
            case Dir.Left:
                currentDirection = Dir.Up; break
        }
    })
    function getNewDot() {
        let pointTail = getNotLit()
        dotX = pointTail.X;
        dotY = pointTail.Y;
        //led.plot(dotX, dotY)
        led.plotBrightness(dotX, dotY, 100)
    }
    function getNotLit(): { X: number, Y: number } {
        let x = randint(0, 4)
        let y = randint(0, 4)
        while (led.point(x, y)) {
            x = randint(0, 4)
            y = randint(0, 4)
        }
        return { X: x, Y: y }
    }
    function hitDot(x: number, y: number) {
        return x == dotX && y == dotY
    }
    function dropTail() {
        led.unplot(snakePartsX[0], snakePartsY[0])
        snakePartsX.shift()
        snakePartsY.shift()
    }
    function loseGame() {
        basic.showAnimation(`
        ..... ..... ..... ..... #####
        ..... ..... ..... ##### ##### 
        ..... ..... ##### ##### ##### 
        ..... ##### ##### ##### ##### 
        ##### ##### ##### ##### ##### 
        `, 80)
        basic.pause(300)
        basic.showNumber(score, 100)
        basic.pause(2000)
        control.reset()
    }

}

I can’t really explain the problem, but I suspect it may be an error or limitation in the simulator due to the nesting. A variation that doesn’t nest all the code in a function works ok. For example, when the bulk of the code is at the global level rather than being contained in wormGame, like: https://makecode.microbit.org/S60303-34112-87667-51937 .