best practice for casting magic or special attack?

hello i’m just wondering what will be the best practice in unity to let the player cast a magic or special:

instantiate the magic/special attack each time the player need to use it?

or

using it from a pool object?

thank in advance for your help and answers.

I think pooling would be kinda overkill for this. Now, maybe if you had a LOT of spells and effects and all that, then maybe. I may be wrong tho, Ive never used pooling before. I guess just make sure the instance is properly destroyed after its use so garbage doesnt add up.

thanks for your answer, i just want to minimize the garbage, that is the reason of the question.

I’ve always pooled particle fx. Never actually measured the difference/costs, might not matter depending on your platform targets/how many fx you have.

My most common particles are blood and impact, figure why not. Pooling is very little work.

Pool is just: instead of destroy, add object to list and disable, instead of create, check list first.

thank you frosted, i was thinking in pooling all the magic/specials to earn some fps trying to avoid the garbage collection.

i’m working on a 2d side scrolling action game, the player has lots of magic and specials attacks.

last week i was optimizing the game and i put it on 63 fps.

but i think if a use a object pool also for the magic/specials attacks maybe i can gain some fps.

Pool, if you’re aiming for mobile, and whichever works if you’re aiming for PC. Especially things like projectiles.

I believe the general rule is that everything that can be re-used should be.

i’m aiming for a pc release.

thank you for all your answers, i think i will go with the pool option.

IMO, this is not a good rule.

I’d advise to stick with YAGNI/KISS instead. Implement pooling when you need it. On mobile you NEED it. On PC you might need it in some circumstances, but not all objects need to be pooled.

2 Likes

right now i’m only pooling my enemies; all the magic/specials attacks are created using instance and when they go out of the screen they are destroyed.

I donno man, I mean pooling is such a small amount of code in most cases. We’re often talking about a few lines. If there’s the possibility of creating like 100 or more between scene loads I would almost always pool unless there’s a good reason not to. Like 5 instances, whatever.

In general, you won’t notice pooling in terms of overall FPS, you will only see occasional frame spike once in a while. Although, it does depend on how many fx we’re talking about, if there are like 100s then this is different from like 2-3 on screen at once.

If you’re at 63fps on a PC platformer, I would suspect there are problems elsewhere, unless you’re working with extremely low end hardware.

On PC, you should almost not even notice a GC call, it should be very minor unless you’re already under serious load.

There are more costs associated with it than just code. At the very least you need to keep it in memory too.

It also adds a headache of properly resetting the object when dragging it out of the pool into the scene for a second time. For example, if your enemies have bullet wounds on them, you’ll have to make sure you remove those. You end up with functionality duplicated/shared with start. Non-pooled object can also easily perform destructive operations on itself, when you’re using a pool, you’ll have to jump through several hoops to make sure that the object is no longer poolable, or marks itself as non-poolable before doing destructive application.

So, here. Plenty of potential headache because you go against KISS/YAGNI and a dozen of potential landmines that can backfire and cost development time later.

True, there are exceptions to every rule. Memory pressure would be one of them.

what is the normal fps count for a pc platformer using unity3d ?

i’m developing on an i7-3820 at 3.6ghz, with 16 gb ddr3 and amd r9-275 video card (i know that i’m a little behind with my video card, i need to replace it), but i don’t think this specs are low end