(world as CLASS) returns null


(Frazer Bennett Wilford) #1

Not always, but occasionally I get an error, where the world is null.

I don’t always seem to get an error. But when I do, from usually messing around and doing actions very fast and in unpredictable ways:

[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.

In one class.

if (target_line != null)
			{
			(target_line as TargetLine).removeTHIS();
			target_line = null;
			}

and in the TargetLine class

public function removeTHIS():void
	{
			(world as Game).targets -= 1;
			world.remove(this);
	}

(Jacob Albano) #2

When casting one class to another, the result is null if the cast fails. If an entity is being updated by the world, its world variable will never be null; however, if you cast that variable to the wrong type, that cast will return null.

Are you sure that the entity’s world is in fact an instance of your Game class?


(Frazer Bennett Wilford) #3

This is how I’ve been creating my entity for that:

if (planet_picked == true && (world as Game).planet_target != id && target_line == null)
		{
			trace("LINE");
			current_target_id = (world as Game).planet_target;
			target_line = world.add(new TargetLine(id, (world as Game).planet_target));
			target_line.layer = this.layer - 1;
		}

Edjsoidjasjsd - because wouldn’t let me edit


(Jacob Albano) #4

Is it possible you’re trying to remove the entity before it’s actually added to the world? When you call world.add() it doesn’t add the entity right away; it puts it in a list to add the next frame.

If you want to be super safe and prevent errors, you can always do this:

var world:Game = world as Game;
if (world)
{
    // do stuff
}

(Frazer Bennett Wilford) #5

Oh really. Well now I know what is happening. I didn’t realize there would be a one frame delay on adding things to the world. Thanks.

That is going to be fiddly to fix. Any way of doing that besides making a variable to delay it by 1 frame. Which is the sort of thing I do, but feels bad to do.


(Jacob Albano) #6

It’s not strictly a delay; the only thing is that you can’t do this:

var e:Entity = new Entity();
world.add(e);
e.world.someStuff();

I guess it’s more accurate to say that entities won’t be added until the current frame finishes.


(Frazer Bennett Wilford) #7

Thanks for helping. My game is about 70% done now. Been working on it for 3 months now. Can’t wait to release. Over 15,000 lines of code now :smiley: surprised this is the first time this caused an issue to be honest.


(Ultima2876) #8

You can use FP.world as a fallback?

var world: CLASS = world as CLASS; //try to use entity world
if(world == null) { world = FP.world as CLASS; } //if it fails, use global world
//if world is still null here then the current world is not the right CLASS!

This might not work if you’re adding and removing a bunch of entities in your world’s constructor as FP.world will likely still be set to the old world. To avoid that, adopt the good practice of NEVER using the world constructor to create entities or run any kind of game logic, only to initialize simple variables. Instead, use the world’s begin() function.