Method Parameters in Blocks?

I’m using @richard’s Improved Sprite Data extension to define methods local to sprites (A.K.A. “Sprite Functions”). These are quite limited because they do not accept parameters, meaning everything I want to pass to the function must be set in a sprite data variable (or worse, a global variable). This creates two issues:

  1. All sprite function parameters are in the same namespace, causing messy code and potential name collisions

  2. More function parameters requires more lines (blocks?) of code in order to call the function. For example, a function with four parameters would require five blocks: Four to set each variable, and one to call the function. Below is a real example from a project I’m working on. The “FadeToPalette” method has two parameters.

    image

A global function would only require one block to do the same thing:

Is there an extension or another obvious solution to this problem?

(I suppose I could just use JavaScript instead of blocks…)

4 Likes

Nope afaik

I forgot to mention, but it would also be very useful for sprite functions to be capable of returning a value.

I did some googling and I’m pretty sure sprite “functions” aren’t even functions since they can’t return values, and they’re actually “nullary procedures”

2 Likes

in theory I could make a quick extension with a “set (mySprite) data (“func”) to (…)”.

Its always makes me happy seeing somebody else use my own extension… :smiling_face_with_tear:

what does that mean, “nullary procedures”? I’m pretty sure what you found wasn’t makecode or something they/Ai didn’t understand, but im positive that it’s possible.

i was also wandering if you could return values from then or give them parameters. it the only reason im not using them.

Also would be great if block-objects could have methods too, i thought about a veery interesting thing that would be possible with that.

1 Like

is it possible to use block-objects as parameters for functions? im reworking my particle system to use them instead of nested arrays :face_vomiting: :face_vomiting: :face_vomiting: and i would want “physics(particle)” function but i cant because its a block-object so i have to do “physics(array, index)”

1 Like

You can use a hacky solution function Bob (block object:array){

Console.log(block object.get value at(0) }

So you use an array right block object param and then for an input you use an array with 1 value which is the block object and then you use get value at 0 to access it

1 Like

It’s not specific to makecode, I found a stackoverflow answer saying that functions without arguments are “nullary” and another saying that functions that don’t return are really procedures. It is a real term but nobody seems to agree on whether procedures and functions are the same thing.

Maybe I should just call them “functions without arguments that don’t return” because that actually makes sense.

1 Like

done! https://github.com/c-minusminus/better-sprite-data

This took roughly 20 minutes to make. It should be simple to use. There is also a slight issue with the run (the parameter is defaulted to a variable and not an array)

For help, if you copy-paste the code in the test.ts in my extension to the js editor in your project, you can see how to use it in blocks.

3 Likes

Wow thanks so much! This probably wont be much usefull fot my dungeon crawler but i have a lot of other uses for that!

Thanks, this will make everything way easier from now on!

If it’s trivial to add, return values would be really nice. Regardless, this extension will still be really useful in reducing the number of variables I need to store in sprites!

Fixed the [run (item) data (" ") with …), so you should update the extension.

1 Like

Wow yeah… nested arrays… so lame am I right :sweat_smile: /s

1 Like

there lame with you do myArray[x + y * w + z * w * h] or similar

1 Like