Microsoft MakeCode

Recreating the while block

(reposting to the microbit community)
Is there a way I can make my own while block?
I want my extension to have a while
Block that behaves exactly like the default while block in loops. If there is a way to take the already existing while block and change its name and shadow that also would be great.
Example:
Screenshot 2021-09-18 220922

You might take a look at this post: Creating an multi handler function in an extension - #2 by E-EnerG-Gamecentral

Here’s my answer from there, which might actually fit:

This may not solve your problems, but the MakeCode playground is a great tool to explore the block styles and prototype blocks. See: https://makecode.com/playground .

About the closest example is has is the in-line event handler.

You may want to follow the link and:

  1. Switch to the micro:bit view (The “Editor” drop down menu)
  2. Pick “Events” in the Sample menu
  3. Click Run
  4. Select the Puzzle Piece palette to see what the examples look like. The “on an inline event” looks like a pretty good fit. It can be modified to have a parameter.

That being said, I’m not sure how easy/hard it is to get the behavior you want. I suspect it isn’t too bad.

Bill

Hi, thank you very much for the reply.
Unfortunately the problem is not making the block itself, but rather the problem is if I make the block, it takes boolean as a parameter and I **can’t recheck the condition (the condition returns the at first it will always be true). Is there a way to fix this?

Sorry. I didn’t really think about an expression vs. a value.

This may be something to ask about via the issues on the pxt github. If there’s no way to do this now, I suspect they could support new syntax for these blocks that would convert expressions to lambdas. For example, a normal value-based block may have parameters like:

   export function onEventAsStatement(value: boolean, handler: () => void) {
        if(value) {
            handler();
        }
    }

And one that accepts a lambda expressions may look like:

   export function onEventAsStatement(value: boolean=>boolean, handler: () => void) {
        while(value) {
            handler();
        }
    }

The expressions created in blocks would either be converted to a value or a lambda based on the parameter type. Parameters of the style TYPE=>TYPE would be treated as a lambda with the TYPE as the expected expression type. Any ()=>void expressions would be code blocks (maybe support multiple code blocks too for if-else style behavior). Of course, there may be a bunch of problems with this approach that I’m not considering…

Bill