function createStausBar(data)
--code here
return handler
end
a = createStausBar(data)
a.setMaxValue(5)
a.setValue(3)
How can I do this? I assume I will have to go the oop route?
function createStausBar(data)
--code here
return handler
end
a = createStausBar(data)
a.setMaxValue(5)
a.setValue(3)
local handler = {}
function handler.draw()
--do stuff
end
return handler
Of course, OOP is always better ;)/>then yes your best route is to make use of metatables :)/>
Eh, not always. OOP isn't the only design pattern in the world of programming.Of course, OOP is always better ;)/>
Correction: OOP is usually better (from my experiences)Eh, not always. OOP isn't the only design pattern in the world of programming.Of course, OOP is always better ;)/>
Make sure you understand meta tables first :P/>Thanks, I'm going to read some oop tutorials!
Metawhat?Make sure you understand meta tables first :P/>Thanks, I'm going to read some oop tutorials!
a={}
b={}
b.test = 17
setmetatable(a, {__index=b})
print(a.test)
That will print "17". This is useful for setting the "class" of a table, simply by setting meta tables which provide it with the "class" functions.
obj:func(args)
and it would be the same as doing
obj.func(obj,args)
local function getHandler (text)
local handler = {
text = text or "default text"
}
handler.getText = function ()
return handler.text
end
handler.setText = function (newText)
handler.text = newText
end
return handler
end
local handle = getHandler("Hello")
print(handle.getText()) -->> Hello
handle.setText("World!")
print(handle.getText()) -->> World!
MKlegoman357:17: attempt to call nilSpoiler
You don't really need to use metatables for this:local function getHandler (text) local handler = { text = text or "default text" } handler.getText = function () return handler.text end handler.setText = function (newText) handler.text = newText end return handler end local handle = newHandler("Hello") print(handle.getText()) -->> Hello handle.setText("World!") print(handle.getText()) -->> World!
--# I'm using the do block here to hide the `prototype` table from outside scopes
--# we don't want anything gaining direct access to that, we want them to access it via a handler
local newHandler
do
local prototype = {}
function prototype:getText()
return self.text
end
function prototype:setText(text)
self.text = text
end
newHandler = function(text)
local ivars = {
text = text or "default"
}
return setmetatable(ivars, {__index = prototype})
end
end
local helloWorld = newHandler("Hello")
local fooBar = newHandler("foo")
print(helloWorld:getText())
print(fooBar:getText())
--# change helloWorld text, fooBar text will stay the same
helloWorld:setText(helloWorld:getText() .. " World!")
print(helloWorld:getText())
print(fooBar:getText())
MKlegoman357:17: attempt to call nilSpoiler
You don't really need to use metatables for this:local function getHandler (text) local handler = { text = text or "default text" } handler.getText = function () return handler.text end handler.setText = function (newText) handler.text = newText end return handler end local handle = newHandler("Hello") print(handle.getText()) -->> Hello handle.setText("World!") print(handle.getText()) -->> World!
in any case, it is better to use OO for that, you can reuse the functions as opposed to creating a bunch of functions in memory.OO Example
--# I'm using the do block here to hide the `prototype` table from outside scopes --# we don't want anything gaining direct access to that, we want them to access it via a handler local newHandler do local prototype = {} function prototype:getText() return self.text end function prototype:setText(text) self.text = text end newHandler = function(text) local ivars = { text = text or "default" } return setmetatable(ivars, {__index = prototype}) end end local helloWorld = newHandler("Hello") local fooBar = newHandler("foo") print(helloWorld:getText()) print(fooBar:getText()) --# change helloWorld text, fooBar text will stay the same helloWorld:setText(helloWorld:getText() .. " World!") print(helloWorld:getText()) print(fooBar:getText())
take note of the use of the colon syntax however. If you wish to use the dot notation you must either do helloWorld.getText(helloWorld) or make use of a handy little script I made here which you would replace the local prototype = {} with. That little script allows you to invoke functions either with the colon syntax or dot notation without having to supply the table (if you do however that is fine).
local glass = peripheral.wrap("right")
glass.clear()
local frontArgs = { 1, 1, 50, 50, 0xFF0000, 0 }
function createBox(frontArgs)
local newHandler
do
local prototype = {}
--Variables
prototype.frontHandler = glass.addBox(unpack(frontArgs))
--Functions
function prototype:setWidth(value)
self.frontHandler.setWidth(value)
end
--Main
newHandler = function(text)
local vars =
{
text = text or "derp"
}
return setmetatable(vars, { __index = prototype })
end
end
end
testHandler = createBox(frontArgs)
testHandler:setWidth(500)
test:28: attempt to index ? (a nil value)
I assume it doesn't find the setWidth function?Indeed.–EDIT2:
Oh you're not supposed to put everything in a function, it is already a function…
local glass = peripheral.wrap("right")
glass.clear()
local settings =
{
x1 = 0,
y1 = 0,
x2 = 100,
y2 = 20,
z = 1,
displayColor = 0xFF0000,
backColor = 0x000000,
opacity = 100,
maxValue = 50
}
local createBox
do
local prototype = {}
function prototype:initiate(vars)
self.settings = vars
self.settings.width = math.abs(self.settings.x1 - self.settings.x2)
self.settings.height = math.abs(self.settings.y1 - self.settings.y2)
print("Creating handler with self.settings = "..textutils.serialize(self.settings))
self.displayHandler = glass.addBox(self.settings.x1, self.settings.y1, 1, self.settings.height, self.settings.displayColor, self.settings.opacity)
self.displayHandler.setZ(self.settings.z + 1)
self.backHandler = glass.addBox(self.settings.x1, self.settings.y1, self.settings.width, self.settings.height, self.settings.backColor, self.settings.opacity)
self.backHandler.setZ(self.settings.z)
end
function prototype:setValue(value)
self.settings.value = value
self.displayHandler.setWidth(self.settings.value/self.settings.maxValue*self.settings.width)
end
createBox = function(vars)
print("Creating box with "..textutils.serialize(vars))
return setmetatable(vars, { __index = prototype })
end
end
test = createBox({})
test:initiate(settings)
for i = 1, 50 do
sleep(1)
test:setValue(i)
print(i.."/50")
end
glass:102: attemp to index ? (a nil value)