Variable scope in nested clips

I know this has already been discussed but somehow I still dont get it completely.

I read this post

and made this test
variable_confusion.wick (1.7 KB)

variable foo is defined inside clip “clipA” as a property of the clip object
this.foo=“I am foo”;
How is I can see it from project level ?
mytext.setText(foo); // ???
I expected to have to write
mytext.setText(clipA.foo);

also how do I target a parent variable/property from inside a child clip?
let’s say I am in clipB that is inside clipA
How do I access clipA.foo ?

variables set in default scripts have different scope (or should I say context) from variables set inside clip frames?

1 Like
  • I think something important to note is that the “this.foo” variable in the first frame of your clipA object is binding that variable to the frame, not the object. In this case, “this” refers to the frame itself. That’s why clipA.foo isn’t working - it’s searching for a foo variable attached to the clip, but the foo variable is actually attached to the clip’s frame. There is probably a way to get a variable from a frame. If I find a solution I’ll let you know, but for now I’d suggest binding your variable to the clip itself rather than the frame it’s on. For example, if you go to the default script of clipA and say this.foo=“I am foo”, that will bind the variable to the clip instead of the frame.

  • It is possible to target parent variables by specifying the whole path from the project level. For example, if you created clipB inside of clipA, and you want it to reference a variable of clipA, you would have to say project.clipA.
    Example: copying text from clipA to clipB, from within clipB:

this.testo.setText(project.clipA.testo.textContent);

I would do this.parentClip.foo

1 Like

Come to think of it, that would be the simpler option for a parent clip (I completely overlooked that, thanks for pointing it out!)

thankyou for the fast reply

1 Like

made a test variables_scope.wick (2.0 KB)

quad clip contains circle clip
quad sets foo variable to “I am foo” in default script
from circle i try to retrieve foo value with this.parentClip.foo —> undefined

what’s wrong?
also another thing that puzzle me
if in circle script I write
console.log(parent.width) i obtain quad width !!
while this.parent.width does not work
How is it?
why this.parentClip.width does not work ??

in the code you wrote:

foo="I am foo";

so it defines the variable foo in the scope of that script, not on the object itself. So when you try to get the value foo of that clip object, since it wasn’t even defined in the first place it returns undefined.


Did you write this.parent.width or this.parentClip.width? Because those are two different things. For a Clip, the direct parent of the clip should be a Frame. So when you use this.parent you are talking about the frame the clip is contained in, so it wouldn’t work. But if you write this.parentClip, then you are talking about the clip the clip is contained in, so it would work. The variable parent would refer to this.parentClip so that would also work.

1 Like

thankyou so much pumpkinhead for taking the trouble to answer me!

yes I get it now
setting this.foo=… in square let circle retrieve the variable using parentClip.foo !