I have some problems with getting this library to work that should add buttons to the hud of the mod OpenGlasses:
I made it first as a program and then converted it to a library for better organization of the code (and for later use). It worked fine when it was a program that called it's own functions, but now it throws errors when it calls them from another program. It says it can't insert the value to the empty table "API.buttonsName". When it was a standalone table it worked fine to insert the value into it, but it keeps saying the that spot is "bad" (that it doesn't exist?). I did try as you guys can see another method to insert it, to no success.
Here is the program that calls the library:
--buttonTest1.lua
local gButtons =require("gButtonAPI3")localfunction test1()print("Working.")end
gButtons.createNewButton("box1","Box 1",2,8,45,15,255,0,0,test1)whiletruedo
gButtons.update()end
And here is the whole library (better write too much than too little):
--gButtonsAPI3.lua
local component =require("component")localevent=require("event")local API ={}local glasses = component.glasses
API.buttons ={}
API.buttonsNames ={}local xV, yV =-1,-1function API.createNewButton(name,label,x,y,w,h,cR,cG,cB,callback)--API.buttonsNames[#buttonsNames+1]= name
table.insert(API.buttonsNames, name)
API.buttons[name]={
rect = glasses.addRect(),
lbl = glasses.addTextLabel(),
posRect ={x,y},
sizeRect ={w,h},
colo ={cR,cB,cG},
state =false,
callbk = callback,
posTxt ={x +(w /2)-((string.len(label)/2)+(w /4.5)),y +(h /3)}}
API.buttons[name]["rect"].setPosition(x,y)
API.buttons[name]["rect"].setSize(w,h)
API.buttons[name]["rect"].setColor(cR,cG,cB)
API.buttons[name]["rect"].setAlpha(0.4)
API.buttons[name]["lbl"].setPosition(API.buttons[name]["posTxt"][1],API.buttons[name]["posTxt"][2])
API.buttons[name]["lbl"].setText(label)
API.buttons[name]["lbl"].setScale(1)
API.buttons[name]["lbl"].setColor(255,255,255)endfunction clickEvent(id, device, user, x, y, button, maxX, maxY)
fX = x *(512/ maxX)
fY = y *(288/ maxY)
xV = fX
yV = fY
endlocalfunction initialize()event.ignore("interact_overlay", clickEvent)event.listen("interact_overlay", clickEvent)end
initialize()function API.update()
os.sleep(1/20)for i in pairs(API.buttonsNames)doif
xV >= API.buttons[API.buttonsNames[i]].posRect[1]and xV <= API.buttons[API.buttonsNames[i]].posRect[1]+API.buttons[API.buttonsNames[i]].sizeRect[1]and
yV >= API.buttons[API.buttonsNames[i]].posRect[2]and yV <= API.buttons[API.buttonsNames[i]].posRect[2]+API.buttons[API.buttonsNames[i]].sizeRect[2]then
API.buttons[API.buttonsNames[i]].callbk()
xV =-1
yV =-1endendendreturn API
And the error log is attached to this post as a picture:
If anyone got any idea what could be the error, I gladly accept any answer! If anyone has questions about the code just ask me and I'll try explaining what it does (or rather "should" do).
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
Hi!
I have some problems with getting this library to work that should add buttons to the hud of the mod OpenGlasses:
I made it first as a program and then converted it to a library for better organization of the code (and for later use). It worked fine when it was a program that called it's own functions, but now it throws errors when it calls them from another program. It says it can't insert the value to the empty table "API.buttonsName". When it was a standalone table it worked fine to insert the value into it, but it keeps saying the that spot is "bad" (that it doesn't exist?). I did try as you guys can see another method to insert it, to no success.
Here is the program that calls the library:
And here is the whole library (better write too much than too little):
And the error log is attached to this post as a picture:
If anyone got any idea what could be the error, I gladly accept any answer! If anyone has questions about the code just ask me and I'll try explaining what it does (or rather "should" do).
Link to post
Share on other sites