Why does my extension block not work?

Yes, I made a custom font extension. Well, almost. This block doesn’t work -

This block is supposed to set an image to a string in a particular font.

Here is the code for that block -

//%block="set $output=variables_get(picture) to $input"
export function SetImage(output: Image, input: string) {
    let heig = 0
    let widt = 0
    let curwidt = 0

    for (let currentletter = 0; currentletter <= input.length; currentletter++) {

        if (!(ligs.indexOf(input.charAt(currentletter)) == -1)) {
            heig = Math.max(heig, ligages[(ligs.indexOf(input.charAt(currentletter)))].height)

    for (let currentletter2 = 0; currentletter2 <= input.length; currentletter2++) {
        if (!(ligs.indexOf(input.charAt(currentletter2)) == -1)) {
            widt += ligages[(ligs.indexOf(input.charAt(currentletter2)))].width
        } else if (input.charAt(currentletter2) == " ") {
            widt += 3*letterspace
        widt += 1

    output = image.create(widt, heig)
    for (let currentletter3 = 0; currentletter3 <= input.length; currentletter3++) {

        if (!(ligs.indexOf(input.charAt(currentletter3)) == -1)) {
            drawTransparentImage(ligages[(ligs.indexOf(input.charAt(currentletter3)))], output, curwidt, 3)
            curwidt += letterspace
            curwidt += ligages[(ligs.indexOf(input.charAt(currentletter3)))].width


Can someone explain why it doesn’t work? @richard, was it you who made the text sprite extension? I used some code from there, that might be where it went wrong.

1 Like

Actually that extension was written by @darzu but I’m happy to help! Can you send the rest of the code?

Oh, the link is at the top!

1 Like

First, I think you want to use < and not <= in those for-loops. The main issue though is that you are creating a new image and drawing to that. The sprite is still pointing to the old image, so it won’t update. Either you should update your function to draw to the original image (which may be the wrong size) or you should change your function to return an image instead of taking one in. Then the user could use it like this:

1 Like

To clarify the variable point, it can help to think of variables as a sort of box. What’s inside the box in this case is a pointer to an Image. You can think of a pointer as an address where the image is stored.

The sprite has a box with the image pointer inside it, and when you call this function you’re creating a new box called “output” and putting the same pointer inside of it as the one in the sprite’s box.

When you write output = image.create(widt, heig), you are putting a new pointer in the output box, but the sprite’s box (which is separate) isn’t changing.

Hopefully that’s helpful! I think I got lost in my own metaphor at some point.

1 Like

Thanks, it worked! :grin:

I’ll be posting the finished thing soon!