Discuss Scratch

UndertaleGuy63
Scratcher
100 posts

For This Sprite Only

What is the difference in making a variable for all sprites and making a variable for this sprite only?
Just_Iakov
Scratcher
100+ posts

For This Sprite Only

All sprites means any sprites (including clones) can control this variable.

An example where for all sprite variables can be used at:

when I start as a clone
forever
if <touching [bullet] ?> then
change [score] by (1) //for all sprites
delete this clone
end
end

This script will update the score variable globally whenever a clone is destroyed.

An example where for this sprite only variables can be used at:

when I start as a clone
set [MyHealth] to [10] //for this sprite only

Now we'll add in this script:

when I start as a clone
forever
if <touching [bullet] ?> then
change [MyHealth] by (-1) //for this sprite only
end
end

*Let's just assume you have another script that deletes the bullet upon collision with the clone.

Now the clone will change its MyHealth variable by -1 whenever it touches the bullet. Since the variable is for this sprite only it would only affect its own health (not other clones).


if the health variable is (for all sprites):

Since the health variable is being used for all sprites, every clone would share the same value.

if the health variable is (for this sprite only):

Since the health variable is being used privately for each individual clone, it won't affect the other clone's health variable. It would just affect its own health variable's value.

But be aware, any variables that are for this sprite only are private, which means other blocks cannot detect them.


([MyHealth] of [Sprite1 v])
This block won't be able to detect each individual clone's health (as they are private variables), so you'll have to use list to do so.

There are of course more examples and uses, but for now this is just the basic breakdown.

I hope this helps.

Last edited by Just_Iakov (Oct. 9, 2021 04:28:17)

UndertaleGuy63
Scratcher
100 posts

For This Sprite Only

Thanks!
deck26
Scratcher
1000+ posts

For This Sprite Only

You can also use local variables so different sprite can use the same script without affecting each other - eg you could copy qa script changing health for spriteA to spriteB and as long as the variable is local each is only changing its own value. So it's not just useful for clones.

It can also help make your project easier to manage as when you're working on spriteA you only see the global variables and local variables belonging to the that sprite. If you have a lot of variables this can make it easier to find the one you want in drop down lists etc.
Copira
Scratcher
25 posts

For This Sprite Only

At least in my non professional use it's nice for organization. If you have 5 people being attacked and have 5 global variables called “Health” it would be complicated to navigate There are more professional uses for it (seen above this post) but I hope that gave an answer to your question and have a great day

Powered by DjangoBB