It seems there is a way to do so, but I wanted to ask more of the broader audience to see if this method can work, with the idea inspired by phantombeta.
The typical way of doing things is to insert/push actors into an array and delete/clear them out afterwards. But this leads to constantly creating a new temporary array, copying it over, adding/removing the entry and replacing the old array.
From what I've been lead to believe, the pros for manual management are:
- More efficient injections
- Less reallocating/array copying
- Not optimized by default VM (if it even is done so by JIT)
- Requires manual maintenance
- Searching for nulls to insert into array (unsure if Find() function can be used to locate nulls or if a for loop is needed)
- Allocating new sizes whenever full
Code: Select all
//==========================================================================
// AddActor functions
//==========================================================================
// ReserveArrays
//
// Grows the array by the specific amount if > 0. Then initializes those
// arrays by defaulting them because otherwise the game will crash upon
// attempting to check them.
//==========================================================================
private void ReserveArrays(int amt)
{
if (amt < 1)
return;
int oldSize = Mobj.Size();
Mobj.Reserve(amt);
Chance.Reserve(amt);
for (int i = oldSize; i < Mobj.Size(); i++)
{
Mobj[i] = null;
Chance[i] = 0;
}
}
//==========================================================================
// Internal array management. Finds a null uses that position to store the
// added class type and its spawn chance (weight). If there isn't any space,
// make room for it.
//==========================================================================
private void _AddActor(Class<Actor> cls, int weight)
{
if (Mobj.Size() < 1)
{
ReserveArrays(ReserveAmt);
}
for (int i = 0; i < Mobj.Size(); i++)
{
if (Mobj[i] == null)
{
Mobj[i] = cls;
Chance[i] = weight;
return;
}
int size = Mobj.Size();
if (i >= size - 1)
{
ReserveArrays(ReserveAmt);
i = size - 1;
}
}
}
//==========================================================================
// The main function that handles everything, making sure it exists and has
// weight > 0.
//==========================================================================
void AddActor(String cls, int weight)
{
if (cls == '' || cls == 'None')
return;
Class<Actor> mon = cls;
if (!mon)
{
Console.Printf("%s - bad actor", cls);
return;
}
if (weight < 1)
{
Console.Printf("%s - weight must be above zero", cls);
return;
}
_AddActor(cls, weight);
}
Pretending the non-jit slowdown via the regular VM is a non-issue and eliminating that con (since it can call up to 3 functions in total), is this more efficient?