Making an extension for every JavaScript exclusive feature I can think of!

yeah(I wish the added nestable functions toggleable not sure if blockly supports tho)

1 Like

Yes, but be careful with that because every time it runs it’s creating a new update loop that will run with the previous one created still running, which is probably why they didn’t give us this in the normal blocks…

3 Likes

So would it be more helpful if you put an if statement in there first which you can change any time if you dont want the loop to keep going?

game.onPaint(function) uses game.currentScene().eventContext.registerFrameHandler(scene.UPDATE_PRIORITY, function) which returns a FrameCallback, and using that FrameCallback you can do ... .unregisterFrameHandler(FrameCallback) and it will stop updating. The only issue is being able to make it, I was thinking of a setUpdate(function) and a registerFunction(function): FrameCallback, but if you have any other ideas in mind that would work better I would like to see them!

(game.onPaint is defined here so you don’t need to find it yourself)

1 Like

Yeah that’s probably something that the devs would have to code in and that’s probably not gonna happen because really those nestable blocks are very hard to understand for new programmers and I can imagine them causing a lot of lag issues if, say, someone put a onUpdate inside a Forever, causing thousands of updates to happen… yeah that wouldn’t be a good thing for learning coders to have. Normally in JavaScript you can assign them to variables and then destroy them, which is extremely convenient, but I didn’t put in enough thought when making those blocks to create a system for that.

3 Likes

Heyyyy don’t go around spreading secrets like this. We don’t want them gasp switching to JavaScript or anything like that!

Anyways, I think it’s quite funny that I mentioned this exact thing in my next post. Soooo annoying that yours got approved first, though, because I can’t have anyone thinking the great MakeCode Scientist learned something from someone else. :winking_face_with_tongue:

Anyways now that I’m thinking about it, I would probably make a block like the insert but then also give it like a “name” field and then make another block like “remove update loop with name (name)” or something. I think that I would make this its own extension though…

3 Likes

how do you “destroy” variables just set them to undefined/null and then force garbage collection?

1 Like

to expand on the setUpdate and registerFunction, this is what it could look like:

namespace MyNamespace {
    let func: () => void
    
    //% block="..."
    export function setFunc(handler: () => void) { func = handler }

    //% block="..."
    export function onUpdate(): control.FrameCallback { return game.eventContext().registerFrameHandler(scene.UPDATE_PRIORITY, func) }

    //% block="..."
    export function onPaint(): scene.Renderable { return scene.createRenderable(scene.ON_PAINT_Z, func) }
}
1 Like

Is it possible that you could add enumerations into your extension?

2 Likes

imo that’s on them and it should be in advanced to deter protect them

1 Like

Hey uh woofwoof is it possible for you to add game.popScene and game.pushScene or smth like that
edit: Here is the code!!! still don’t get the push and pop handler nonsense please make a long paragraph explaining it please
:folded_hands:

namespace game {
    //%block="push scene"
    export function push_scene(){
        game.pushScene()
    }
    //%block="pop scene"
    export function pop_scene(){
        game.popScene
    }
}
2 Likes

No, sorry. This has been requested in various places across the forum and currently there’s no special block code that would let a block programmer create their own enum. You can represent an enum in blocks, and many drop down menus and such are actually enums behind the scenes, and I know there’s a way (that I haven’t really used or tried to understand myself) that you can let block coders add new values to your enums, because the SpriteKind menu is an enum that has this ability, but as far as I know there’s no way for the user to create new ones. I bet you could use the spritekinds menu as an enum though… I would test this but I’m in bed and writing on my phone, so I’ll test that in the morning if I remember.

2 Likes

Ok I was wrong and actually after writing the entire following post I tried it in MakeCode and game.onUpdate does not actually return its own object, so deleting specifically onGameUpdate loops is not possible in the way I’m about to describe. I actually usually do this with the scene.createRenderable() function, which I use to render things to the screen every frame, and which does work how I describe. In practice, I rarely use forever() or game.onUpdate() functions in my code, so yeah.

No, the updates have their own destroy function that can be run on the object the function returns. The functions are saved in two* places essentially. When they are created using the game.onUpdate() function, they are saved to an array that is used internally to actually run the updates, and then the update event object that was created is also returned from the function, so that you can put it into a variable if you want.

So basically you could do something like
let myUpdateLoop = game.onUpdate(…) and then you would run something like myUpdateLoop.destroy() to remove it, and that’s a special function that probably just removes it from the internal updates array and stuff.

Because of that internal array, simply setting myUpdateLoop to undefined is this case would not remove all the references to the object, so garbage collection wouldn’t remove it.

*The object is saved in one place and then returned, so you can choose to save yourself if you want to use it in the future but you don’t have to.

2 Likes

I could have sworn I added these…

2 Likes

SpriteKind is a namespace, not an enum

1 Like

what game.onUpdate uses returns the object, aka game.eventContext().registerFrameHandler(scene.UPDATE_PRIORITY, func)

1 Like

Ok they should be added now, along with some other requested blocks including the unfortunately few array functions I could add @AlexK and I think a typeof block someone requested that I must have just forgot to commit. I still could have sworn that I added those blocks, so maybe something happened and somehow I ended up with an old version of the project maybe?? I don’t know. I’m going to look at that later.

If you update the extension and hover over the new blocks (bottom of the Scene category) there is a short explanation of what they do, and longer explanations I believe I have done before… uhhhh… I guess not… I will do this soon then because they are quite cool!

2 Likes

Actually it’s kinda hard to use in blocks cause you have to nest functions for it to would better im pretty sure

1 Like

oh great exalted woofwoof wrong? Such treasonous thoughts!!! also why no game.onPaint? so you can use z ??? Next I thought you meant delete the variable entirely not just the loop lol.

1 Like

No it’s a enum I’m darn sure of that