Discuss Scratch
- DrGaming_92Games
-
100+ posts
Local Variables
For me there have been a handful of times when I needed to make a variable for just one area of a script, increasing clutter and making things generally harder to understand. I suggest adding local variables to Scratch, here's an example of what this block might look like:
This would allow the use of the local variable, but only in the scope of the block. If it were to be used somewhere where the variable hadn't been defined, it would simply return 0 or false, the same way custom reporter blocks do.
The way this variable would be changed would be something like this:
The option for the local variable in the drop-down would only appear if the block was in a scope where the local variable has already been defined. Additionally, to make a local variable there could be an additional box that can be checked that says something like “local variable”. There should also be the option to define multiple local variables at once in a similar way to how parameters are added to custom blocks. And if you want to add or remove a variable you can right click the block and select “edit” to add or remove variables.
Here is an example of how this would be used:
The “charArray” list would contain each individual character of the “word” parameter as a separate item.
If you have any ideas for how to tweak this, please do post saying what you would change and if I agree with it I will edit the OP (this post). Also, please be constructive, if you don't understand what I'm talking about go here.
Create variable (Example Variable :: variables) {
} :: variables
This would allow the use of the local variable, but only in the scope of the block. If it were to be used somewhere where the variable hadn't been defined, it would simply return 0 or false, the same way custom reporter blocks do.
The way this variable would be changed would be something like this:
Create variable (local variable :: variables){
set [local variable v] to [2]
change [local variable v] by [3]
} :: variables
The option for the local variable in the drop-down would only appear if the block was in a scope where the local variable has already been defined. Additionally, to make a local variable there could be an additional box that can be checked that says something like “local variable”. There should also be the option to define multiple local variables at once in a similar way to how parameters are added to custom blocks. And if you want to add or remove a variable you can right click the block and select “edit” to add or remove variables.
Here is an example of how this would be used:
define toCharArray (word)
Create variable (character :: variables){
repeat (length of (word :: custom))
change [character v] by (1)
add (letter (character) of (word :: custom)) to [charArray v]
} :: variables
The “charArray” list would contain each individual character of the “word” parameter as a separate item.
If you have any ideas for how to tweak this, please do post saying what you would change and if I agree with it I will edit the OP (this post). Also, please be constructive, if you don't understand what I'm talking about go here.
Last edited by DrGaming_92Games (March 1, 2020 08:02:46)
- Nambaseking01
-
1000+ posts
Local Variables
Just two quick questions, because I'm not very good with this stuff.
So, when you make a local variable and the definition block is under an event, then the variable would work for the whole script, right?
Also, when you put the definition for the local variable in a loop or custom block, then it would only work in there, right?
So, when you make a local variable and the definition block is under an event, then the variable would work for the whole script, right?
Also, when you put the definition for the local variable in a loop or custom block, then it would only work in there, right?
- DrGaming_92Games
-
100+ posts
Local Variables
So, when you make a local variable and the definition block is under an event, then the variable would work for the whole script, right?
No, it would only work within the c-block:
when this sprite clicked
Create local variable (clicked :: variables){
set [clicked v] to [1]
} :: variables
if <(clicked) = [1]> then
change [Money v] by (1)
end
This wouldn't work because it is outside of the scope so “clicked” would return 0. Just simply put the c-block around the entire script if you're going to use it throughout a script:
when this sprite clicked
Create local variable (clicked :: variables){
set [clicked v] to [1]
if <(clicked) = [1]> then
change [Money v] by (1)
end
} :: variables
This would work because it is within the c-block where “clicked” is defined.
Also, when you put the definition for the local variable in a loop or custom block, then it would work in there, right?
Yes, as long as all the scripts are within the c-block (examples so I know I understood what you said):
Create local variable (spins :: variables){
repeat (5)
change [spins v] by (1)
end
say (join (join [You spun ] (spins)) [ times!]) for (2) secs
} :: variables
define toCharArray (word :: custom)
Create local variable (character :: variables){
repeat (length of (word :: custom))
change [character v] by (1)
add (letter (character) of (word :: custom)) to [charArray v]
} :: variables
In the first example the sprite would say, “You spun 5 times!” and in the second example the list “charArray” would contain each character of the word parameter as a separate item. Hope I answered your questions.
EDIT: I'll add an example of use in the OP
Last edited by DrGaming_92Games (Feb. 29, 2020 20:28:08)
- Maximouse
-
1000+ posts
Local Variables
Yes, this would be very useful because it reduces the amount of global and sprite variables.
Maybe it should allow defining multiple variables at once, like this:
Alternative block (like Snap's script variables):
Maybe it should allow defining multiple variables at once, like this:
create local variables (a) (b) (c) (+ :: #b0b0b0) {
...
} :: variables
Alternative block (like Snap's script variables):
create local variables (a) (b) (c) (+ :: #b0b0b0) :: variables
...
- DrGaming_92Games
-
100+ posts
Local Variables
Yes, this would be very useful because it reduces the amount of global and sprite variables.
Maybe it should allow defining multiple variables at once, like this:create local variables (a) (b) (c) (+ :: #b0b0b0) {
...
} :: variables
Alternative block (like Snap's script variables):create local variables (a) (b) (c) (+ :: #b0b0b0) :: variables
...
Yes I agree with the idea defining multiple local variables at once, but since no other scratchblock has a button on the block to morph the block, perhaps when creating the variable you would get the option to add more variables the same way Scratch's custom blocks allow you to add more parameters?
EDIT: I'll add the idea of defining multiple local variables to the OP
Last edited by DrGaming_92Games (Feb. 29, 2020 20:26:30)
- Maximouse
-
1000+ posts
Local Variables
Yes I agree with the idea defining multiple local variables at once, but since no other scratchblock has a button on the block to morph the block, perhaps when creating the variable you would get the option to add more variables the same way Scratch's custom blocks allow you to add more parameters?
Yes, this would be OK. I just don't want this:
create local variable (a) {
create local variable (b) {
create local variable (c) {
...
} :: variables
} :: variables
} :: variables
Another idea: create local variable is quite long, why not just create variable(s)? For example
create variables (a) (b) (c) {
} :: variables
create variables (a) (b) (c) :: variables
- DrGaming_92Games
-
100+ posts
Local Variables
Yes I agree with the idea defining multiple local variables at once, but since no other scratchblock has a button on the block to morph the block, perhaps when creating the variable you would get the option to add more variables the same way Scratch's custom blocks allow you to add more parameters?
Yes, this would be OK. I just don't want this:create local variable (a) {
create local variable (b) {
create local variable (c) {
...
} :: variables
} :: variables
} :: variables
Another idea: create local variable is quite long, why not just create variable(s)? For examplecreate variables (a) (b) (c) {
} :: variables
create variables (a) (b) (c) :: variables
Yes, “create local variable(s)” is quite long, especially with a variable name next to it, I'll change it, thank you!
Last edited by DrGaming_92Games (March 1, 2020 08:03:03)
- Nambaseking01
-
1000+ posts
Local Variables
Thanks for the explanation!
Anyways, support, I know a lot of programming languages with local variable definers, and since Scratch is a beginner programming language, it could be pretty useful.
Anyways, support, I know a lot of programming languages with local variable definers, and since Scratch is a beginner programming language, it could be pretty useful.
- --Explosion--
-
1000+ posts
Local Variables
Okay, I like this suggestion but this is pretty much what lists are for…
- minor-edit
-
500+ posts
Local Variables
Here are some Workarounds for script-local variables
- DrGaming_92Games
-
100+ posts
Local Variables
Okay, I like this suggestion but this is pretty much what lists are for…
Well I guess you could use lists, but would it not be simpler to just have the ability to make local variables?
- DrGaming_92Games
-
100+ posts
Local Variables
Workarounds for script-local variablesHere are some
I'm not suggesting this because there aren't any workarounds, but rather because it would lessen the amount of variables I have in a project
- DrGaming_92Games
-
100+ posts
Local Variables
bump, and also I thought of a problem: visually telling the difference between local variables and normal variables. I have 2 ideas for this, either make the local variables a lighter orange, or add a little icon next to it the same way cloud variables do (obviously with a different symbol). I guess this could just be ignored, but this seems like something that may need improvement.
Thoughts?
Thoughts?
- Maximouse
-
1000+ posts
Local Variables
bump, and also I thought of a problem: visually telling the difference between local variables and normal variables. I have 2 ideas for this, either make the local variables a lighter orange, or add a little icon next to it the same way cloud variables do (obviously with a different symbol). I guess this could just be ignored, but this seems like something that may need improvement.
Thoughts?
I would suggest a location icon like in Snap!:

Lighter orange would be OK too. Like this:
(normal variable :: #ff8c1a)
(local variable :: #ffb366)
Last edited by Maximouse (March 3, 2020 09:08:14)
- StevenTheSquare
-
100+ posts
Local Variables
Hm yes I can see this being useful for certain games. support
Last edited by StevenTheSquare (March 5, 2020 02:26:21)
- Maximouse
-
1000+ posts
Local Variables
What about local lists?
create variables (local variable :: #ffb366) (local list :: list) {
set [local variable v] to (10)
add (local variable) to [local list v]
} :: variables