• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Question about functions in tables and using vars from table with indexes

dudeim

Member
Joined
Oct 5, 2007
Messages
121
Reaction score
9
Hey,

I'm currently trying to modify variables from a table inside a function that's inside a table.

Lua:
t = {
}

function someFuncThatIsCalled()
for i = 1,10 do
		t[i] = {
		vals131 = 0, --the value that is changed, I gave it some stupid name so I'm sure it's never used somewhere

		add = function()
		vals131 = vals131 + 1 --this increases 1 to the value, it gives me an error here: attempt to preform arithmetic on global 'vals131' (a nil value)
		end,

		sub = function()
		vals131 = vals131 - 1 --this substracts 1 from the value
		end
				}
	end
	
	local m = math.random(1,10)
	t[m].add()
	t[m].add()
	print(t[m].vals131)
end
So why am I getting that error (look at the add function in the table) anyone has an idea?

Thanks!
 
Lua:
t = {
}
vals131 = 0
function someFuncThatIsCalled()
for i = 1,10 do
		t[i] = {
		vals131, --the value that is changed, I gave it some stupid name so I'm sure it's never used somewhere
 
		add = function()
		vals131 = vals131 + 1 --now vals131 is defined as 0 (integer) and can be worked with (increased, decreased)		
                         end,
 
		sub = function()
		vals131 = vals131 - 1 --this substracts 1 from the value
		end
				}
	end
 
	local m = math.random(1,10)
	t[m].add()
	t[m].add()
	print(t[m].vals131)
end
vals131 should be defined before the functions is called.
 
vals131 is a variable of the table itself it's added to the table in the for loop (atleast I think it can be added that way)
This table has 10 indexes and each index should have the var vals131 and those functions as it should change the value of the vals131 variable of that current index like this:
Lua:
--this is an example of how I want to be able to use this table
for i = 1,5 do
t[1].add() --this adds 5 times 1 to vals131 so it should become 5
end
for i = 1,9 do
t[2].add() --this adds 9 times 1 to vals131 so it should become 9
end
for i = 1,39 do
t[3].add() --this should add 39 times 1 to vals131 so it should become 39
end
--etc...
--now if I print it this should be the outcome
print(t[1].vals131)
print[t[2].vals131)
print(t[3].vals131)
--this should print
--5
--9
--39
 
Last edited:
I think I get what you want, yet I can't find a way to do it how you try it myself.
Alternatively you can use a metatable.
Code:
setmetatable(t, {__index = (function(tb,k) if (k=="add") tb[vals131] = tb[vals131]+1 elseif (k=="sub") tb[vals131] = tb[vals131]-1 end end)})
Trying to index add/sub in table t will add/subtract a value now.

Edit: Make sure the add/sub keys aren't defined in the original table yet, since that will not trigger the __index metamethod.
 
If you pass the t's key as an argument it's of course possible. Otherwise I don't think it's possible.
Code:
function(i) t[i][vals131] = t[i][vals131] + 1 end
Etc..
 
Is that the only way as that would kinda suck, well not really bad as there is a workaround but I would've prefered it this way.
 
I really think you're trying to re-make the idea of so called classes. Are you sure you don't want classes for this job?
(an instance got variabeles (in your case t is an instance) => functions are called with the class as upper local scope, making it possible to access the variable like you want)
 
Yeah it's basicly a class I want to create, I first had a metatable like class structure but I don't like the coding style of table:function() I prefer table.function() (yes it's all about that, I'm weird I know).
Well I guess I'll have to get used to : then I guess...
 
Back
Top