Help with declaring variables



I've done some testing, and I figured out the var_declare function will only make script vars outside of blocks. I've tried changing the context of the script, but it only works when changing the context of the var block.
untitled script pic (3)
Although the "this script" block needs to be a sprite.

If anyone can help I'm looking for a way to declare variables using a block, but can function in another block.

The extension function is made to be used within a wrapper block, like in the create variables library.

This allows script variables to be created in the script that called the create [script V] [{var}] @delInput @verticalEllipsis @addInput :: variables block.

This is the intended way to use this function. If you wish to create variables within scripts, use the "create variables" library, or create a block that contains the extension function, and don't directly use the extension function. Plus, if you're creating script variables you're better off just using the script variables ((a)) @addInput block.

Yes, I would recommend that you avoid using the extension block directly, and instead us it in the context that it was used in the library. If at all possible you should just import the library you want and keep the blocks that you need rather than fiddling with the undocumented extension block.

No, what i'm saying is that doesn't work

Try
untitled script pic (4)

The library uses extension blocks
untitled script pic (5)
so doesn't make much of a difference

if i run it to see what variables there are
untitled script pic (6)
untitled script pic (7)

you can see that block just returns global var

Could you present an example application? So forum contributors may help you find a solution for your envisaged application(s).

From another topic
but that won't work inside blocks

Turns out things are a little strange, since this works.

untitled script pic - 2024-10-08T150510.469

I can see why this happens. The variable blocks know what context the variables are supposed to be in, before the script even gets ran. The create [script V] var [{var}] @delInput @verticalEllipsis @addInput :: variables block doesn't define the variables before script gets ran (like the script variables ((a)) @addInput block, or any upvars do), so snap doesn't know that a variable with that name will be created, so the block assumes it's supposed to be a global var. If you use the (var [ v] :: variables) block from the "create variables" library, it gets the variable at runtime, so it first checks if the variable name exists in the scope it was called in (unlike the variable block).

Of course I am just making an educated guess, I don't actually know how snap handles variable blocks, I'm just using what it looks like is happening to make this assumption.

Cool
But making a block with a list-input-upvar (aka slot type 112)
since you need to self declare variables

Sorry about pulling out other topics, but I've been trying to understand this for a long time.

but such blocks can not be used in other blocks

this means you can make a functioning block, but it doesn't work in ALL circumstances.

now as for the block
random blocks script pic
I was able to make a work around


which I was able to do with the help of @ego-lay_atman-bay

but that requires me to add ring vars

The difference there, is that the script in the c-slot gets called after the variables are defined, whereas in the original post, the variables were being defined in the same script as the variable blocks.

That's what I'm looking for, to define the variables with out a c-shape.

"blcok" :rooster::chicken::poultry_leg::hatched_chick::poultry_leg::hatching_chick::rooster::hatching_chick:

haha that typo is hilarious

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.