What is the difference between use $ and % when defining parameters for blocks?

I was looking through the code for the Sprites tab and I noticed that when defining parameters for blocks, sometimes a percentage sign (%) is used, but other times a dollar sign ($) is used. I was just wondering why that is. Is there any difference between them? If so, what is it? I’ve just used the dollar sign for everything, so I was wonder if I’m doing something wrong.

Example of what I’m talking about:
Percent sign (%):

/**
     * Sets the sprite velocity in pixel / sec
     * @param vx
     * @param vy
     */
    //% group="Physics"
    //% weight=100
    //% blockId=spritesetvel block="set %sprite(mySprite) velocity to vx %vx vy %vy"
    //% help=sprites/sprite/set-velocity
    //% vx.shadow=spriteSpeedPicker
    //% vy.shadow=spriteSpeedPicker
    setVelocity(vx: number, vy: number): void {
        this.vx = vx;
        this.vy = vy;
    }

Dollar sign ($):

/**
     * Get the tile kind in a given direction if any
     * @param direction
     */
    //% blockId=spritetileat block="tile to $direction of $this(mySprite) is $tile"
    //% tile.shadow=tileset_tile_picker
    //% blockNamespace="scene" group="Locations" blockGap=8
    //% help=scene/tile-kind-at
    //% weight=20
    tileKindAt(direction: TileDirection, tile: Image): boolean {
        const tilemap = game.currentScene().tileMap;
        let x = this.x >> tilemap.scale;
        let y = this.y >> tilemap.scale;
        switch (direction) {
            case TileDirection.Top:
                y = y - 1;
                break;
            case TileDirection.Bottom:
                y = y + 1;
                break;
            case TileDirection.Left:
                x = x - 1;
                break;
            case TileDirection.Right:
                x = x + 1;
                break;
            case TileDirection.Center:
            default:
                break;
        }
        return tiles.getTileImage(tilemap.getTile(x, y)).equals(tile);
    }

I’m also curious to know that this means:

%sprite(mySprite) or $this(mySprite)

Any help is appreciated.

@ARandomGuyOnMakeCode good question!

The % syntax in block definitions is the old way of doing things. Some of our older APIs still use this syntax, but going forward you should always use $. We can’t update the old APIs to use the new syntax because of how our localization works

Similarly, the $this(mySprite) syntax is the old way of setting parameter defaults. It’s functionally equivalent to this.defl=mySprite.

We changed the syntax for two reasons:

Firstly, the old % syntax was entirely based on the order of the parameters in the function definition, you couldn’t reorder them for the block. The $ syntax bases everything off of the name of the parameters, so you can put them in whatever order you want. This is especially useful for some languages where grammar is very different from English and things need to be moved around to read more naturally.

Secondly, the old syntax was much easier for translators to accidentally mess up when they were translating block text. By moving things like default values out of the text itself, there’s less things to go wrong when translating everything.

tldr: don’t use % or param(default) in block definitions. Use $ and param.defl=default instead

2 Likes

Thank you for the information! :slight_smile::+1: