Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How would you correctly nest this table?

Asked by
Azarth 3141 Moderation Voter Community Moderator
6 years ago

Does anyone know of a better way to nest the 'this' table while still being able to use it as a dictionary? It looks ugly and I'm blanking.

local bank = {}
bank.__index = bank

setmetatable(bank, {
    __index = function(ogt, bank_id)
        -- Converting to a dictionary and nesting 'this'
        ogt[bank_id] = {}
        local this = ogt[bank_id]
        this.balance = 0
        this.banker = bank_id
        setmetatable(this, bank)
        return this
    end
})

function bank:withdraw(how_much)
    self.balance = self.balance - how_much
    return self.balance
end

function bank:deposit(how_much)
    self.balance = self.balance + how_much
    return self.balance
end

function bank:get_balance()
    return self.balance
end

function bank:set_balance(how_much)
    self.balance = how_much
    return self.balance
end

print(bank['person299']:set_balance(10))

`

1 answer

Log in to vote
1
Answered by
blowup999 659 Moderation Voter
6 years ago
ogt[bank_id] = {balance = 0, banker = bank_id}
0
Thanks, but that's not what I mean :3 Azarth 3141 — 6y
Ad

Answer this question