Jump to content




Peripheral wrap in API not working?


  • You cannot reply to this topic
6 replies to this topic

#1 lifewcody

  • Members
  • 143 posts
  • Locationstill looking....

Posted 04 November 2014 - 11:28 PM

So I created an API which has this code
main = peripheral.wrap("top")
and on the other program, I call the API and call the function but I get this error:
attempt to index ? (a nil value)

So my question is, can you even use peripheral.wrap in an API? and if you cannot, any ways around this?

#2 valithor

  • Members
  • 1,053 posts

Posted 05 November 2014 - 12:05 AM

My answer here is probably wrong, but seeing as no one else has answered I will attempt to answer.

From my experience defining variables in an api does not work. A way around this is by making the api return the value that you would normally assign to the variable. You would then do something like variable = api.function() in the actual program.

For this example this file is named api
function apifunction()
  return "returning this string"
end

os.loadAPI("api")
variable = api.apifunction()
print(variable)

Edited by valithor, 05 November 2014 - 12:07 AM.


#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 05 November 2014 - 01:02 AM

It should work fine. Please post the full API code and the program you're attempting to use it with.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 November 2014 - 01:34 AM

View Postvalithor, on 05 November 2014 - 12:05 AM, said:

-snip-
you can define variables in APIs just fine...
API
someVar = "hello world"
Code
os.loadAPI("api")
print(api.someVar)
the problem comes in when you localised them to the API and then try access them...

Edited by theoriginalbit, 05 November 2014 - 01:35 AM.


#5 Bomb Bloke

    Hobbyist Coder

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

Posted 05 November 2014 - 01:35 AM

It may be worth noting that when your computer boots, a global "peripheral" variable is assigned a pointer that leads to the table containing a selection of functions (eg, "getType", "wrap", etc).

If a script overwrites that "peripheral" variable, then that script/API/whatever - and other scripts - won't have access to that table any more. It's possible the "problem" isn't in the code you're trying to run, but rather was triggered by code you ran previously.

#6 valithor

  • Members
  • 1,053 posts

Posted 05 November 2014 - 01:48 AM

View Posttheoriginalbit, on 05 November 2014 - 01:34 AM, said:

-snip

Was speaking out of experience. When I tested defining a variable in an api right before I posted that it did not work, but reading how you called the variable makes me see I was probably trying to call the variable incorrectly.

#7 lifewcody

  • Members
  • 143 posts
  • Locationstill looking....

Posted 05 November 2014 - 08:53 AM

View PostBomb Bloke, on 05 November 2014 - 01:35 AM, said:

It may be worth noting that when your computer boots, a global "peripheral" variable is assigned a pointer that leads to the table containing a selection of functions (eg, "getType", "wrap", etc).

If a script overwrites that "peripheral" variable, then that script/API/whatever - and other scripts - won't have access to that table any more. It's possible the "problem" isn't in the code you're trying to run, but rather was triggered by code you ran previously.

That was it! I had a function to detect peripherls named peripheral, duh lol. anyways thanks for the help!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users