microsoft / pxt-microbit

A Blocks / JavaScript code editor for the micro:bit built on Microsoft MakeCode
https://makecode.microbit.org
Other
720 stars 590 forks source link

want to make global var default of a block, but compiler says no #5857

Open tballmsft opened 2 months ago

tballmsft commented 2 months ago

in microsoft/pxt-arcadeshield we have a global variable screen: Bitmap. I want screen to be the default variable used in blocks, which is accomplished using the bmap.defl line below

//% fixedInstances decompileIndirectFixedInstances
interface Bitmap {
    /**
     * Fill a rectangle
     */
    //% helper=imageFillRect blockNamespace="bitmaps" inlineInputMode="inline" group="Drawing"
    //% block="fill rectangle in %bmap=variables_get at x %x y %y width %w height %h %c=colorindexpicker"
    //% help=github:microsoft/pxt-arcadeshield/reference/bitmaps/bitmap/fill-rect
    //% bmap.defl=screen
    fillRect(x: number, y: number, w: number, h: number, c: color): void;

However, the compiler thinks I am trying to add a new variable screen and does renaming, so I get:

let screen2: Bitmap = null
screen2.fillRect(0, 0, 0, 0, 0)

instead of what I want

screen.fillRect(0, 0, 0, 0, 0)
riknoll commented 2 months ago

hmmm... yeah referencing variables defined in other files is not supported today. i'm also not sure it's such a great idea, what if the user creates their own screen variable? will it override the default screen or will it be pointing to some variable that the user might have no knowledge about?

i think it would be safer to just define a screen() function with a block and use that as the default.