Lua oop static object value

452 Views Asked by At

How would I make self.character.data().bank the same to all instances of the class even when using the function setBank? currently when using setBank it only updates self.character.data().bank on the one instance if it makes sense

_G.GetUser = {
    users = {},
    users_group = {},
    userData = {},
    CurrentCharacter = {}
}

_G.GetUserMethods = {}

GetUserMethods.__call = function(self, source)
    local o = setmetatable({}, {
        __index = self
    })

    o.src = source

    o.id = self.users[o.src]

    o.rank = self.users_group[o.src]

    return o
end

GetUserMethods.__index = {
    getCurrentCharacter = function(self)
        self.character = {}

    self.character.id = self.CurrentCharacter[self.id]

    self.character.data = function()
        return self.userData[self.character.id]
    end

    self.character.setBank = function(v)
        if self.character.data() then
            self.character.data().bank = v
        end
    end
        
    self.character.getBank = function()
        if self.character.data() then
            return self.character.data().bank
        end
        
        return nil
    end

        return self.character
    end
}

setmetatable(GetUser, GetUserMethods)
1

There are 1 best solutions below

11
Egor Skriptunoff On

Replace

importClass {'getuser'}

with

if not _G.GetUser then
   importClass {'getuser'}
end

in each file.