Jump to content




API not workong correctly

game lua help

6 replies to this topic

#1 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

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 by Creator, 01 March 2015 - 08:19 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,464 posts

Posted 01 March 2015 - 12:53 AM

Please post the code involved.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 March 2015 - 12:59 AM

They're loaded into _G, which should be accessible globally.

#4 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

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

#5 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

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

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#7 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 02 March 2015 - 03:02 PM

It works now.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users