Discuss Scratch

nikoliden
Scratcher
5 posts

Setting variable not working

I can't figure out what is going on here. I was having a problem with a more complex code, but I made a copy of it and simplified it down to this very simple script. Can anyone tell me why it isn't setting the variable “path” correctly to 5?

https://scratch.mit.edu/projects/296783888/
codeman1044
Scratcher
1000+ posts

Setting variable not working

So you are using a variable in a way you don't want to. Ironically, what's happening is something I was trying to make happen about a week ago, but couldn't figure out how.
anyways, the answer is this: local variables (ones that are assigned to “this sprite only”) make clone copies of themselves when clones of that sprite are made. What this means is that when the clone is made, if it tries to affect the local variable, it will only affect it's own clone copy. This is really useful for when you are making multiple clones of a sprite. The way to fix this, to make clones affect actual variables, is to just keep it as a global variable (for all sprites), or just make the actual sprite affect the variable.
Hope I helped! If something doesn't make sense, just ask.

Last edited by codeman1044 (March 24, 2019 21:22:34)

-Rex-
Scratcher
500+ posts

Setting variable not working

This has to do with the way local variables (i.e., variables marked with “For this sprite only”) work. When a clone is created, the local variables from its “parent” sprite are copied to the clone's local variables, which only the clone can access. This means that each sprite AND clone has its own local variables. When the clone sets the local variable, it only changes its own instance/copy of the variable, and not the parent sprite's instance. What's being shown on stage is the sprite's instance of the local variable, which the clone can't change.
nikoliden
Scratcher
5 posts

Setting variable not working

So I don't actually want a global variable, it is supposed to be a local variable (I want each clone to have its own version).

You're saying that when I use “show” within a clone, it's not showing me the clone's local instance? How can I display what the local version is?
Since I'm assigning the variable within a clone and displaying it within a clone, I would think it would display the local version, no?
nikoliden
Scratcher
5 posts

Setting variable not working

And as a follow-up question, for debugging, it's useful to see what the local (clone) versions of variables are so that you can figure out what the close is doing… is there no way to do that if the command “Show variable” doesn't display the non-clone version?
nikoliden
Scratcher
5 posts

Setting variable not working

BTW, in case the link changes, here's the jist of the code issue:
whenIstartasaclone
setpathto5
showvariablepath

It's odd to me that the behavior here is that “path” can be set within a clone, but displaying it displays a different version of the variable.
-Rex-
Scratcher
500+ posts

Setting variable not working

Yes, the show/hide variable blocks can only show the sprite's copy of a local variable. To accomplish what you would want, you could either set a global variable to the local variable value (as you have done), or use a say/think block under the Looks category.
deck26
Scratcher
1000+ posts

Setting variable not working

Or use a cloneID variable as the index for a list and each clone can change its own value there.
nikoliden
Scratcher
5 posts

Setting variable not working

fixed it
qwerty116
Scratcher
100+ posts

Setting variable not working

Mine just basically skips the block.


UAC

whenclickedsetto
Nambase_Test
Scratcher
35 posts

Setting variable not working

qwerty116 wrote:

Mine just basically skips the block.


UAC

whenclickedsetto

Hi, please don't necropost on old topics - will report this topic to be closed to prevent future necroposts.

Powered by DjangoBB