A few weeks ago, I messed around with trying to create global variables in Expressions. I remember adding some code similar to this to a text layer's Source Text property:
$.global.myVar1 = "hello world";
And then in another text layer's Source Text prop, I called that var:
$.global.myVar1;
And that second text layer actually displayed "hello world" in the viewport! But if I reordered the layers, or tried reassigning myVar1's value in the second text layer, all sorts of buggy things would happen. I tried recreating the setup just now, but it won't work at all, and I don't remember what exactly I did the first time. But I do remember it working.
So, has anyone found any useful and predictable behavior for $.global?
Has anyone found a practical use for the $.global object?
Any sort of shared space is incidental, buggy, and shouldn't be relied upon. Don't do this.
- pablohotsauce
- Posts: 10
- Joined: April 13th, 2019, 2:19 pm
But I wanna break stuff 
I figured it probably wasn't useful, but now I'm frustrated that I can't remember what code I used to get a buggy global variable working, a while back. Either the code in my original post is wrong, or it did actually work in 16.0.1, but that behavior got patched out in 16.1.1.

I figured it probably wasn't useful, but now I'm frustrated that I can't remember what code I used to get a buggy global variable working, a while back. Either the code in my original post is wrong, or it did actually work in 16.0.1, but that behavior got patched out in 16.1.1.
Yes there is. You can use it to create global master functions that means you can write one expression and have it control multiple properties and multiple layers, without them simply copying the value. This is incredibly useful for procedural animation where you have scads of layers and you want their properties to be defined by expressions, but if you tweak the expression you don't want to have to copy and paste it to all the affected layers.
More on my blog here: https://blob.pureandapplied.com.au/the- ... r-effects/
More on my blog here: https://blob.pureandapplied.com.au/the- ... r-effects/
- pablohotsauce
- Posts: 10
- Joined: April 13th, 2019, 2:19 pm
Ahh nice, I was close: I just needed to take out ".global" in my earlier code.stib wrote: ↑September 11th, 2019, 1:01 amMore on my blog here: https://blob.pureandapplied.com.au/the- ... r-effects/
BTW, related to the global function in your blog post, if you want to be on more stable ground (i.e., avoid using $), you can create global source code inside a text layer, and evaluate that global code in other layers. Here's a how-to:
1. Create a new text layer, name it "function source" in this example, and hide the layer. In its Source Text prop, enable expressions, and add this code, wrapped in the back-tick / blockquote character (this last part is important)
Code: Select all
`
function myFunction() {
seedRandom(index, timeless = false);
return "index: " + index +
", random: " + Math.round(Math.random(index * 100));
}
`
Code: Select all
eval(thisComp.layer("function source").text.sourceText.value);
myFunction();
-
- Posts: 4
- Joined: December 16th, 2021, 6:07 am
- Location: Shanghai
Evaluating the whole text layers is useful but quite strange. It's so silly of AE that it can only select limited variables in limited types by using the "expression control" effect in layers, and use a complicated code to access them inefficiently, and that it can not directly store global variables.