简体   繁体   English

Lua oop static object值

[英]Lua oop static object value

How would I make self.character.data().bank the same to all instances of the class even when using the function setBank ?即使使用 function setBank ,我如何使self.character.data().bank与 class 的所有实例相同? currently when using setBank it only updates self.character.data().bank on the one instance if it makes sense目前在使用setBank时,它只在一个实例上更新self.character.data().bank如果它有意义

_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)

Replace代替

importClass {'getuser'}

with

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

in each file.在每个文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM