# Create new blocks comparable to for-loops+functions?

**URL:** https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320
**Category:** micro:bit
**Created:** [June 5, 2020, 12:57pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320 "2020-06-05T12:57:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [June 5, 2020, 12:57pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320/1 "2020-06-05T12:57:40Z")

</div>

Hi,

Is there any way to make blocks that are somewhat like for-loops combined with functions: they can be placed in-line with other blocks (like for-loops) and can have parameter values that can be used (like functions). I’d something somewhat like Python’s `with` statement, that allow variables to be used in a certain context. After looking at [https://makecode.com/playground](https://makecode.com/playground) I haven’t seen a way to create for-loop-like blocks.

Some background: I want an easy way to allow someone to access all the fields of an object. Currently I’m working on an object based approach that requires “getting” an instance and then passing it to different getter-style methods to extract instance variables. I’d really like a single clamp-style block that gets the object and provides access to a consistent set of all instance variables. (I’m also open to suggestions for other approaches)

Bill

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [June 5, 2020, 3:46pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320/2 "2020-06-05T15:46:11Z")

</div>

Like this?

![Screen Shot 2020-06-05 at 8.44.59 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/9f503fc5e9dcb289a867f7f91e63aaa9c1ce5ea4.png)

Here’s the definition:

```typescript
//% block="$instance with $num $bool"
//% instance.shadow=variables_get
//% instance.defl=sprite
//% draggableParameters=variable
//% handlerStatement=1
export function exampleHandler(instance: Sprite, handler: (num: number, bool: boolean) => void) {

}

```

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [June 5, 2020, 4:28pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320/3 "2020-06-05T16:28:39Z")

</div>

I think that’s exactly what I was looking for (but I’ll have to review/test a little more). Thanks!!!

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [June 5, 2020, 4:36pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320/4 "2020-06-05T16:36:47Z")

</div>

Quick follow-up: I didn’t see any documentation on this. Is there any? (Or any quick tips…In the absence of info I’ll try to look through the source code)

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [June 5, 2020, 4:43pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320/5 "2020-06-05T16:43:48Z")

</div>

I’m afraid the only docs are the ones [here](https://makecode.com/defining-blocks) and they are a little spotty. All the options are defined in the code [here](https://github.com/microsoft/pxt/blob/master/localtypings/pxtarget.d.ts#L563) but it’s not always obvious what they actually _do_. Sorry, this is definitely a part of our documentation that needs improvement!

If you ever get stuck, probably best to just ask on the forum like you did 🙂

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [June 5, 2020, 4:59pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320/6 "2020-06-05T16:59:41Z")

</div>

Since you opened the door to questions… 🙂

I’m assuming that in this case the Sprite object could be provided (the `$instance` field), but that `.defl=...` would use a default sprite variable…Is that correct?

And what’s the `instance.shadow` about? Getters?

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [June 5, 2020, 5:40pm UTC](https://forum.makecode.com/t/create-new-blocks-comparable-to-for-loops-functions/2320/7 "2020-06-05T17:40:14Z")

</div>

The `instance.shadow` is the block ID of the default block that gets put in that input. In this case, we want a variable block (with a variable dropdown), which happens to have the ID `variables_get`.

`instance.defl` defines the default value for that block. If this was a number, I could put a number in there and that’s what would show up in the toolbox. For variables, it sets the name of the variable to “sprite” in this case. The default-default for variables is the name “item”
