Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
The Simon

Trouble with Library

Question

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:

--buttonTest1.lua
local gButtons = require("gButtonAPI3")

local function test1()
  print("Working.")
  
end
  
gButtons.createNewButton("box1","Box 1",2,8,45,15,255,0,0,test1)

while true do
  gButtons.update()
end

And here is the whole library (better write too much than too little):

--gButtonsAPI3.lua
local component = require("component")
local event = require("event")

local API = {}

local glasses = component.glasses

API.buttons = {}
API.buttonsNames = {}

local xV, yV = -1,-1


function 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)
  
end

function clickEvent(id, device, user, x, y, button, maxX, maxY)
  fX = x * (512 / maxX)
  fY = y * (288 / maxY)
  xV = fX
  yV = fY
  
end



local function 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) do
	if 
      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 = -1
	  
	end
  end
end

return 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).

ErrorLua2.png

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Ive loaded your script and api and they seem to be working as expected..

I see the test button and when i click the 'Working.' message is printed.

I'm not getting your error at all. Perhaps reboot, its possible an older dev version of your 'gButtonAPI3' is or was still loaded when you ran your test.

if you don't already know, this trick can be helpful while developing apis and testing them as you tweak them.

package.loaded.myAPInameHere = nil -- # require is idempotent. purge it!
local api = require 'myAPInameHere'

api.stuff()

 

Link to post
Share on other sites
  • 0
On 2017-12-23 at 8:51 PM, Molinko said:

Ive loaded your script and api and they seem to be working as expected..

I see the test button and when i click the 'Working.' message is printed.

I'm not getting your error at all. Perhaps reboot, its possible an older dev version of your 'gButtonAPI3' is or was still loaded when you ran your test.

if you don't already know, this trick can be helpful while developing apis and testing them as you tweak them.


package.loaded.myAPInameHere = nil -- # require is idempotent. purge it!
local api = require 'myAPInameHere'

api.stuff()

 

Yeah, I had already rebooted the computer with the program on it. I eventually gave up for the day, next day I began working on it again. And hold and behold, it works!

Maybe it solved itself when I restarted the game (restarted my computer). I hadn't restarted the game at all that day, so that could have been the problem. I highly doubt it was because of my computer and simply the game. I have to keep that in mind in the future to restart my game once in a while so I don't go nuts over stuff like this.

 

I made this library because I couldn't find any myself. I thought of sharing it. You know where I can post my library so it doesn't disapear into the depth of the internet and falls into oblivion (and has to be re-invented again)?

Link to post
Share on other sites
  • 0

That sounds like the OC config. If you're developing outside the game in an editor while the game is open then [by default] the in-game files will not sync with the real filesystem until MC is closed. If you want to save files live outside the game and not have to restart the game you can change the config in .../instances/your_pack_here/config/opencomputers/settings.conf. Set the option to "bufferChanges=false". Be sure to read the disclaimer above the option too.

Link to post
Share on other sites

Join the conversation

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.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.