Day 17 - A pool for NPCs

Simple NPC pooling.


Wasteful behavior

We’re no longer leaking huge amounts of data updating the NPCs, and we’re tidying up the corpses, but we’re still wasting both CPU time and memory. Old defunct NPC objects are kept around, while a new NPC will always have to be instantiated. First we’ll convert to using SetActive instead of my homemade solution for deactivating NPCs, and add some stats to track our instances.

Assets/Managers/GUIManager.cslink
32
33
34
35
36
void OnGUI(){
    GUI.Box (new Rect (0,0,100,50), "# of cells\n" + mapManager.num_cells);
    GUI.Box (new Rect (100,0,100,50), "NPC Instances\n" + npcManager.NumNpcScripts);
    GUI.Box (new Rect (200,0,100,50), "Active NPC Insts\n" + npcManager.NumActiveNpcScripts);
    GUI.Box (new Rect (300,0,100,50), "NPCs in DB\n" + npcManager.NumNpcsInDb);

We’ll get rid of this old UI stuff soon, I hope.

Assets/Managers/NpcManager.cslink
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public int NumNpcScripts {
    get { return this.npcScripts.Count; }
}
public int NumActiveNpcScripts {
    get {
        int i = 0;
        foreach (KeyValuePair<int,NpcScript> kvp in this.npcScripts) {
            GameObject npc = kvp.Value.gameObject;
            if (npc.activeSelf) i++;
        }
        return i;
    }
}
int numNpcsInDb;
public int NumNpcsInDb {
    get { return numNpcsInDb; }
}
Assets/Managers/NpcManager.cslink
72
73
74
75
76
77
78
79
80
81
82
83
84
void UpdateAllNpcs() {
    List<NPCLite> npcs = DragonsSpine.DAL.DBNPC.GetAllNpcs();
    print("I found " + npcs.Count + " NPCs");
    numNpcsInDb = npcs.Count;
    foreach (NPCLite npc in npcs) {
        if (npc.lastActiveRound > maxGameRound) maxGameRound = npc.lastActiveRound;
    }
    foreach (NPCLite npc in npcs) {
        // skip inactive NPCs, and make sure they know they are inactive
        if (npc.lastActiveRound < maxGameRound - 10) {
            if (npcScripts.ContainsKey(npc.worldNpcID)) {
                npcScripts[npc.worldNpcID].gameObject.SetActive(false);
            }

58 NPCs have been cleaned up, but we still have an instance for every one that ever existed. Tsk, tsk.


Reanimator

So let’s make a simple mechanism to re-use those old NPC objects.

Assets/Managers/NpcManager.cslink
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if (!npcScripts.ContainsKey(npc.worldNpcID)) {
    // Try to find an inactive one to re-use
    NpcScript tempNpc = null;
    int tempNpcId = 0;
    foreach (KeyValuePair<int,NpcScript> kvp in npcScripts) {
        if (!kvp.Value.gameObject.activeSelf) {
            tempNpc = kvp.Value;
            tempNpcId = kvp.Key;
            break;
        }
    }
    if (tempNpc != null) {
        print ("Found inactive NPC " + tempNpcId + " and am reusing it!");
        npcScripts.Remove(tempNpcId);
        tempNpc.Reset();
    } else {
        print ("Could not find an inactive NPC for " + npc.worldNpcID + " so am instantiating it!");
        tempNpc = (NpcScript) Instantiate(npcScript);
    }
    tempNpc.npcId = npc.worldNpcID;
    tempNpc.name = npc.Name;
    SetMaterials(tempNpc);
    tempNpc.toBeSeen = (position.z <= mapManager.zTop + 1);
    tempNpc.gameObject.SetActive(true);
    npcScripts[npc.worldNpcID] = tempNpc;
}
Assets/NpcScript.cslink
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void Start () {
    Reset();
}

public void Reset() {
    oldPosition = newPosition = transform.position;
    oldScale = newScale = transform.localScale;
    timeSinceStable = 0f;
    toBeSeen = true;
    npcId = 0;
    name = "undefined";
    lastActiveRound = 0;
    hitsFull = hits = 1;
    health = 1f;
    presumedDead = false;
}

Not that the extra objects were taking up so much, really, but we’ll want to be pooling things in the near future as we display more of the action.


Day 17 code - client