Hi guys I have a problem. My program keeps throwing an error when I run it, it is probably a syntax error, but I dont know Lua well enough... here is the code:
buttonTable = {}
function newButton (label, title, x, y, w, h)
buttonTable[lable.title] = title
buttonTable[lable.x] = x
buttonTable[lable.y] = y
buttonTable[lable.w] = w
buttonTable[lable.h] = h
return label
end
test1 = newButton (test1,buttonTest,2,2,6,4)
print (test1.title)
the error is:" .temp:5: attempt to index ? (a nil value)"
Here's a problem with your code. The incorrect buttonTable[>>lable<<.title] = title is causing a error, because you have a variable that you entered that is not specified.
I've fixed up your code so it won't error.
Here it is:
buttonTable = {}
function newButton (label, title, x, y, w, h)
buttonTable[label.title] = title
buttonTable[label.x] = x
buttonTable[label.y] = y
buttonTable[label.w] = w
buttonTable[label.h] = h
return label
end
test1 = newButton (test1,buttonTest,2,2,6,4)
print (test1.title)
Actually I just checked, the original code is ok, the variable has the correct name, I just named it wrong here (my bad). But there is still that error.
Label is a string yes? What you probably want is something like this:
-- I'm not copying all the code, just going to fix the broken function
local function newButton (label, title, x, y, w, h) -- seriously, look up scope and the local keyword like I said in another thread
-- it might be confusing at first but it is a good habit to get into
buttonTable[label] = { label = lable, title = title, x = x, y = y, w = w, h = h }
return buttonTable[label]
end
What this code does is create a new button 'object' and assign it to the buttonTable under the key 'label', it then returns the 'object'. As you might notice, we create a new table which we treat as the button object
Ok thanks!
Quote from: Lupus590 on Nov 04, 2020, 08:09 PM-- I'm not copying all the code, just going to fix the broken function
local function newButton (label, title, x, y, w, h) -- seriously, look up scope and the local keyword like I said in another thread
-- it might be confusing at first but it is a good habit to get into
buttonTable[label] = { label = lable, title = title, x = x, y = y, w = w, h = h }
return buttonTable[label]
end
What this code does is create a new button 'object' and assign it to the buttonTable under the key 'label', it then returns the 'object'. As you might notice, we create a new table which we treat as the button object
I would agree on this, this is way better on creating a new object. It's described in Jummit's Multitasking API which creates a object for a window. I've made a clipping on the code on how Jummit's API works on making a new window. I've managed to give credit for the iGP OS operating system, of course.
programs.new = function(func, x, y, w, h, title)
local x = x or 1
local y = y or 1
local w = w or widht
local h = h or height
local program = {
x = x, y = y, w = w, h = h, title = title,
term = window.create(
defaultTerm, x, y, w, h
),
selected = false,
coroutine = coroutine.create(func),
reposition = function(self, x, y)
self.x, self.y = x, y
self.term.reposition(x, y)
end,
resize = function(self, w, h)
oldX, oldY = self.term.getPosition()
self.term.reposition(oldX, oldY, w, h)
os.queueEvent("term_resize")
end,
reset = function(self, x, y, w, h)
self.x, self.y, self.w, self.h = x, y, w, h
self.term.reposition(x, y, w, h)
os.queueEvent("term_resize")
end,
terminateCount = 0
}
return program
end