Jump to content




[CC 1.4.5]Clickable button function. help

help lua

  • You cannot reply to this topic
8 replies to this topic

#1 megamit

  • Members
  • 9 posts

Posted 23 October 2012 - 06: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()


#2 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 23 October 2012 - 08:27 PM

Could you do code tags instead of quote tags?

#3 megamit

  • Members
  • 9 posts

Posted 23 October 2012 - 08:44 PM

Added the code tags

#4 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 24 October 2012 - 03: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 ()


#5 megamit

  • Members
  • 9 posts

Posted 24 October 2012 - 07:12 AM

View PostComputerCraftFan11, on 24 October 2012 - 03:51 AM, said:

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.

#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 24 October 2012 - 08: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

#7 megamit

  • Members
  • 9 posts

Posted 24 October 2012 - 01:20 PM

View PostKaoS, on 24 October 2012 - 08:01 AM, said:

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:

Quote

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()


#8 makerimages

  • Members
  • 236 posts

Posted 24 October 2012 - 02:08 PM

Maybe im worn but remove the () after ex, its a parameter, not a function

#9 megamit

  • Members
  • 9 posts

Posted 24 October 2012 - 03: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 ^_^/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users