Microsoft MakeCode

I need help developing an extension

So I was snooping around in the editor to see if I could find an avg function, but I can’t seem to find it, neither in JavaScript nor Blocks. So after that, I decided to make it into an extension, but can anyone who has made extensions help me? This is what I have so far…

namespace Math {
    //% block="avg $value"
    export function avg(value: Array) {
let temporaryValue = 0
for (let index = 0; index < value.length; index++){
temporaryValue += index
}
return (temporaryValue / value.length)
    }

Is there a better way to do this? I’ve hit this error with the playground editor:

Generic type ‘Array’ requires 1 or more argument(s)

Thanks

1 Like

Great extension idea! The error you’re running into is that value: Array should be value: number[]. Arrays are represented by square brackets, and the “type” of the array is written before the brackets, so as another example, an array of sprites would be value: Sprite[].

1 Like

Another Questions, how do I make subcategories? Such as the life cycle and effect found in the sprites tab. Also here is the extension so far:

And how can I make the input be a sprite kind?

You can define those with group attributes https://makecode.com/defining-blocks#category-groups

here’s a quick example from one of mine; setting the groups here: https://github.com/jwunderl/pxt-status-bar/blob/efcd376357ae7215e65616a11283e03bceaa7fa2/status-bar.ts#L329 and setting which group a block belongs to here https://github.com/jwunderl/pxt-status-bar/blob/efcd376357ae7215e65616a11283e03bceaa7fa2/status-bar.ts#L871

For spritekind it’d be something like this:

//% blockId=allOfKind block="array of sprites of kind $kind"
//% kind.shadow="spritekind"
export function allOfKind(kind: number): Sprite[] {
    ...

(spritekind is the blockid of the block that makes the dropdown of spritekinds, and shadow says 'make tthe thing here start as ‘spritekind’ / have that stay around even if they drag something else on top of it)

1 Like

So now I am trying to make it output a true/false statement,

//% blockId=allOfKind block=“does sprite kind $kind exists”
//% kind.shadow=“spritekind”
export function allOfKind(kind: number): Sprite[] {
let temporaryVar = 0
for(let value of Extras.arrayOfAllSprites()){
temporaryVar += 1
}
if(temporaryVar > 0){
return True
}
else{
return False
}
}

It gives me:

Cannot find name ‘True’

The same thing for false
can you help me @jwunderl

In javascript true / false is all lowercase

Now it says True is not assignable to type ‘Sprite[]’

Do you know how I can fix this?

//% blockId=allOfKind block=“does sprite kind $kind exists”
//% kind.shadow=“spritekind”
//% group=“SpriteKind”
export function allOfKind(kind: number): Sprite[] {
let temporaryVar = 0
for(let value of Extras.arrayOfAllSprites()){
temporaryVar += 1
}
if(temporaryVar > 0){
return true
}else{
return false
}
}

The return type of the function is wrong - its saying its returning an array of sprites (: Sprite[]) where you’re trying to return a boolean value. Just swap Sprite[] with boolean.

Also, you’ll need to change the blockId (and probably the function name too to give it something meaningful) - the example I gave was mostly copied from code in the editor by default, leaving it as allOfKind will conflict with the default sprite block.

1 Like

Another Question, Why does this happen:

Check the extension