Skip to content

DevHrytsan/FlaxObjectPool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flax Object Pool

Description

  • The recycling and reuse of objects, reducing the overhead of creating and destroying objects repeatedly.
  • Easy to use
  • It's also possible to use with other game engines with minor modifications.

How to install

For Flax 1.7+

  1. Open your project.
  2. Navigate to Tools -> Plugins -> Clone Plugin Project.
  3. Enter the following URL: https://github.com/DevHrytsan/FlaxObjectPool.git.
  4. It will ask you to reload the editor. Do it.
  5. Profit!

For Flax 1.6 and below

  1. DON`T open your project in Editor.
  2. Add FlaxObjectPool folder to the Plugin folder in your existing project

    [!WARNING] If you do not already have a Plugin folder in your project, create one. Ensure that the FlaxObjectPool folder is named correctly as "FlaxObjectPool".

  3. Next, add a reference from your game project to the added plugin project. Open <project_name>.flaxproj with a text editor and add a reference to the plugin project. Like this:
 "References": [
      ....,
        {
            "Name": "$(ProjectPath)/Plugins/FlaxObjectPool/FlaxObjectPool.flaxproj"
        }
    ],
  1. Open your project.
  2. Enjoy!

Usage

At the beginning of your C# script, you should include the necessary namespace:

using FlaxObjectPool;

To create an object pool, instantiate the BaseObjectPool class with the desired type , where should be a reference type (class). You can specify several parameters during initialization, including functions for creating, getting, releasing, and destroying objects, as well as default capacity, maximum size, and a flag for limiting releases. For example, I will use Actor class:

var objectPool = new BaseObjectPool<Actor>(
    () => OnCreateFuncion(),    // Function to create objects
    obj => OnGetObject(obj),     // When getting an object
    obj => OnReleaseObject(obj), // When releasing an object
    obj => OnDestroyObject(obj)  // When destroying an object
);

Call the Get() method to retrieve an object from the pool. When you're done with the object, use the Release(T item) method to return it to the pool. For example:

Actor myActor = objectPool.Get(); //Getting from pool
objectPool.Release(myActor); //Releasing to pool

You can clean and dispose of objects in the pool when necessary. The Dispose() method disposes of the entire pool, while the Clean() method releases all active objects back into the pool. Use them as needed.

objectPool.Dispose(); // Dispose the entire pool
objectPool.Clean();  // Release all active objects back to the pool

You can retrieve pool-related information, including its default capacity, maximum size, the total object count in the pool, and the count of active objects, by accessing properties like DefaultCapacity, MaxSize, CountAll, and ActiveCount.

Example

You'll find it in the plugin's root folder under "Content -> Demo" and open the "DemoScenePool" scene. It showcases the usage of pooling with custom projectiles. To remove the demo content, delete all the "Demo" folders in FlaxObjectPool.

FlaxEditor_T9sPvSqcnT.mp4

Contribution

Feel free to contribute to this project and suggest some ideas for it. Don`t forget about Flax Engine discord server

License

Under the MIT license. Feel free to use :)

About

Object Pooling for Flax Engine

Resources

License

Stars

Watchers

Forks

Languages