- Home /
Use a Stack or List for Transform pooling ?
What the best way to pool transforms, using an Array, List or Stack ?
I know if I use a list I have to check for the next available Transform (deactivated) in the list.
Do stacks always keep the deactivated Transform at the top ready to use and the active ones at the bottom ?
Thanks.
Answer by whydoidoit · Feb 16, 2014 at 08:57 AM
With a stack you normally push newly deactivated items onto the stack and pop them off when you need them. In other words they aren't in the stack while they are being used. That way stack.Count == 0 says there are no more available. I use this method.
Ok, thanks, So I setup
var dustStack : Stack.<Transform> = new Stack.<Transform>(20);
Then I build like
for (var c : int = 0; c < poolSize; ++c){
var newy:GameObject = Instantiate(fallDust.gameObject);
newy.transform.parent = transform;
newy.transform.position = transform.position;
newy.transform.position.z += 0.1 * c;
newy.name = "Soldier Dust-" + c;
dustStack.Push(newy.transform);
newy.SetActive(false);
}
Then I use like
dustStack.Pop();
Then do I push it back when I've finished with it? or pop it back?
dustStack.Push();
Answer by yodda · Sep 25, 2021 at 03:11 PM
Stack is Fixed index while list is not
List is more Flexible for iterating with index [#]
Stack is more for using
first in last out first out last in
if you have an array and you want the item in the middle go for List
if its for object pooling like a barrel of a gun go for Stack
Your answer
Follow this Question
Related Questions
Problem with arrays in a list 1 Answer
Undo/back system using a List/Array 2 Answers
Comparing each random element in an array with each other element 3 Answers
Copy values between two classes in two lists. 1 Answer
I'm sure this algorithm is correct...yet its not performing its function properly.. 1 Answer