Accessing variable values by name via a reporter in code ?

If I have a set of vars with any kind of values t1=10, t2=true, t3=abc,
is there a way to have reporter function that will return the value of the var if I input the name of the var "in text" into the function.
reporter function (t1) <<< t1 is in text format, not the brown var name icon
returns 10

Or alternatively, is there a way all the vars of the program can be accessed in a "sensing reporter" type function; just like "my costumes", "my clones", "my ______", etc

I need to use these vars as codes in a project embedded in text. Even though I know it would be easier to use a list, it will not suit me for practical reasons.

You can use a dictionary. Try this project: I think this is the only way that you can do it without using JavaScript and I don't know enough JavaScript.

Hi @tguneysu,

You can use


and then, build your own block to work with vars calling their name.


@ donotforgetmycode_sn
I am going to play with this. See if I can make it work.
It is simple, but creates a step of modifying the dictionary to synch with the vars.

@ jguille2
Sorry Joan, I am lost trying to understand what you mean.
Is the call block supposed to be used "with inputs t1 t2 t3" ?
And having nothing selected in the reporter block " __ of Sprite" produces an error.

Hi @tguneysu,
I guess you have forgot to ringify the "_ of_" block.
Test it, and then, you'll get what you had requested.

For example, building this block:


You can do...



@ jguille2
Thanks Joan ! This is perfect!
Definitely worth putting it in the manual.

I think you can also use a join block, rather than a call block. It is much simpler to use.
untitled script pic (7)

It's simpler. I like it :slight_smile:

Thanks for all this help guys.
Now if it only was possible to do this also with the local variables !!!
I know, I know - I'm asking for too much.

It is possible with local variables. Try it

I did ! Did not work.
Do locals have a different way of specifying the name?

Hi Turgut,

here's a quick custom block you can make using a JS-function that - I think - works correctly with scopes, and also reports script variables:

Sorry, I got confused with the words "global" and "local". That was my fault

That's not a dictionary. Dictionary is built into javascript, and should be built in to Snap! It has constant time lookup.
use ((name) of (my [self v])) where name is a reporter, such as a custom block argument or a one input join.

I've noticed that script variables are actually called the name and then after that, (temporary)

@ jens
Good thing about this version is it works with BOTH global and local vars. I like it.

I am using Jen's suggested JS code to access variable values by name:

And it works great for both global and local vars.

However, if I use this within a MAP block within my custom block, then none of the custom block parameters or the script vars declared within the custom block are reachable. Outside the MAP, all is fine!

Does MAP create its own realm somehow?

And how does one use the parameters of the custom block in a MAP within the block via this method?

Here are my blocks as I was testing them:




you can access script variables using the block I've shown you:

but accesssing formal parameters would have to be done by invoking the JS directly in the script, i.e. without wrapping another block around it:

I'm not sure I understand what you're trying to accomplish, introspection?

I know you are busy with the rewrite and I am sorry to bother you.

As an answer to the last reply, the example you provided are placed on coding area and they work the same with me.

However, my example is using the "value of __" JS code you provided in my custom block. There, neither script vars, nor the custom block parameters are accessible. Of course I might be doing something wrong. But, given that the same code works when I take it out of my custom block and place it on the coding area, there must be another issue.

Here again is my custom block:

My objective is:

  1. access custom block parameters t1 and t2 by name
  2. access custom block script variables m1 etc by name

As I said above, this works on the coding page without the custom block header, but not under my custom block definition.

Here is a trace run (with ERROR):
both JS calls (outside the MAP, and inside the MAP) give errors.

Same code on coding page without the custom block header (NO ERROR):

As far as why am I doing this:

  • I have pieces of text that have embedded formatting codes, eg: t1, t2, t3, c1, c2, c3, etc. that have a prefix character to tell me they are format codes (eg: ~).
  • I am using the MULTILINE text block to input these, then SPLIT to turn them into a list by LINE, then MAP to process each line.
  • On each line, as I encounter the codes mentioned, I look up the values by name (since they are in text format) that correspond to them and action it accordingly.

The example I have provided above is not the real code, but just an example to demo the problem encountered. So it probably makes no sense.

Anyway, many thanks for your attention in spite of your busy schedule.