Discuss Scratch

Flump_Bump
Scratcher
43 posts

Question about clones and variables

Hey, I was wondering if you can make a variable that changes for a specific clone, not for all of the clones. For example, I'm trying to make a life simulator where each creature is a clone. Each clone has an age and health. The variable is different for each clone. If there is a way to make this work, please teach me how. Thanks!
super_crazy
Scratcher
100+ posts

Question about clones and variables

Clones make a copy of the original sprite completely. This means that private variables are unique to each clone. If you made the “age” and “health” variable private, then the data for every creature (clone) will be different.

The next problem is to change the private variable. If you do a broadcast to change the variable (see below), all the clones will receive the broadcast and change the variable.
broadcast [decrease health v] // this is broadcasted to all clones

when I receive [decrease health v]
change [health v] by (-10) // this changes the variable
One way is to give every clone its own “id” variable and only change the variable if the “id” equals “the id of the desired clone”.
Another solution is to check if the clone should decrease its health and change the variable inside the “When I start as a clone” block.
when I start as a clone
set [age v] to [ 1]
set [health v] to [ 100] // You could also set it before you make the clone
repeat until <[health] = [100]> // Just an example
if <touching [enemy sprite v] ?> then
change [health v] by [-10] // This will only change THIS clones variable if THIS clone is "touching a sprite"
wait (1) secs // Makes sure the clone doesn't die instantly
end
more code :: grey
end
delete this clone
I hope this helped you understand clones and their variables
If you still don't understand please ask!
drmcw
Scratcher
1000+ posts

Question about clones and variables

Just to add to what super_crazy has said. When they say “private” they mean that when you create a variable you need to check the “for this sprite only” option. The variable that is then created, health and age for example, can then be changed independently for each clone; which is what you want.
Flump_Bump
Scratcher
43 posts

Question about clones and variables

super_crazy wrote:

Clones make a copy of the original sprite completely. This means that private variables are unique to each clone. If you made the “age” and “health” variable private, then the data for every creature (clone) will be different.

The next problem is to change the private variable. If you do a broadcast to change the variable (see below), all the clones will receive the broadcast and change the variable.
broadcast [decrease health v] // this is broadcasted to all clones

when I receive [decrease health v]
change [health v] by (-10) // this changes the variable
One way is to give every clone its own “id” variable and only change the variable if the “id” equals “the id of the desired clone”.
Another solution is to check if the clone should decrease its health and change the variable inside the “When I start as a clone” block.
when I start as a clone
set [age v] to [ 1]
set [health v] to [ 100] // You could also set it before you make the clone
repeat until <[health] = [100]> // Just an example
if <touching [enemy sprite v] ?> then
change [health v] by [-10] // This will only change THIS clones variable if THIS clone is "touching a sprite"
wait (1) secs // Makes sure the clone doesn't die instantly
end
more code :: grey
end
delete this clone
I hope this helped you understand clones and their variables
If you still don't understand please ask!
That really helped! Thanks a bunch!

when green flag clicked
forever

if <<(current [month v]) = [11 ]> and <<(current [day v]) = [26 ]> >> then
say [Happy Thanksgiving!]

end

end

Last edited by Flump_Bump (Nov. 26, 2015 13:34:05)

Powered by DjangoBB