ripple-new / Functions / GlobalFunctions.lua
GlobalFunctions.lua
Raw
--The global functions file, contains generic functions.

local targetListConversion = {
    ["Actor"] = "modules",
    ["Module"] = "moduleCommands",
    ["GameInstance"] = "actorList",
    
}
function targetListConversion:Find(type) 
    return self[type];
end

local targetTableConversion = {
    ["Actor"] = "Module",
    ["Module"] = "Command",
    ["GameInstance"] = "Actor",
    
}
function targetTableConversion:Find(type) 
    return self[type];
end



GlobalFunctions = {
    Add = function(obj, target)
        local targetTableType = targetTableConversion:Find(obj.type)
        if not string.find(target.type, targetTableType) then return end

        local listToChange = targetListConversion:Find(obj.type);
        
        for k,v in pairs(obj[listToChange]) do
            if k ~= "type" then if k == target.id then print("Nothing found."); return "Nil" end end
        end
        local randnum = string.format("%x", math.random(math.maxinteger));
        repeat
            randnum = string.format("%x", math.random(math.maxinteger));
        until obj[listToChange][randnum] == nil
        obj[listToChange][randnum] = target;
        return randnum;
    end,
    Find = function(obj, id)
        local listToChange = targetListConversion:Find(obj.type);
        if obj[listToChange][id] ~= nil then return obj[listToChange][id] else return false end
    end,
    Remove = function(obj, id)
        local listToChange = targetListConversion:Find(obj.type);
        if obj[listToChange][id] ~= nil then
            local returned = obj[listToChange][id]
            obj[listToChange][id] = nil;
            return returned;
        else
            print(obj.type .. " not found. Removed nothing");
            return nil;
        end
    end,


}