Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
0
Members

Globals?

A topic by Nocturnal Wisp created Sep 19, 2016 Views: 689 Replies: 2
Viewing posts 1 to 3

Is there a way to access a variable from another script or make a global variable I can access from any script?

outside of a behavior you can write this:

declare var globalVar;
globalVar = "What Ever You Want";

this will enable you to access globalVar in other scripts.


However, you can also get a specific variable from another behavior like so:

let varToGet = Sup.getActor( "SomeActor" ).getBehavior( BehaviorName ).varName;

So, for instance, if you were making a 'Godzilla-Birthing-Simulator' game, and you wanted to check if the Baby Godzilla was born, here's one way you could go about it:

Let's say you have an actor called "Godzilla Mama" and it has a behavior attached to it called "GodzillaMamaBehavior". In this behavior, you have a variable called babyZilla, which is a boolean telling weather or not the Mama Godzilla has given birth yet.

Now, let's pretend you have another actor called "Dr. Godzilla", and in it's behavior script, you wanted to access one of Godzilla Mama's variables. You can write [ in the Dr. Godzilla behavior script ]:

update() {
let isBabyZillaBorn = Sup.getActor( "Godzilla Mama" ).getBeavior( "GodzillaMamaBehavior" ).babyZilla;
if( isBabyZillaBorn ) {
// Do Baby-Zilla Stuff
}
}

I hope my dumb example wasn't too confusing, let me know if you need any more help :)

(+1)

Thank you so much. This helped!