Metatables are weird

DUDE I CANT LEAVE IT AS IT IS. IT MAKES ME SO MAD, IM SO ANGRY.
Basically

    local field = {}
    
    local logic = {}
    logic.__index = logic
    logic.__newindex = function(self, i, v)

        if i == "Strength" then
            self.HealthRegen = 1 + v
            self.DamageBoost = 0 + v * 5
            self.PhysicalDamage = 0 + v * 2
            hum.MaxHealth = 500 + v * 100
        elseif i == "Agility" then
            self.Defence = 1 + v
            self.MovementSpeed = 16 + v
            self.AttackSpeed = 5 + v * 2
            self.RangedDamage = 0 + v

        ...

        elseif i == "MovementSpeed" then
            hum.WalkSpeed = v
        end
        
        rawset(self, i, v)
        
    end
    
    local att = setmetatable(field, logic)
    att.Agility = 50;

HAPPENS NOTHING, THIS STUPID METHAMETHOD WONT RUN
BUT IF ILL DO

        ...
        elseif i == "MovementSpeed" then
            hum.WalkSpeed = v
        else
            rawset(self, i, v)
        end

IT WILL WORK BUT WONT SHOW ME CHANGES I JUST DID IN THE TABLE.
IT WONT SHOW ME HOW THE MOVEMENTSPEED VARIABLE CHANGED. DUDES I BEG YOU FOR HELP!!

I think this problem is being caused by the fact the __newindex metamethod is only called the first time a new index is presented to the table. There’s a method for getting all accesses to the table: Programming in Lua : 13.4.4

This method can be implemented in your case like this:

--Create a unique index for the proxy table
local index = newproxy()

local field = {}
field[index] = {}

local logic = {}
logic.__index = function(self, i)
	--Access the proxy table
	return rawget(self[index], i)
end
logic.__newindex = function(self, i, v)

	if i == "Strength" then
		self.HealthRegen = 1 + v
		self.DamageBoost = 0 + v * 5
		self.PhysicalDamage = 0 + v * 2
		hum.MaxHealth = 500 + v * 100
	elseif i == "Agility" then
		self.Defence = 1 + v
		self.MovementSpeed = 16 + v
		self.AttackSpeed = 5 + v * 2
		self.RangedDamage = 0 + v
	end
	
	--Add this to the proxy table so every time access it normally, the __newindex function will be called
	rawset(self[index], i, v)
end

local att = setmetatable(field, logic)
att.Agility = 50;
print(att.Defence, att.MovementSpeed, att.AttackSpeed) --> 51 66 105
att.Agility = 25;
print(att.Defence, att.MovementSpeed, att.AttackSpeed) --> 26 41 55
1 Like

Will it print a whole table if ill do print(att)?

What do you mean by “whole table”?

well i want to see all the attributes i have in the output.
Actually nevermind, your methods works perfectly, thanks you so much dude, i rly appreciate it.
Would be even cooler if you leave more info about proxies and etc.

1 Like

They did in the link they provided.

https://www.lua.org/pil/13.4.3.html
https://www.lua.org/pil/13.4.4.html
https://www.lua.org/pil/13.4.5.html
https://www.lua.org/pil/16.html
https://www.lua.org/pil/16.1.html
https://www.lua.org/pil/16.2.html
https://www.lua.org/pil/16.3.html
https://www.lua.org/pil/16.4.html
https://www.lua.org/pil/16.5.html

1 Like