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

API not workong correctly

Started by Creator, 28 February 2015 - 11:24 PM
Creator #1
Posted 01 March 2015 - 12:24 AM
Hi guys,

my problem is that I load an API with one program and then access the API from another program. When I execute the first program, it loads the API and executes the second program, which returns attempt to index. So the question is ara API loaded system wide or locally?

Thanks guys :)/>

Creator
Edited on 01 March 2015 - 07:19 PM
Lyqyd #2
Posted 01 March 2015 - 12:53 AM
Please post the code involved.
Bomb Bloke #3
Posted 01 March 2015 - 12:59 AM
They're loaded into _G, which should be accessible globally.
Creator #4
Posted 01 March 2015 - 12:35 PM
The code involded is in githud at TheOnlyCreator under the repo TheOS in the files Core/Boot And Core/Login. The API is under Core/Api/GUI

~Creator
Creator #5
Posted 01 March 2015 - 08:01 PM
When try to access the gui api after loading it, it throws this message: attempt to index a nil value

Here is the API:


--Variables--
local buttonsMain = {}
local keysMain = {}
--Functions--
local function LocalAddField(v)
buttonsMain[#buttonsMain+1] = {v[1],v[2],v[1]+v[3]-1,v[2]+v[4]-1,v[5]}
end
function AddField(v)
if not pcall(LocalAddField,v) then
  print([[
  Usage:
  As an argument pass a table structured like this:
  {
   xPos,
   yPos,
   wide,
   height,
   returnValue,
  }
  ]])
end
end
local function LocalDrawButton(v)
if v[5] ~= "nil" then
  paintutils.drawFilledBox(v[1],v[2],v[1]+v[3]-1,v[2]+v[4]-1,v[5])
end
term.setTextColor(v[9])
term.setCursorPos(v[1]+v[6]-1,v[2]+v[7]-1)
local textToPrint = string.sub(v[8],1,v[1]+v[3]-v[6])
term.write(textToPrint)
buttonsMain[#buttonsMain+1] = {v[1],v[2],v[1]+v[3]-1,v[2]+v[4]-1,v[10]}
end
function DrawButton(v)
if not pcall(LocalDrawButton,v) then
  print([[
  Usage:
  As an argument pass a table structured like this:
  {
   xPos,
   yPos,
   wide,
   height,
   colorOfButton,
   inButtonXPosOfLabel,
   inButtonYPosOfLabel,
   label,
   textColor,
   returnValue,
  }
  You can have the option to not use the background color.
  You can do this by simply setting color of button to nil.
  ]])
end
end
function DetectButtonOrKeyHit()
while true do
  local event, button, x, y
  repeat
   event, button, x, y = os.pullEvent()
  until (event == "mouse_click" and buttonsToTest ~= nil) or (event == "key" and keysToTest ~= nil)
  if event == "mouse_click" then
   for i,v in pairs(buttonsMain) do
    if v[1] <= x and x <= v[3] and v[2] <= y and y <= v[4] then
	 return {v[5], button, x, y}
    end
   end
  elseif event == "key" then
   for i,v in pairs(keysMain) do
    if button == v[1] then
	 return {v[2]}
    end
   end
  end
end
end
function ResetButtons()
buttonsMain = {}
end
local function LocalCPrint(v)
if not v[6]t == "nil" then
  v[1] = string.sub(v[1],1,v[6])
end
term.setTextColor(v[2])
if not v[3] == nil then
  term.setBackgroundColor(v[3])
end
term.setCursorPos(v[4],v[5])
term.write(v[1])
end
function cPrint(v)
if not pcall(LocalCPrint,v) then
  print([[
   Usage:
   Arguments are as follws:
   {
   Text to print
   Text color
   Background color
   xPos
   yPos
   Maximal lenght of the string
   }
   Maximal lenght of the string can be left "nil" with qoutes
  ]])
end
end

This is how I call it


gui.ResetButtons()
gui.cPrint({"Login",colors.black,"nil",3,3})
gui.cPrint({"Username: >",colors.gray,"nil",3,6})
gui.cPrint({"Password: >",colors.gray,"nil",3,8})
gui.AddField({15,6,35,6,"userField"})

Thanks guys,

I hope you can help me ;)/>
Bomb Bloke #6
Posted 02 March 2015 - 12:34 AM
Generally this means the API cannot be loaded, typically because it's erroring.

Try manually running the API as you would a regular script, see what that tells you.
Creator #7
Posted 02 March 2015 - 03:02 PM
It works now.