- Home /
Limit the size/capacity of a list
I tried setting the listname.capacity in both start and update and even tried listname.capacity.set but it doesn't exist
what are you trying to do? capacity just sets/reports the limit when the list requires resizing, not limiting the size of it iirc, you'll need to handle that yourself...
Answer by wibble82 · Nov 06, 2015 at 12:31 PM
Hi
The list 'capacity' does not place a fixed size on the list - it is there to tell you how many items the list could currently store without allocating more memory (as it allocates memory out in blocks). When you set it, you are telling the list 'I expect there to be this many items' - it's a hint to avoid lots of little memory allocations.
Is no in built function to add to a list without going over a certain size - indeed, the whole point of 'List' is that it is a dynamically sizing array!
If you want fixed size behaviour you'll either want to:
just use an array (though this doesn't behave quite like a list)
write your own function that only adds to the list if it is not already full
-Chris
Answer by $$anonymous$$ · Aug 23, 2018 at 05:19 PM
hi there
try this
public Unit unit;
public int armyCapacityLimit = 10;
public void RecruitUnits()
{
if (armyCapacity.Count < 10)
armyCapacity.Add(unit);
else
return;
}
Answer by HenryStrattonFW · Nov 06, 2015 at 11:18 AM
For a fixed size, consider using an array, If for whatever reason you do really want a list. then you need to encapsulate access to the list so that you can force a size restriction when you try to add to the list.
Could you please elaborate more? What is and how to use 'encapsulate access'?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to extend tree size to more than 200? 0 Answers
how to access list from another class (list of cards on deck class) ? 1 Answer
List limits element size 2 Answers
List of type Button has size set to zero at runtime 0 Answers