FP.world.create


(John Andersson) #1

Okay, so I recently learned that using fp,world.create and fp.world.recycle is better than add and remove

However, my entire game is filled with “add” and “remove” commands. Replacing the “remove” to “recycle” was easy, but “add” to “create” is tricky!

Currently, I’ve tried changing from

FP.world.add(new Box);

to

FP.world.create(new Box);

But I know it’s supposed to be something like

FP.world.create(new Box as Box);

But that gives me an error.

Thank you


(Justin Wolf) #2

world.create(Box);

The point of create is that it recycles Entities you’ve called world.recycle on. So the new is not required, as that not only doesn’t work, it would miss the point of not having to make an entirely new Entity.

Be careful when converting all your world.add calls to world.create. Everything in your constructor of your Box class will not be called if it was successfully pulled from the recycle list. I make an init function on my classes and call it like so:

Box(world.create(Box)).init();

Then in your init function on Box, you add everything you want to call when the Entity is first created. You can still put code in your constructor, however it should be code you only need to define once, because it’ll never be called again unless there’s no more Box left in your recycle list, thus resulting in world.create actually making a brand new Entity and performing identical to world.add.


(Sagan) #3

Hi, I’ve recently been researching this too. Still a little confused. At first I thought that create() in conjuction with an init() function would completely replace the constructor and eliminate the need to call new(), but now I realize that its only replacing the add() function.

I’m trying to create persistent levels where the player can go back and forth between areas. So far I’m using a Level class with vectors of entities contained in the level, and when the player leaves the level, I store those entities’ data as an object associated with the level’s name, and delete the lists of actual entity instances, and then when reloading the next level, I’m making new instances of each entity with the data passed in from the object associated with the level. I’m guessing this is not the best way to do it… In order to make use of world.create() and world.recycle() do I have to keep every entity instance in memory somewhere? How should I be going about this? Thanks to anyone who can help.


(Justin Wolf) #4

When you call recycle on an Entity, it is added to a pool automatically. The next time you use create to make that Entity, it pulls it from the object pool. No need to make your own!


(Sagan) #5

Ah, awesome. Thank you so much!