[Libgdx] Particle Effect Pool

Hello, i have a problem. The function execute, only execute effect twice and also wrong sizes. One huge explosion and on very small which seems correct that is the scaled one. Then it does not show anymore any effects. I would like to execute effects by name and positon. Since i will have a list with effects.

public class Effect {

    private HashMap<String, ParticleEffect> effectList;
    private ParticleEffect effect;
    private ParticleEffectPool effectPool;
    private Array<ParticleEffectPool.PooledEffect> effects;

    public Effect() {
        effectList = new HashMap<String, ParticleEffect>();
        effects = new Array<ParticleEffectPool.PooledEffect>();
    }

    public void execute(String name, Vector2 position) {
        // Create effect:
        effect = effectList.get(name);
        effect.scaleEffect(0.12f);
        effectPool = new ParticleEffectPool(effect, 1, 2);

        ParticleEffectPool.PooledEffect poolObtain = effectPool.obtain();
        poolObtain.setPosition(position.x, position.y);
        effects.add(poolObtain);
        poolObtain.start();
    }

    public void addEffect(String name, ParticleEffect eff) {
        effectList.put(name, eff);
    }

    public void render(SpriteBatch batch, float deltaTime) {
        // Update and draw effects:
        for (int i = effects.size - 1; i >= 0; i--) {
            ParticleEffectPool.PooledEffect effect = effects.get(i);
            effect.draw(batch, deltaTime);

            if (effect.isComplete()) {
                effect.free();
                effects.removeIndex(i);
            }
        }
    }
}

In this case, this isn’t the part of the code that would say why it’s only executing twice…

But the other code is just a input key that cast the function execute. So something has to be wrong inside the effect class.

Oh, I see it… why are you making a new pool each execution?

Well i thought about adding it into the constructor. But since it seems it take a ParticleEffect. Should i just define a Empty ParticleEffect and add it into the constructor then?

Everyone makes coding mistakes, regardless of experience.
Sometimes, it’s faster to fix with someone else looking at it with fresh pair of eyes. Sometimes, it’s faster to fix with someone else with more experience in that area.
But given your’e saying this is the entirety of your program minus some input trigger, then it may very well be in your best interest to struggle and take these obstacles as an opportunity to challenge and improve your debugging and problem-thinking skills.
It’s one thing to ask for help on a piece of code you’ve been unable to figure out after hours of struggling, it’s another to default to asking for help the minute you encounter behavior you can’t explain immediately.

Take a look at this very relevant post, https://softwareengineering.stackexchange.com/a/10751

I understand what you mean, but i’ve tried to figure it out but with no luck. I found out the issue is that, if i remove scale. It show the particle effect. But it feels like the effect gets just bigger and bigger cause more fps drop. Can’t i have list of effects? Or do i need create new pool for every effect :confused:

Read the examples, source code and documentation for the LibGDX components/methods you’re using, if you still don’t understand why it’s doing funky stuff after trying everything then don’t use it and roll out your own diy particle controller with concepts that you understand.

I don’t understand what’s going on in your code snippet, i would have to spend an hour reading source code and examples to help you, maybe, juuuust maybe you should do this yourself :-*

[quote] i’ve tried to figure it out
[/quote]

Yea shuuuure, what have you tried out? Have you tried reading and understanding the javadocs + source code for all the functions you’re using as well as the libgdx wiki article with all the nice examples?

I’ve used the examples and they work perfectly. But i try to adapt it to my way and been battling the issue for hours and still i get back to square 1

I’ll try again, in your execute you are not adding an effect to the pool, you are creating a whole new pool.

I haven’t worked with those enough to say what kind of results that would have.

The best advice I could give you again, would be to place plenty gdx.app.log(tag, string) where you make the tag class.getsimplename, and the string list out the variables that you want to track. Then, you will see the log
screen class: variabe = 1
effect class: etc…

then you can compare those values with the values that you should be getting and it will help isolate where things go wrong.

once things are working as expected, just comment out the logging and move on… sometimes you might add something later that breaks something else and you won’t need to retype everything.

Well it seems i figure it out. Firstly it seems every effect need their own pool, also before even execute the effect. You should initialize the scale before hand.

Well since this is releated to the topic, is it normal that execute particle effect once cause 30-50 fps drop rate?