Giter Site home page Giter Site logo

flaxobjectpool's Introduction

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 :)

flaxobjectpool's People

Contributors

devhrytsan avatar

Stargazers

kacevoid avatar muhammad Yasin avatar Chandler Cox avatar Enrico Speranza avatar Wojciech Figat avatar Edu Garcia avatar

Watchers

 avatar

flaxobjectpool's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.