Entity Recycling Help


(TheHuckleberryWill) #1

Continuing the discussion from Adding entities in entity constructor?:

How would I properly use Entity recycling, and in what situations would it be useful?


(Zachary Lewis) #2

This video shows how to use entity recycling and why to use it.


(TheHuckleberryWill) #3

Nice vid, also, when I want to create something with constructor arguments, what do I do?


(Ultima2876) #4

Create a separate setup() function and call it like so:

var ent: Entity = FP.world.create(MyEntityClass);
(ent as MyEntityClass).setup(param1, param2);

or to keep it a little more condensed:

(FP.world.create(MyEntityClass) as MyEntityClass).setup(param1, param2);

the setup function is just a regular function and can have whatever arguments you like. I’d recommend that you make it return ‘this’ as follows:

public function setup(param1: int, param2: String): MyEntityClass
{
   //do some stuff here
   return this;
}

(Oli) #6

///Hi sorry! My gun class calls upon this method whenever a shot is fired

My init method look like this in the bullet class

I get the following run time error would you know what im messing up


(Ultima2876) #7

Your Bullet class constructor has to have no arguments, as the world’s create function will call your constructor and supply no arguments - this causes the error which is just saying ‘Bullet’s constructor has told me it requires 4 arguments, but the world create function isn’t giving me any!’.

The fix is to keep your constructor for classes that need to be recycled argument-free, and supply all necessary variables through the init() function – which is what it looks like you have done anyway, you’ve maybe just forgotten to update your constructor to remove them :slight_smile:


(Oli) #8

Thx man, Appreciate It;0


(Oli) #9

I have freed my constructor and I dont get any error! but my Init method does not seem to assign the values. They all stay at zero know why?