Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
0
Members

Getting global var from other object's script

A topic by Orlan Rod created Jul 30, 2016 Views: 607 Replies: 2
Viewing posts 1 to 2

How can i access/set a global variable from another object's script? Thanks.

Well, this is more a typescript question. What i normally do if i want to have some global variable/function is keep them in a separated namespace, for example:

namespace Game {
    export let Camera : Sup.Camera = Sup.getActor('Camera').camera;
}

And with that you could get the camera from any script, or change it if you want.

class PlayerBehavior extends Sup.Behavior {
    camera : Sup.Camera
    awake() {
        this.camera = Game.Camera;
        // Or if you want to change it.
        Game.Camera = this.actor.getChild('Camera').camera; // Assuming that its a camera.
    }
}

Now, if what you want is access a behavior variable from another class, you could do something like this:

// Yeah, i like using the Game namespace...
namespace Game {
    export let EnemiesB : EnemyBehavior[] = [];
}

class EnemyBehavior extends Sup.Behavior { name : string; awake() { Game.EnemiesB.push(this) } }

then, you could access all enemies related variables using a loop or something

class PlayerBehavior extends Sup.Behavior {
    awake() {
        for(let enemy of Game.EnemiesB) {
            Sup.log(enemy.name);
            // Again, you can change it here if you want
            enemy.name = "something";
        }
    }
}

Additionally to all this, Superpowers have a built-in function to access the behavior o a known actor, but it haven't worked for me so you will have to wait to someone else to explain that.

Thanks, that worked great.

Yeah, i initially thought that with getBehavior function found in the api would get me there, but i don't understand how to use it.