Array type automatically switches to generic type when I move from Javascript to block code

(I accidentally deleted my last post from before while trying to see how to edit it to add the ‘help’ tag sorry)

so I need the array in a parameter of a function to be specifically an ‘any’, which I specify it in Javascript

function interaction (dialog: any[]) {

but every time I move over to block-code, it automatically changes it back to the generic array type, which causes errors leading me to pause, go back to javascript, and test it over there

function interaction (dialog: Array[]) {

is there any workaround to this problem or am I just “SOL” as some may say :sweat_smile:

2 Likes

@Freastt can you share your code?

the type inference performed by our blockly compiler isn’t always the best where arrays are concerned. sometimes you can trick it into working though

3 Likes

no problemo, here’s a link to the current game as it is right now https://arcade.makecode.com/S37861-42218-62759-21058

additionally, here’s also the specific function where the error is occuring

function interaction (dialog: Array[]) {
    while (!(DialogActive)) {
        timer.background(function () {
            DialogActive = true
        })
        controller.moveSprite(char, 0, 0)
        music.play(music.createSong(assets.song`mySong`), music.PlaybackMode.InBackground)
        TheBox = sprites.create(assets.image`logbox`, SpriteKind.Player)
        animation.runImageAnimation(
        TheBox,
        assets.animation`speechbubble`,
        150,
        true
        )
        TheBox.setFlag(SpriteFlag.RelativeToCamera, true)
        TheBox.setPosition(80, 140)
        spriteutils.moveTo(TheBox, spriteutils.pos(80, 98), 250, true)
        TheBox.z = 2
        char1 = sprites.create(assets.image`c1`, SpriteKind.Player)
        animation.runImageAnimation(
        char1,
        FreastLog.Char1(dialog[0]),
        200,
        true
        )
        char1.setFlag(SpriteFlag.RelativeToCamera, true)
        char1.setPosition(-60, 54)
        spriteutils.moveTo(char1, spriteutils.pos(24, 54), 250, false)
        char2 = sprites.create(assets.image`c0`, SpriteKind.Player)
        animation.runImageAnimation(
        char2,
        FreastLog.Char2(dialog[0]),
        200,
        true
        )
        char2.setFlag(SpriteFlag.RelativeToCamera, true)
        char2.setPosition(200, 54)
        spriteutils.moveTo(char2, spriteutils.pos(136, 54), 250, true)
        NamePlate = sprites.create(assets.image`CharBox`, SpriteKind.Player)
        animation.runImageAnimation(
        NamePlate,
        assets.animation`namePlate`,
        150,
        true
        )
        NamePlate.setPosition(32, 76)
        NamePlate.setFlag(SpriteFlag.RelativeToCamera, true)
        nameText = textsprite.create("", 0, 13)
        nameText.setFlag(SpriteFlag.RelativeToCamera, true)
        nameText.setPosition(7, 75)
        nameText.z = 4
        NamePlate.z = 3
        char2.z = 1
        char1.z = 1
        story.setSoundEnabled(true)
        for (let index = 0; index <= dialog.length - 1; index++) {
            if (FreastLog.Priority(dialog[index]) == 0) {
                char1.setScale(1.05, ScaleAnchor.Middle)
                char1.y = 48
                char2.setScale(0.8, ScaleAnchor.Middle)
                char2.y = 54
            } else {
                char2.setScale(1.05, ScaleAnchor.Middle)
                char2.y = 48
                char1.setScale(0.8, ScaleAnchor.Middle)
                char1.y = 54
            }
            animation.runImageAnimation(
            char1,
            FreastLog.Char1(dialog[index]),
            200,
            true
            )
            animation.runImageAnimation(
            char2,
            FreastLog.Char2(dialog[index]),
            200,
            true
            )
            nameText.setText(FreastLog.Name(dialog[index]))
            story.printDialog(FreastLog.Dialog(dialog[index]), 80, 100, 40, 150, 6, 0, story.TextSpeed.VeryFast)
        }
        sprites.destroy(nameText)
        sprites.destroy(NamePlate)
        controller.moveSprite(char, 100, 100)
        spriteutils.moveTo(char1, spriteutils.pos(0, 60), 100, true)
        sprites.destroy(char1)
        spriteutils.moveTo(char2, spriteutils.pos(160, 60), 100, true)
        sprites.destroy(char2)
        spriteutils.moveTo(TheBox, spriteutils.pos(80, 140), 100, true)
        sprites.destroy(TheBox)
        TheBox.z = 0
        timer.background(function () {
            pauseUntil(() => spriteutils.isDestroyed(TheBox))
            DialogActive = false
        })
    }
}
1 Like

@Freastt i think the easiest fix here would be to change your FreastLog.Log function to return a custom type instead of an away of multiple types as you’re doing now.

for example:

namespace FreastLog {
    export class LogEntry {
        constructor(
                public name: string,
                public dialog: string,
                public char1: Image[],
                public char2: Image[],
                public priority: number
        ) {}
    }


    //% block    
    export function Log(name:  string, dialog: string, c1: Array<Image>, c2: Array<Image>, p: number): LogEntry 
    {
        return new LogEntry(name, dialog, c1, c2, p);
    }

    //% block
    export function Name(dialogue: LogEntry): string {
        return dialogue.name
    }

    //% block
    export function Dialog(dialogue: LogEntry): string {
        return dialogue.dialog
    }

    //% block
    export function Char1(dialogue: LogEntry): Image[] {
        return dialogue.char1
    }

    //% block
    export function Char2(dialogue: LogEntry): Image[] {
        return dialogue.char2;
    }

    //% block
    export function Priority(dialogue: LogEntry): number {
        return dialogue.priority;
    }
}

then change the type of your other function to LogEntry[] instead of any[]