1

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)
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
Fnug
  • 23
  • 5

1 Answers1

0

Replace

importClass {'getuser'}

with

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

in each file.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • Hmm didn't work and aren't there any smarter alternatives cause self.userData contains cash, bank, hunger, thirst, stress, inventory would prefer not to use elseif k == "cash" etc.. – Fnug Dec 22 '20 at 12:24
  • `didn't work` - More details please. Can you give minimal working example of your code? – Egor Skriptunoff Dec 22 '20 at 14:11
  • Well it still didn't work like when changing bank on one instance of the class it didn't change the bank to the value changed on another instance same with cash, hunger, thirst, stress and this is problematic cuz I need to save the data to database but kinda hard when it doesn't contain the same data and unsure what u were thinking bout working example got nothing working as intended but here a pastebin https://pastebin.com/0ZJ9xZpr – Fnug Dec 23 '20 at 02:45
  • 1) My answer is wrong. 2) Your original code looks OK. 3) What means `importClass just loads the file`? Do you create new global table `_G.GetUser` in each file inside this "importClass"? You must use `require` to import class. Give a link to documentation of "importClass" – Egor Skriptunoff Dec 23 '20 at 21:27
  • https://pastebin.com/XwXehecA require is not needed since this is for fivem when creating a resource we add a manifest where we can load files contained for the resource but i can do like so "@fg-base/shared/import.lua" so i just load the file containing importClass and from there i can load these classes – Fnug Dec 23 '20 at 22:29
  • Exactly! The Lua file containing class is loaded on every invocation of `importClass`. So, you create new `GetUser` in each file. Answer updated. – Egor Skriptunoff Dec 23 '20 at 22:48
  • The class only gets loaded when using importClass so for each resource I need to load the class again with importClass so your answer "if not _G.GetUser then" will always return true cause _G.GetUser isen't loaded :/ – Fnug Dec 24 '20 at 03:59
  • 1) Are your two files run in different Lua VM? 2) Have you tested my answer? Does it work? – Egor Skriptunoff Dec 24 '20 at 08:58
  • 1) unsure if they run in different VM tried asking the fivem community but no reply... 2) I did test your answer but it kept on importing the class since _G.GetUser was nil – Fnug Dec 25 '20 at 11:57
  • If global variable `GetUser` was set in file1 but is not visible in file2 then the files are run in independent Lua VMs, completely isolated from each other. Try execute `A=42` in file1 and later read `print(A)` in file2. – Egor Skriptunoff Dec 25 '20 at 12:18
  • A would return 42 if the files where in the same folder and loaded thru same manifest otherwise it would be nil like the Global variable GetUser – Fnug Dec 25 '20 at 21:55