obviously this return nil. But I would like to know if I can do something like
Table1:AddArguments()
Table2:AddArguments()
function setup()
Table1 = {10,10,10}
Table2 = {20,20,20}
Table1:AddArguments()
end
Undefined = class()
function Undefined:AddArguments()
x = 0
for i,v in pairs(self) do
x = x + v
end
print(x)
end
@FearMe2142 I’m not exactly sure what you’re asking or trying to do with your code. Are you trying to add more entries to an existing table, or add a value to an existing value already in the table.
@FearMe2142 Here’s an example that adds an entry to a table and adds a value to each table entry.
function setup()
Table1 = {10,10,10}
t1=Undefined(Table1) -- create instance t1
print("Add an entry")
t1:AddArguments(25)
t1:print()
print("Add a value")
t1:AddValue(100)
t1:print()
end
Undefined = class()
function Undefined:init(t) -- initialize the table
self.tab=t
end
function Undefined:AddArguments(val) -- add another entry to the table
table.insert(self.tab,val)
end
function Undefined:AddValue(val) -- add a value to each table entry
for z=1,#self.tab do
self.tab[z]=self.tab[z]+val
end
end
function Undefined:print() -- print each table entry
for a,b in pairs(self.tab) do
print(a,b)
end
end
@dave1707
Thank you this is fantastic! I was trying to add a value to an existing table.
One other question, is there a comparison to identify whether something is a table or string like:
If Table1 == type(Table) then
--blah blah
End
function setup()
tab={"abc"}
str="abc"
print(type(tab))
print(type(str))
if type(tab)=="table" then
print("its a table")
end
if type(str)=="string" then
print("its a string")
end
end