Has anyone found a practical use for the $.global object?

Moderators: Disciple, zlovatt

Post Reply
User avatar
pablohotsauce
Posts: 10
Joined: April 13th, 2019, 2:19 pm

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?
User avatar
zlovatt
Posts: 47
Joined: October 31st, 2016, 5:00 pm
Location: Portland
Contact:

Any sort of shared space is incidental, buggy, and shouldn't be relied upon. Don't do this.
User avatar
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.
stib
Posts: 21
Joined: December 10th, 2006, 10:23 pm
Contact:

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/
User avatar
pablohotsauce
Posts: 10
Joined: April 13th, 2019, 2:19 pm

stib wrote: September 11th, 2019, 1:01 amMore on my blog here: https://blob.pureandapplied.com.au/the- ... r-effects/
Ahh nice, I was close: I just needed to take out ".global" in my earlier code.

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));
    }
`
2. Create another text layer, keep it visible. In its Source Text prop, enable expressions, and add this:

Code: Select all

eval(thisComp.layer("function source").text.sourceText.value);
myFunction();
Boom. You don't need to bother with passing variables etc, because when the destination text layer evaluates the "function source" layer's code, it treats it as its own code. The drawback is it can be kinda slow to render, if lots of destination layers are evaluating source text layers.
SolidBlock
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.

Post Reply