All Lists and Variables Interaction

I need help on making blocks such as making, renaming, clone, or delete actual lists and variables. I need this for my projects.


So, first thing, Snap! is different from Scratch in the way we handle lists. In Scratch, a list is a special kind of thing, with its own name, that can be used only in the red list blocks. But in Snap! lists are just data, like numbers, and can be used anywhere. You can SAY a list, for example, or use one as an item in another list, etc.

So, in particular, you can SET a variable to a list value. That's how you give a list a name, the same as giving a string a name by putting it in a variable.

This means that most of the things in your set of needed blocks reduce to doing those things to any variable. Making a list is typically done with the LIST reporter, but renaming a list is just SETting a different variable to it.

Similarly, you can't rename a variable as such, but you can SET new TO old, so "new" will be another name for the value of "old."

There's no such thing as cloning a variable or a list. For most values, you don't need a new copy, because they're immutable: you can't change their value. So if you say


it puts a 5 in X, but it doesn't change the value of 4!

Lists are different; they are mutable. So it does make sense to talk about making a copy of a list, and the easiest way to do that is KEEP ITEMS <( ) = ( )> FROM my-list. This makes a new list containing all the items from the original list that are equal to themselves, i.e., all of them.

As in any modern language, there's no such thing as deleting anything. If nothing points to a value (a list, string, costume, whatever) then its memory will be reclaimed. If you want your program to forget about a list that's the value of some variable, just give the variable a different value, or use a script variable, which exists only while its script is running.

Hope that helps.

Well, thank you. But I was looking for help on making blocks like deleting variables that you created (with the var block library), then the script will create and delete it.

Oh, I see. Just for globals, I think, since script variables disappear by themselves when the script finishes. I agree, that should be in the library. I'll get to it soon.

Thank you.

While we are on the topic, can there be a way to read a variable reporter, like this:
That would be absolutely amazing!
I know you guys are probably totally overloaded with work, but you can still add it to the TO-DO list.

Oh yeah huh, variables would be more useful if you can see what's in them... Will do. Maybe on the airplane if I can't sleep. :~)

Doesn't seem to work for sprite local variables:

Nor script vars:


I haven't yet figured out how to access local and script vars with the JS function block.

Maybe, at least for now, I should rename "variable" to "global variable".

Great! That will be really useful. Thanks!

I found out how to access local variables with the JS function block.

this is a'good news

Funny, I tried that, but it didn't work.

How did you try it?

It's OK, I realised I didn't actually try it. I tried something similar.

@bromagosa should make this a library. It would be quite useful for other people to use.

@bh is the one in charge of libraries, I believe.

Yeah, I'll take care of this when I get back from Heidelberg (flying tomorrow).

I'm especially in charge of this because I started a library for dynamically named variables, but just for the record, Jens makes libraries all the time and he doesn't ask me for permission. :~)