Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiate a custom amount of objects on startup to save even more performance #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

alfredbaudisch
Copy link

With the actual code, if you have a fire rate that allows you to shoot let's say 100 bullets at a time, it will instantiate 100 game objects at runtime for the first shoots. Now if you have different pools running at once like bullets, explosions, coins, power ups, etc (like I have in my game, 7 pools) think about instantiating all these objects when the game is running the first time we need the objects, there is a noticeable frame rate and performance drop during the gameplay, specially in fast paced games.

For that reason I improved the class to allow the pre-instantiation of a defined amount of objects at startup. For the Turrent demo, on startup 10 bullets and 10 explosions are added to the pool and recycled before we even need them, this way when we shoot, the pool will never create another object, since 10 is more than enough to the demo's fire rate.

The magic is in the new CreatePool(T prefab, int initialAmount) and RecycleAll(T obj) methods.

EDITED: CreatePool(T prefab, int initialAmount) now uses SpawnInactive instead of Spawn + RecycleAll.

@alfredbaudisch
Copy link
Author

New changes today:

Added the method SpawnInactive and now it is used in place of Spawn and RecycleAll when creating a pool with a pre-defined amount of objects.

Added also the utility method GetAllOfType.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant