View Single Post
04-04-19, 07:19 PM   #1
ArsenalLagspike
A Murloc Raider
Join Date: Mar 2019
Posts: 8
Question Class instances and object pool issues

Ok, so I am instantiating object pools to help me manage state for the mod. Here is a basic run-down of how I am doing it:

Code:
HF_ObjectPool = {
    class = nil;
    available = {};
    inUse = {};
};
HF_ObjectPool.__index = HF_ObjectPool;

-- Instantiates a new pool for a specific class.
-- This class will be used to generate new instances of objects from.
function HF_ObjectPool.new(_class)
    local self = setmetatable({}, HF_ObjectPool);
    self.class = _class;
    return self;
end;
So whenever I need a new pool of objects, I would say:

Code:
HF_GroupPool = HF_ObjectPool.new(HF_Group);
HF_Group being a class object similar to how I am instantiating the object pool class.

The issue I am having in theory is this:

This works fine. Unique keys, the new things get placed in the inUse table on each with helpers I didn't include. Yay.
Code:
PoolA['thing1'] = HF_Thing.new('thing1');
PoolB['thing2'] = HF_Thing.new('thing2');
This blows up. The inUse table in B for some reason recognized the key meant to only be in table A, and then invokes the re-use strategy I have in place.
Code:
PoolA['thing1'] = HF_Thing.new('thing1');
PoolB['thing1'] = HF_Thing.new('thing1');
I am also assigning all function for each "class" like:
function Class.someFunction()

Would it make a difference to make local classes then assign them to props when I "new" an "instance"? Would making them local do anything? I'm new to Lua so I'm not sure why table refs are bleeding between tables.
  Reply With Quote