Discuss Scratch

jeffhannan
Scratcher
5 posts

Initializing variables and lists

Hi, in scratch projects I see variables initialized. Without running the project, I click on the variable and it has a non-zero value.

I can't find any info on how to initialize them, outside of the script itself. Is there a way?

Likewise, how would you initialize lists, as in this page describing how to use lists as arrays:
https://wiki.scratch.mit.edu/wiki/Array

Thanks
Jeff
Uniquename1
Scratcher
100+ posts

Initializing variables and lists

It will remain on the last value of the variable when you restart so if it needs to be zero at the start set it to zero at the start. Also sometimes people set them to not zero at the start ie set lives to 3 or something.

I don't use lists but I think you'd use

add [] to [list v]
or
insert [] at (1 v) of [list v]
monstermash3
Scratcher
1000+ posts

Initializing variables and lists

when gf clicked
set [variable v] to [initial value]
or
when gf clicked
delete (all v) of [list v]
set [index v] to [1]
repeat (length of [initial list value v])//"initial list value" list should have the initial list contents
add (item (index) of [initial list value v]) to [list v]
change [index v] by (1)
end
footsocktoe
Scratcher
1000+ posts

Initializing variables and lists

jeffhannan wrote:

Hi, in scratch projects I see variables initialized. Without running the project, I click on the variable and it has a non-zero value.

I can't find any info on how to initialize them, outside of the script itself. Is there a way?

Likewise, how would you initialize lists, as in this page describing how to use lists as arrays:
https://wiki.scratch.mit.edu/wiki/Array

Thanks
Jeff

You only initialize lists that must be empty when the project starts.
If the list will always be the same for every run of the project (for example, a color palette or a list of locations that are always the same) then you do not initialize the list.
jeffhannan
Scratcher
5 posts

Initializing variables and lists

monstermash3 wrote:

when gf clicked
set [variable v] to [initial value]
or
when gf clicked
delete (all v) of [list v]
set [index v] to [1]
repeat (length of [initial list value v])//"initial list value" list should have the initial list contents
add (item (index) of [initial list value v]) to [list v]
change [index v] by (1)
end

Thanks. But, where does the “initial list value” list get set?
jeffhannan
Scratcher
5 posts

Initializing variables and lists

footsocktoe wrote:

You only initialize lists that must be empty when the project starts.
If the list will always be the same for every run of the project (for example, a color palette or a list of locations that are always the same) then you do not initialize the list.

Thanks. That is what I want, some constant data. But where does that get set?
footsocktoe
Scratcher
1000+ posts

Initializing variables and lists

jeffhannan wrote:

footsocktoe wrote:

You only initialize lists that must be empty when the project starts.
If the list will always be the same for every run of the project (for example, a color palette or a list of locations that are always the same) then you do not initialize the list.

Thanks. That is what I want, some constant data. But where does that get set?

When you create the project.
You create a list in the DATA section of blocks.
Then you can load it manually one item at a time or you can write code that loads it.
Once the list is created, it will be in the project forever, unless you put a “delete that list” block in the project.

Powered by DjangoBB