Can an entity actually add an entity?


(Blake Jones) #1

So say I have an entity that adds another entity using

FP.world.add(new MyEntity);

the new MyEntity will be able to trace but will not show any of its images on the screen and when I override its update function it won’t even run its update function…traces won’t even work there.

So when an entity adds another entity the new entity doesn’t actually work?


(billy2000) #2

well yes it does work. Have u added a image/spritemap to 2nd entity’s graphic? Can u show us the code of the entity u whant to add ?


(Blake Jones) #3

this is the added enitity (the MyEntity from above):

public class CellImage extends Entity
{
	[Embed(source = "../lib/assets/mechanicshop.png")] private const CELLIMAGE:Class;
	private var cell:Image = new Image(CELLIMAGE);
	
	
	public function CellImage() 
	{

		graphic = cell; //this graphic doesn't display
		trace("this trace works");
		
	}
	
	public override function update():void
	{
		trace("this trace doesn't work");
	}

	
}

(Blake Jones) #4

However if I call the new entity from the world class it works perfect. its only when I add it through another entity does it mess up.


(billy2000) #5

1st in the override public function update put this line of code 1st:

super.update();

2nd u called the entity like this right?

FP.world.add(new CellImage());

(Blake Jones) #6

Yeah tried that again still same result. so confused at this point

edit: and not sure how the super.update() would be required for the update function to run one time. but it doesn’t. even with that.


(rostok) #7

it should work. maybe you are adding it between world changes. try

this.world.add(new MyEntity);

(Blake Jones) #8

I get this error :frowning:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Inventory/updateCellImages()[C:\Flash Projects\FlashPunkBasics\src\Inventory.as:33] at Inventory()[C:\Flash Projects\FlashPunkBasics\src\Inventory.as:19] at GameWorld()[C:\Flash Projects\FlashPunkBasics\src\GameWorld.as:27] at Main()[C:\Flash Projects\FlashPunkBasics\src\Main.as:16]


(billy2000) #9

does ur world class had super.update in the override public function update? if not …try to use the same line of code u used at the entity:

super.update();

as 1st life of code in that function.


(Jacob Albano) #10

From the error it looks as though you’re trying to add the entity from the constructor of your Inventory class. At that time the entity isn’t in a world, so the world reference is null. Try overriding the added() method of your Inventory class and adding the entity there.


(Blake Jones) #11

Holy french fries that did it. thanks so much guys. Jacob that really helped I was banging my head against the wall.

So the constructor is like where you would want to set your initial conditions before the entity actually gets put into the world? and if you want to add other classes you should wait until it actually is placed. Thanks so much I had no idea constructors worked that way.

ps i love all of you


(Jacob Albano) #12

Exactly. In addition, it’s generally a good idea to do as little in the constructor as possible; I can’t find any official source for this, but FlashDevelop’s code analysis plugin will warn against constructors of over 20 lines or so, due to the fact that the Flash runtime can’t optimize them. It’s best to do heavy work in another function; in this case added().


(Alex Larioza) #13

For future reference:

The constructor of any object is invoked when the “new” operator is used to create a new instance of the object.

The added() function is called when the instance is added to a world.


(Ultima2876) #14

Do everything in added(), that way it’s a lot easier to make sure you are keeping good compatibility with recycle() - always recycle entities!

If you need arguments for your created entities, create a separate setup() function that you call after FP.world.create.


(Blake Jones) #15

this thread has been a gold mine :slight_smile: I didnt even know there was a recycle. so much to discover


(Ultima2876) #16

The discovery process is a lot of fun! Probably the most fun thing about game development is that there’s always more to learn!


(JP Mortiboys) #17

One of the reasons why you should keep constructors small is that code inside a constructor isn’t optimized by the JIT, so it runs slower than any other function.

Also, from a FP point of view, since many entities are recycled, having the initialization in the constructor doesn’t really make sense - if the entity is reused rather than created, the constructor will not fire anyway.


(Ultima2876) #18

I’ve heard this before, but it was my understanding that AS3 does little to no code optimization anyway. Have you got examples or a resource that shows the difference and some info about where AS3 would optimize stuff?

I know that it doesn’t do any optimisation on basic typecasts, useless loops, constant expressions or variable accesses.


(Jacob Albano) #19

There’s a difference between code optimization and JITing (Just-InTime compiling) bytecode. As I understand it, AS3 source code is compiled to bytecode that the AVM (Actionscript Virtual Machine) can interpret. This bytecode is quick to compile and doesn’t contain any platform-specific instructions, and this is why a SWF can run on any platform that has a Flash Player.

When the SWF is run, the bytecode that makes up each function is converted to instructions that run natively on the host platform, meaning that less interpretation is needed the next time around. This JITed bytecode is much faster and more efficient, with the drawback that it only works on the platform and architecture of the host; however, this drawback doesn’t matter at all since the JITed bytecode will never be moved to another computer anyway.

Since the AVM doesn’t JIT constructors, the performance of a constructor call will never improve during the duration of the program’s execution.

I don’t claim to be an expert on the subject, and I’m sure I made a mistake or two, but that should give you a general idea of the process. It’s quite a different concept than the optimization that C/++ uses to clean out dead code at compile time.


(Ultima2876) #20

That makes sense, yeah.

I did have a google but I found it very hard to find information on the subject - for some reason I assumed code optimisation and JIT optimisation would be the same.