Global variable scope


#1

Hey,

I’m wondering if there exists a good practice for defining global variables in Studio projects?

I’d like to define some strings in the root symbol of the project and access these in subsymbols living quite deep inside other subsymbols. Currently trying to implement a definition script where I could set Zapalytics strings and then apply these in subsymbols that need to log events.

In the root I have a script ‘globals’ with:

export namespace analytics {
    export const someString = "test";
}

And in a script inside a subsymbol:

var a = root__sym__globals.analytics;
console.log(a.someString); // will later become Z.stats.logEvent()

The Studio editor seems to think this is ok as it doesn’t tell me about errors and the autocomplete finds root__sym_globals.analytics. However, as I try to publish the zap, the compiler throws me an error about not finding it. I assume my way is too much of a hack and not the intended way of doing this…

Anyone know how I could accomplish what I’m trying to do? I’d be happy with something hacky and working for now as I’d love to implement this in my project right away. This could be later polished to become a useful tool (zapalyticsManager) for any Studio project.


#2

I have played a little with them. But I used the info from this link to help me.
Changing a 3D object’s skin/ material with button press

Hope it helps

Steve


#3

Thanks for the suggestion. Unfortunately, what Sebastian describes as ‘global’ there is different from my meaning. It works when you want to access a subsymbol script from the parent but doesn’t help when you want to do it the other way around.

I realized that my attempt was rather silly anyway, I believe I’m trying to use typeof definitions (root__sym__stuff) that are meant for Studio code as if they were references to the actual scene objects. This obviously wont work.

I think I’ll just restructure my code so that I will have variables in the subsymbols and then I’ll just populate those from the root script. Should avoid globals anyway.