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

GUI API doesent work

Started by Wilma456, 24 June 2016 - 12:16 PM
Wilma456 #1
Posted 24 June 2016 - 02:16 PM
I have coded a gui api to mode a scrollable menu (the scrolling is not implement at the moment). But the clickaable menus doesent work.

Code

--Testprogram
os.loadAPI("/lib/gui")
gui.createScroll("Hello")
gui.addScrollFunc("print","hello",1)
gui.createScroll("world")
gui.addScrollFunc("print","world",1)
gui.drawScroll()

--API
local scrollta = {}
local scrollrun = {}
local scrollarg = {}
local scrollbu = {}
local scrollnu = 0
local header = false
function createScroll(tex)
  scrollnu = scrollnu + 1
  scrollta[scrollnu] = tex
  scrollrun[scrollnu] = {}
  scrollarg[scrollnu] = {}
  scrollbu[scrollnu] = {}
end
function addScrollFunc(exec,arg,bu)
  scrollrun[scrollnu][bu] = exec
  scrollarg[scrollnu][bu] = arg
  scrollbu[scrollnu][bu] = true
end
function createScrollHeader(head)
  headtext = head
  header = true
end
function drawScroll()
term.clear()
term.setCursorPos(1,1)
if header == true then
  print(headtext)
end
   for _, scrolli in ipairs(scrollta) do
	 print(scrolli)
   end
while true do
local event,mouse,x,y = os.pullEvent("mouse_click")
if header == true then
  if y+1 <= scrollnu then
    getfenv()[scrollrun[mouse][y+1]](scrollarg[mouse][y+1])
  end
else
  if y <= scrollnu then
    print(scrollrun[y][mouse])
    print(scrollarg[y][mouse])
    getfenv()[srollrun[y][mouse]](scrollarg[y][mouse])--(scrollarg[y][mouse])
  end 
end
end 
end
Print shows me the right variable. And if I write getfenv()["print"]("test") ist works. Wheres the problem?
The_Cat #2
Posted 24 June 2016 - 04:04 PM
You havn't linked the API, but is 'getfenv()' in that API? If so you need to call it with gui infront of it. Like how it says on the 3rd line gui.create… etc.
Wilma456 #3
Posted 24 June 2016 - 04:22 PM
You havn't linked the API, but is 'getfenv()' in that API? If so you need to call it with gui infront of it. Like how it says on the 3rd line gui.create… etc.
Wenn I wrote getfenv()["print"]("test") it works. Thats not the problem.
The_Cat #4
Posted 24 June 2016 - 04:52 PM
My bad. 'getfenv()' doesn't exist as far as I can tell so it will error there anyway. As for the problem at hand, I'm not 100% sure what you mean, is it because you cannot click on the buttons separately and have different outcomes for the two? If so you will need to store the X and Y positions of the buttons. You can then use those positions to check with where the user clicked.
Edited on 24 June 2016 - 03:14 PM
Bomb Bloke #5
Posted 25 June 2016 - 02:17 AM
You haven't bothered to specify, but I imagine you get an "attempt to index nil" on this line:

getfenv()[srollrun[y][mouse]](scrollarg[y][mouse])--(scrollarg[y][mouse])

… because you haven't defined a "srollrun".

This line a little higher up isn't relevant to your immediate problem, but also needs correction:

getfenv()[scrollrun[mouse][y+1]](scrollarg[mouse][y+1])

… as you've got the table indexes the wrong way around.

If you're still having problems, explain what the script DOES do.
Wilma456 #6
Posted 25 June 2016 - 12:40 PM
You haven't bothered to specify, but I imagine you get an "attempt to index nil" on this line:

getfenv()[srollrun[y][mouse]](scrollarg[y][mouse])--(scrollarg[y][mouse])

… because you haven't defined a "srollrun".

This line a little higher up isn't relevant to your immediate problem, but also needs correction:

getfenv()[scrollrun[mouse][y+1]](scrollarg[mouse][y+1])

… as you've got the table indexes the wrong way around.

If you're still having problems, explain what the script DOES do.

thankyou

But i have a new question:
If there any better way to use function in tje runing program as to code a function in the api who just runs os.loadAPI?

--Exampele
--Best way to call the func test
function test()
   print("test")
end
os.loadAPI("/lib/gui")
gui.createScroll("Hello")
gui.addScrollFunc("test","hello",1)
gui.createScroll("world")
gui.addScrollFunc("print","world",1)
gui.drawScroll()
KingofGamesYami #7
Posted 25 June 2016 - 01:13 PM
--Exampele
--Best way to call the func test
function test()
   print("test")
end
os.loadAPI("/lib/gui")
gui.createScroll("Hello")
gui.addScrollFunc(test,"hello",1) --#this passes the actual function, rather than a string, as an argument.
gui.createScroll("world")
gui.addScrollFunc("print","world",1)
gui.drawScroll()
Wilma456 #8
Posted 25 June 2016 - 01:44 PM
--Exampele
--Best way to call the func test
function test()
   print("test")
end
os.loadAPI("/lib/gui")
gui.createScroll("Hello")
gui.addScrollFunc(test,"hello",1) --#this passes the actual function, rather than a string, as an argument.
gui.createScroll("world")
gui.addScrollFunc("print","world",1)
gui.drawScroll()

This doesent works
MKlegoman357 #9
Posted 25 June 2016 - 01:58 PM
--Exampele
--Best way to call the func test
function test()
print("test")
end
os.loadAPI("/lib/gui")
gui.createScroll("Hello")
gui.addScrollFunc(test,"hello",1) --#this passes the actual function, rather than a string, as an argument.
gui.createScroll("world")
gui.addScrollFunc("print","world",1)
gui.drawScroll()
This doesent works

How are you using the argument (function)?
Wilma456 #10
Posted 25 June 2016 - 02:06 PM
--Exampele
--Best way to call the func test
function test()
print("test")
end
os.loadAPI("/lib/gui")
gui.createScroll("Hello")
gui.addScrollFunc(test,"hello",1) --#this passes the actual function, rather than a string, as an argument.
gui.createScroll("world")
gui.addScrollFunc("print","world",1)
gui.drawScroll()
This doesent works

How are you using the argument (function)?

in this function, the argument are not used, Its only there, because I had no errors.
MKlegoman357 #11
Posted 25 June 2016 - 02:43 PM
in this function, the argument are not used, Its only there, because I had no errors.

I'm talking about the 'gui.addScrollFunc()' function. What is it doing? How is it using the function 'test'? You can treat functions just like any other variable. For example:


local function hello ( name )
  print( "Hello, " .. name .. "!" )
end

local function foo ( fn )
  fn( "World" )
end

foo( hello ) --> Hello, World!
Edited on 25 June 2016 - 12:43 PM
Wilma456 #12
Posted 25 June 2016 - 04:58 PM
in this function, the argument are not used, Its only there, because I had no errors.

I'm talking about the 'gui.addScrollFunc()' function. What is it doing? How is it using the function 'test'? You can treat functions just like any other variable. For example:


local function hello ( name )
  print( "Hello, " .. name .. "!" )
end

local function foo ( fn )
  fn( "World" )
end

foo( hello ) --> Hello, World!

After you add a text with with gui.createScroll() You can add a funcktion to this text. For example:

gui.craeteScroll("text")
gui.addScrollFunc("print","hello world",1)
if you run this code, you get the text "text". If you clickk on the text "text" it runs print("hello world").
MKlegoman357 #13
Posted 25 June 2016 - 05:36 PM
Like KingofGamesYami suggested, just pass the actual function and then call it like in my example.