Trying to access my world's variables -.-


(John Andersson) #1

So instead of using static variables, like I’ve always done, now I’m trying to change the habit.

I’m trying to access my world’s variables in an enemy, so I can change the skin of the enemy depending on what type of level is active.

I’ve tried this

var gameworld:GameWorld = GameWorld
if (gameworld.isNormalLevel) spritemap = new Spritemap(NORMAL, 30, 20);
if (gameworld.isGreenLevel) spritemap = new Spritemap(GREEN, 30, 20);

as you can see, I can access the variables just fine (isNormalLevel and isGreenlevel are both variables in the world file), but I get this stupid error:

Implicit coercion of a value of type Class to an unrelated type game:GameWorld.

I know the answer is probably ridiculously stupid easy, but since I’m worthless I don’t know it. Please help, thanks I googled this and found solutions to accessing entities with names, but I can’t seem to give my world a name…


(Jean) #2

You are assigning a type, and not an Object.

You should do this: var gameworld:GameWorld = new GameWorld();


(Jacob Albano) #3

That’s partially right but it won’t produce the desired result since he’s trying to access the world his entities are in.

public override function added():void {
    this.gameworld = world as GameWorld;
}

public override function removed():void {
    this.gameworld = null;
}



(John Andersson) #4

Ehm, should I type that somewhere? I’m confused, how would it allow me to access the entities?

Anyway, if I do

gameworld:GameWorld = new GameWorld();

then I need to fill in the parameter for what tileset I want to use, and that makes no sense, why would I do that when I just need to access the world’s entities

I guess I could type in GameWorld(null); , but it feels equal to using static variables… it feels wrong? I get null reference errors when using null… this makes no sense, why can’t just public vars be accessible? What is the point of having public and private vars


(Jacob Albano) #5

I provided code that will solve your problem right above your comment.


(John Andersson) #6

So I should add it in the gameworld code and then I can access the world’s variables from other entities? How does it work?


(Jacob Albano) #7

In any entity that needs access to the Gameworld’s properties:

var gameWorld:GameWorld = world as GameWorld;
gameWorld.stuffJohnAdded = "stuff";