Re: "Questions that don't deserve their own thread" thread
Posted: Fri Oct 21, 2016 2:30 pm
Assign values to a table that use values from the same table after the table is initialized.
Code: Select all
msgbox = {
padding = 10,
container = {
x = 10,
y = screen.H/2, -- comma there not actually an error, which is neat
}
}
msgbox.container.w = screen.W-msgbox.padding*2
msgbox.container.h = screen.H/5-msgbox.padding
msgbox.text = {
x = msgbox.container.x,
y = msgbox.container.y
}
Code: Select all
msgbox = {}
msgbox.padding = 10
msgbox.container = {}
msgbox.container.x = 10
msgbox.container.y = screen.W-msgbox.padding*2
-- ...you get the idea, but anyway, if you can, keep some of the stuff in the initializer part if you can, otherwise you'll have to write a dozen lines like the above.
Code: Select all
do
local PADDING = 10
local X, Y = 10, screen.H/2
msgbox = {
padding = PADDING,
container = {
x = X,
y = Y,
w = screen.W-PADDING*2,
h = screen.H/5-PADDING,
},
text = {
x = X,
y = Y
},
}
end
Code: Select all
[b]--[ WARNING BAD / WRONG SYNTAX ]--[/b]
--[Some file somewhere]--
--Global table
TableObjectClass = {
value1 = 100,
value2 = "cows",
--Table within a table test?
_myTableInventory = {
thing1 = 2,
thing2 = 4
},
}
--[Some file somewhere else]--
--Function make copies and store them
GlobalTableStorage = {}
Function makeCopiesandfStore()
-- Dont think lua has a "new" keyword
Local _table = TableObjectClass -- ???? pretty sure this is not correct
GlobalTableStorage = {
obj1 = _table
}
end
Code: Select all
--[[ a.lua ]]--
-- a class
local class = {}
-- class member, not per-instance
class.meh = 0.0
-- method
class.foo = function(c,a)
c.meh = a -- sets class variable
c.kek = a -- sets instance variable
end
-- method
function class.bar(c)
return c.meh -- returns class variable
end
-- method
function class:baz()
return self.kek -- returns instance variable
end
local mtClass = {__index = class}
-- constructor
function new(a)
local obj = {kek=1.0} -- new instance with an instance field/member
obj = setmetatable(obj, mtClass) -- make it so the methods defined in class can be called from the instances
obj.kek = a
return obj
end
-- returning only the constructor; you could also return a separate table filled with class methods, if you wanted.
return new -- return classmethods
----------------------------------------------------------------------------------------------------------------------------------------------
--[[ main.lua ]]--
-- require in our defined class, this will return the constructor into bleh.
local bleh = require 'a'
-- a table holding instances of 'a'
local aTable = {}
for i=1, 10 do
aTable[i] = bleh(i) -- will create class instances, that hold the number i within them.
end
for i=1,10 do print(aTable[i]:bar(), aTable[i]:baz()) end -- should print the following:
--[[
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10
--]]
I could have used better var/field names though. But yes, you build your own classes, if you want/need to.smunnelsnakzruule wrote:Wow, thanks alot, so... I guess Lua makes you build your own class from a set of tables?
Is "return" the magic word here for making copies from a set of pre-built table functions?
Anyway, I'll study the example you gave me and look at the Lua reference manual again. thanks alot.