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

Peripheral wrap in API not working?

Started by lifewcody, 04 November 2014 - 10:28 PM
lifewcody #1
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?
valithor #2
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 on 04 November 2014 - 11:07 PM
Lyqyd #3
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.
theoriginalbit #4
Posted 05 November 2014 - 01:34 AM
-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 on 05 November 2014 - 12:35 AM
Bomb Bloke #5
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.
valithor #6
Posted 05 November 2014 - 01:48 AM
-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.
lifewcody #7
Posted 05 November 2014 - 08:53 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.

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