This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
megamit's profile picture

[CC 1.4.5]Clickable button function. help

Started by megamit, 23 October 2012 - 05:38 PM
megamit #1
Posted 23 October 2012 - 07:38 PM
[SOLVED]

I've written some functions for myself which i will put in a api for myself later but im having trouble getting it to work. I've managed to get the functions to return button im pressing but i cant seem to get it to run any code thats specified in the buttons table and im not sure how to implement it any ideas?



w,h = term.getSize()
buttons = {}
function printC(ypos,str)
term.setCursorPos(w/2-#str/2,ypos) term.write(str) end
function printPos(xpos,ypos,str)
term.setCursorPos(xpos,ypos) term.write(str) end
function click(typ,x,y)
count = 1
for i,v in pairs(buttons) do
if buttons[i].coor[1] == x and buttons[i].coor[2] == y then
print(buttons[i].coor[3])
end
end
end
function hello()
print("hello")
end
function update()
term.setBackgroundColour(colours.blue)
shell.run("clear")
term.setBackgroundColour(colours.lime)
for i=1,h,1 do
if i==1 or i==h or i==4 then printPos(1,i,string.rep(" ",w))
elseif i<h then printPos(1,i," ") printPos(w,i," ")
end
end

term.setBackgroundColour(colours.black)
printC(2,"Picture Pass") printC(3,"Passcoded Lock Screen") term.setTextColour(colours.green)


end
function drawButton(xpos,ypos,colour,str,exe)
term.setBackgroundColour(colour)
printPos(xpos,ypos,str) printPos(xpos+#str,ypos," [X]")
buttons = buttons, {
  [str] = {
	coor = {xpos,ypos,exe}
  }
  }

buttons[str] = {coor = {xpos+#str+2, ypos}}
end
function mInput()
p1,p2,p3,p4 = os.pullEvent()
if p1 == "mouse_click" then click(p2,p3,p4) end
if p1 == "key" then press(p2,p3,p4) end
end


update()
drawButton(7,7,2,"Button","hello()")
drawButton(7,8,2,"other","hello()")
mInput()
jag #2
Posted 23 October 2012 - 09:27 PM
Could you do code tags instead of quote tags?
megamit #3
Posted 23 October 2012 - 09:44 PM
Added the code tags
ComputerCraftFan11 #4
Posted 24 October 2012 - 04:51 AM
To run a function, do it like this:

function hello() --Start the function called hello
  print("Hello world.")
end --Close the function
function example( par1 ) --Start the function that runs any function
  par1() --Run the given program
end --End the function
example( hello ) --Run example(), in the parameter, don't put "" or ()
megamit #5
Posted 24 October 2012 - 08:12 AM
To run a function, do it like this:

function hello() --Start the function called hello
  print("Hello world.")
end --Close the function
function example( par1 ) --Start the function that runs any function
  par1() --Run the given program
end --End the function
example( hello ) --Run example(), in the parameter, don't put "" or ()

Ah i didnt know that. do you know some way to run the program from a stored variable in the buttons table like

example(buttons.[buttonname].coor[3]) -- where coor[3] stores the function

EDIT. It didnt work. i added the function that runs other functions code, but it will just throw out attempt to call nil which is what i expected.
KaoS #6
Posted 24 October 2012 - 09:01 AM
that's cos there is no function for press (used in line 47), you are calling a function that does not exist. hence the attempt to call nil error
megamit #7
Posted 24 October 2012 - 02:20 PM
that's cos there is no function for press (used in line 47), you are calling a function that does not exist. hence the attempt to call nil error
^_^/>/> i see why you say that but im getting line 16: attempt to call nill. here is the current state:


w,h = term.getSize()
 buttons = {}
 function printC(ypos,str)
term.setCursorPos(w/2-#str/2,ypos) term.write(str) end
 function printPos(xpos,ypos,str)
term.setCursorPos(xpos,ypos) term.write(str) end
 function click(typ,x,y)
count = 1
for i,v in pairs(buttons) do
if buttons[i].coor[1] == x and buttons[i].coor[2] == y then runner(buttons[i].coor[3])
print()
end
end
end
function runner(ex)
ex()
end
 function hello()
print("hello")
end
 function update()
term.setBackgroundColour(colours.blue)
shell.run("clear")
term.setBackgroundColour(colours.lime)
for i=1,h,1 do
if i==1 or i==h or i==4 then printPos(1,i,string.rep(" ",w))
elseif i<h then printPos(1,i," ") printPos(w,i," ")
end
end

term.setBackgroundColour(colours.black)
printC(2,"Picture Pass") printC(3,"Passcoded Lock Screen") term.setTextColour(colours.green)


end
function press()
end

 function drawButton(xpos,ypos,colour,str,exe)
term.setBackgroundColour(colour)
printPos(xpos,ypos,str) printPos(xpos+#str,ypos," [X]")
buttons = buttons, {
  [str] = {
    coor = {xpos,ypos,exe}
  }
  }

buttons[str] = {coor = {xpos+#str+2, ypos}}
end
 function mInput()
 p1,p2,p3,p4 = os.pullEvent()
if p1 == "mouse_click" then click(p2,p3,p4) end
if p1 == "key" then press(p2,p3,p4) end
end


update()
drawButton(7,7,2,"Button",hello)
drawButton(7,8,2,"other",hello)
mInput()
makerimages #8
Posted 24 October 2012 - 03:08 PM
Maybe im worn but remove the () after ex, its a parameter, not a function
megamit #9
Posted 24 October 2012 - 04:03 PM
AHHHHH Thanks guys. I Was looking over my code and noticed i was putting the variables in to the button table twice but the second time i wasnt putting the function name in. so when i call the table key of the function name it was nil.
adjusted the drawButton variable stuff
to just this

buttons[str] = {coor = {xpos+#str+2, ypos, exe}}

CASE CLOSED!!…. for now. but thats for a new thread ^_^/>/>