Create a New Variable with a Script?

Is it possible to create a new variable with a script?

Script variables block

Yes, as @donotforgetmycode_sn have said you can create script variables

Also, you have create variables in program library to create global and sprite variables.

Joan

I'm thinking about linking my varBlocks project again, but I've already done that several times...

Hi @snapenilk,

Your only problem is using JS blocks.
You are doing
errorJS1
but Snap! do this as
errJS2

That's all,

Joan

I didn't think that mattered.

Thanks so much for calling my attention to the "Create Global Variables" block in the Snap! external libraries. That was exactly what I needed. Many thanks!

I created a global variable using the create "Global Variable" block.

 Global Variables [Tune]

In the next line of the script, I would like to set the global variable Tune to the contents of Note List:

 Set [Tune] to [Note List]

Is there a way to assign a value to a variable in a script after the variable has been created in a script?

Thanks again for guiding me to an example or documentation.

Hi @glenbull ,

I guess your problem is the new variable isn't on the dropdown options of Set, is it?

Then, you can

settingNewVar

Joan

That is exactly what I needed. I created a Player Piano with a keyboard that allows users to create a series of notes. I wanted to allow users to save a tune that they created to a variable name (e.g., "Smoke on the Water," etc.).

This procedure requests a name for the list of notes and saves the list to a variable:

image

Thanks for the guidance on this!

Here's a link to the prototype:

Hey @glenbull,
I made some more blocks for this some time ago:

Thanks so much for sharing these blocks.

Cool project :+1::+1:
How about adding a button that lets you switch between different octaves. :slight_smile:

Adding a button to switch octaves is a great idea! I just added this feature to the player piano. Thanks for the suggestion.

Also, just to clarify, the way I figured out how to do this using JS was by using Developer Mode to inspect the set and add variable boxes. They do pretty much what the source of the library's blocks do, call a special method (stored in the 'selector' attribute). The ones in question being
doSetVar / setVar
addVariable

Where?

Hi @r4356th,

Just search create variables in program library :

createVarsLib

That's all,

Joan

Thank you so much!