1583 posts
Location
Germany
Posted 05 May 2013 - 02:17 PM
Hi com,
I'm trying to make a little GUI API.
So, I have a function to "start" the gui.
(Starts an infinite loop to catch mouse events)
Here's the function:
function startMenu()
while true do
local sEvent, sButton, sX, sY = os.pullEvent("mouse_click")
for i = 1, #buttons do --buttos is a table which contains the button coords
if sX >= buttons[i][1] and sX <= buttons[i][2] and sY >= buttons[i][3] and sY <= buttons[i][4] then
buttonFunctions[i] --this is a table which contains the functions that runs if a button is clicked
end
end
end
end
whats wrong with the code?
1688 posts
Location
'MURICA
Posted 05 May 2013 - 02:55 PM
Well, one thing I've noticed:
buttonFunctions[i]
Should probably have parentheses "()" after it.
For future reference, you should probably actually tell us what the error is instead of saying "this code is broken, tell me what is wrong".
1583 posts
Location
Germany
Posted 05 May 2013 - 04:23 PM
Well, one thing I've noticed:
buttonFunctions[i]
Should probably have parentheses "()" after it.
For future reference, you should probably actually tell us what the error is instead of saying "this code is broken, tell me what is wrong".
Ok, thx.
The error:
Attempt to call nil
It only crashs when this function isn't commented.
715 posts
Posted 05 May 2013 - 05:39 PM
In general, if you don't know what the error is then you have to give us the full code & the whole error message.
That includes the numbers infront of the error message, as that tells us at which line the error occurred.
Otherwise it's a guessing game as we can't tell if variables have been defined outside of the visible code or not, or if they are defined before or after you're trying to access them.
Having said that, "attempt to call nil" means the program was instructed by you to call a function which doesn't seem to exist at that point in time.
With the code you provided thus far it seems that buttonFunctions wasn't yet defined.
And something that isn't yet defined is always nil. But you can neither access nor call something that is nil.
In short:
Define buttonFunctions, so that it isn't nil before you try to access it.
Also make sure there's actually a function in buttonFunctions before you try to call it.
1583 posts
Location
Germany
Posted 05 May 2013 - 05:43 PM
In general, if you don't know what the error is then you have to give us the full code & the whole error message.
That includes the numbers infront of the error message, as that tells us at which line the error occurred.
Otherwise it's a guessing game as we can't tell if variables have been defined outside of the visible code or not, or if they are defined before or after you're trying to access them.
Having said that, "attempt to call nil" means the program was instructed by you to call a function which doesn't seem to exist at that point in time.
With the code you provided thus far it seems that buttonFunctions wasn't yet defined.
And something that isn't yet defined is always nil. But you can neither access nor call something that is nil.
In short:
Define buttonFunctions, so that it isn't nil before you try to access it.
Also make sure there's actually a function in buttonFunctions before you try to call it.
Yea, I think I will post the code tomorrow.
I have defined the buttonFunctions (and It contains functions :D/> )
715 posts
Posted 05 May 2013 - 05:45 PM
Yea, I think I will post the code tomorrow.
I have defined the buttonFunctions (and It contains functions :D/> )
Alright, then it depends on if you defined it before startMenu() or after.
But I'm looking forward to your post tomorrow, if it's not a huge wall of text :P/>
199 posts
Location
Switzerland
Posted 06 May 2013 - 02:41 AM
another problem could be that the Table buttonFunctions is shorter then the table buttons
add this before the loop to check if they are equal
print("Buttons:"..#buttons)
print("Functions: "..#buttonFunctions)
for i = 1, #buttons do
if sX >= buttons[i][1] and sX <= buttons[i][2] and sY >= buttons[i][3] and sY <= buttons[i][4] then
buttonFunctions[i]
end
1583 posts
Location
Germany
Posted 06 May 2013 - 12:09 PM
another problem could be that the Table buttonFunctions is shorter then the table buttons
add this before the loop to check if they are equal
print("Buttons:"..#buttons)
print("Functions: "..#buttonFunctions)
for i = 1, #buttons do
if sX >= buttons[i][1] and sX <= buttons[i][2] and sY >= buttons[i][3] and sY <= buttons[i][4] then
buttonFunctions[i]
end
Nope, I've a function to draw, add(add in the buttonsntable), addButtonFunction :)/>
In my example code (not online yet) I've 2 buttons (drawn, added and a function is registered)
1583 posts
Location
Germany
Posted 06 May 2013 - 01:05 PM
Thx for your help guys!
I've found the error (It was something with the coords in my buttons table)