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

Api Help

Started by DarkNinja2462, 24 February 2012 - 12:08 AM
DarkNinja2462 #1
Posted 24 February 2012 - 01:08 AM
I made an api to add some extra turtle functions. I also made a program to run it.
Currently the only function is to see if the turtle's inventory is full.
I got the error:

isFull:3: attempt to index ? (a nil value)
The program isFull:

os.loadAPI("turtle2")
state = turtle2.isFull()
print(state)
The API turtle2:

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
if turtle.getItemCount(1) == 64 and turtle.getItemCount(2) == 64 and turtle.getItemCount(3) == 64 and turtle.getItemCount(4) == 64 and turtle.getItemCount(5) == 64 and turtle.getItemCount(6) == 64 and turtle.getItemCount(7) == 64 and turtle.getItemCount(8) == 64 and turtle.getItemCount(9) == 64 then
return true
else
return false
end
end
I followed the API tutorial to the letter.
Plz tell me whats wrong?
FuzzyPurp #2
Posted 24 February 2012 - 01:26 AM
Program

os.loadAPI("turtle2")
print(state)
API

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
if turtle.getItemCount(1) == 64 and turtle.getItemCount(2) == 64 and turtle.getItemCount(3) == 64 and turtle.getItemCount(4) == 64 and turtle.getItemCount(5) == 64 and turtle.getItemCount(6) == 64 and turtle.getItemCount(7) == 64 and turtle.getItemCount(8) == 64 and turtle.getItemCount(9) == 64 then
state = true
else
state = false
end
end
DarkNinja2462 #3
Posted 24 February 2012 - 01:27 AM
no, I typed turtle2.isFull()
Function isFull s defined in turtle2
FuzzyPurp #4
Posted 24 February 2012 - 01:34 AM
no, I typed turtle2.isFull()
Function isFull s defined in turtle2

Edited my post - try that, otherwise wait for a lua pro shark - shouldnt be long. They haven't ate much today.
DarkNinja2462 #5
Posted 24 February 2012 - 01:42 AM
Where do I place the API file?
/rom/??
/rom/apis/??
/rom/programs/??
DarkNinja2462 #6
Posted 24 February 2012 - 01:45 AM
And I did what you said above except in the program I added the turtle2.isFull().
So now my program looks like:

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
if turtle.getItemCount(1) == 64 and turtle.getItemCount(2) == 64 and turtle.getItemCount(3) == 64 and turtle.getItemCount(4) == 64 and turtle.getItemCount(5) == 64 and turtle.getItemCount(6) == 64 and turtle.getItemCount(7) == 64 and turtle.getItemCount(8) == 64 and turtle.getItemCount(9) == 64 then
  state = true
else
  state = false
end
end
And the program is now:

os.loadAPI("turtle2")
turtle2.isFull()
print(state)
FuzzyPurp #7
Posted 24 February 2012 - 02:14 AM
Where do I place the API file?
/rom/??
/rom/apis/??
/rom/programs/??

to think of it that might have been your original problem too. put the api in /rom/apis folder and it gets loaded auto with craftOS apis. and you would call its functions like so: turtle2.isFull()

if you put the api in programs folder for instance then
os.loadAPI("rom/programs/turtle2")
Xtansia #8
Posted 24 February 2012 - 03:09 AM
And I did what you said above except in the program I added the turtle2.isFull().
So now my program looks like:

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
if turtle.getItemCount(1) == 64 and turtle.getItemCount(2) == 64 and turtle.getItemCount(3) == 64 and turtle.getItemCount(4) == 64 and turtle.getItemCount(5) == 64 and turtle.getItemCount(6) == 64 and turtle.getItemCount(7) == 64 and turtle.getItemCount(8) == 64 and turtle.getItemCount(9) == 64 then
  state = true
else
  state = false
end
end
And the program is now:

os.loadAPI("turtle2")
turtle2.isFull()
print(state)

API:

if not turtle then
    error( "Cannot load turtle API on computer" )
end
function isFull()
local state
if turtle.getItemCount(1) == 64 and turtle.getItemCount(2) == 64 and turtle.getItemCount(3) == 64 and turtle.getItemCount(4) == 64 and turtle.getItemCount(5) == 64 and turtle.getItemCount(6) == 64 and turtle.getItemCount(7) == 64 and turtle.getItemCount(8) == 64 and turtle.getItemCount(9) == 64 then
     state = true
else
     state = false
end
return state
end

Program:

os.loadAPI("turtle2")
local state = turtle2.isFull()
print(state)
Advert #9
Posted 24 February 2012 - 04:11 AM
I made an api to add some extra turtle functions. I also made a program to run it.
Currently the only function is to see if the turtle's inventory is full.
I got the error:

isFull:3: attempt to index ? (a nil value)
The program isFull:

os.loadAPI("turtle2")
state = turtle2.isFull()
print(state)
The API turtle2:

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
if turtle.getItemCount(1) == 64 and turtle.getItemCount(2) == 64 and turtle.getItemCount(3) == 64 and turtle.getItemCount(4) == 64 and turtle.getItemCount(5) == 64 and turtle.getItemCount(6) == 64 and turtle.getItemCount(7) == 64 and turtle.getItemCount(8) == 64 and turtle.getItemCount(9) == 64 then
return true
else
return false
end
end
I followed the API tutorial to the letter.
Plz tell me whats wrong?

After some experimentation, I think what's wrong is that you tried loading turtle2, but it errored (possibly when you were making the program); then you fixed the error, but since in the turtle, the API is still "being loaded", and when you call os.loadAPI again, it sees that the API is already being loaded, and doesn't do anything; hence turtle2 is nil.
Rebooting the turtle should fix this, as I can't see anything wrong with your code.

Here's what I used to test:


-- Testing code
os.loadAPI("turtle2")
print(turtle2.isFull())
turtle2

if not turtle then
 error("NAT")
end

function isFull()
 for i = 1, 9 do
  if turtle.getItemCount(i) ~= 64 then
   return false
  end
 end
 return true
end
FuzzyPurp #10
Posted 24 February 2012 - 05:18 AM
^ I told you the sharks are circling ^
DarkNinja2462 #11
Posted 24 February 2012 - 04:15 PM
I did Exactly as you said but I put turtle2 in /rom/apis/turtle/
So my isFull is here:
Spoiler

os.loadAPI("rom/apis/turtle/turtle2")
print(turtle2.isFull())
My API
Spoiler

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
for i = 1, 9 do
  if turtle.getItemCount(i) -= 64 then
   return false
  end
end
return true
end

[
The ERROR:
Spoiler

bios:206: [string "turtle2"]:7:
unexpected symbol
TurtleOS 1.3
> isFull
API turtle2 is already being loaded
isFull:2: attempt to index ? (a nil value)
Is something wrong with my bios.lua?
Advert #12
Posted 24 February 2012 - 04:18 PM
I did Exactly as you said but I put turtle2 in /rom/apis/turtle/
So my isFull is here:
Spoiler

os.loadAPI("rom/apis/turtle/turtle2")
print(turtle2.isFull())
My API
Spoiler

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
for i = 1, 9 do
  if turtle.getItemCount(i) -= 64 then
   return false
  end
end
return true
end

[
The ERROR:
Spoiler

bios:206: [string "turtle2"]:7:
unexpected symbol
TurtleOS 1.3
> isFull
API turtle2 is already being loaded
isFull:2: attempt to index ? (a nil value)
Is something wrong with my bios.lua?

You need to replace -= with ~=
and reboot your computercraft computer, since the API is already being loaded.

In addition, if you put your api in rom/apis, you don't have to os.loadAPI it.
BouncyWolf102 #13
Posted 03 March 2012 - 01:46 PM
hi im new to lua and turtles but is there a way to check if the slots are taken up like with any amount not just 64?
Espen #14
Posted 03 March 2012 - 01:55 PM
hi im new to lua and turtles but is there a way to check if the slots are taken up like with any amount not just 64?
turtle.getItemCount(SLOTNUMBER) returns the amount of items in slot SLOTNUMBER.
turtle.getItemSpace(SLOTNUMBER) returns how much space is left in slot SLOTNUMBER. Essentially it's the 64 minus the former.^^
MCGamer20000 #15
Posted 08 June 2013 - 05:05 PM
This needs changed. First of all, some items, like the snowball, doesn't have 64 as a full stack. You could only stack 16 snowballs in one place. Also, in ComputerCraft 1.4, the turtle inventory size was changed to 16. A solution for these is:

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
for i = 1, 16 do
if turtle.getItemSpace(i) ~= 0 then
return false
end
end
return true
end
H4X0RZ #16
Posted 08 June 2013 - 06:15 PM
@OP
(I think) you can't print out a boolean!

Improvement:
The api

if not turtle then error("some error message",2) end

function isFull()
local fullSlots = 0
for i = 1,16 do
if turtle.getItemCount(i) == 64 then fullSlots = fullSlots + 1 end
if fullSlots == 16 then return true else return false end
end

The program

os.loadAPI("turtle2")
if turtle2.isFull() then
print("The turtle's inventory is full!")
else
print("The turtle's inventory isn't full!")
end
billysback #17
Posted 08 June 2013 - 09:10 PM
ummm…
what the hell?
double necro?
February 2012 -> March 2012 -> yesterday!?

also, freack I'm pretty sure you can print a boolean, try

print("boolean: "..tostring(false))
Ninjawolf0007 #18
Posted 27 July 2013 - 10:03 PM
I'm having an issue… I'm trying to load my api from a resource pack and in
 os.loadAPI(" *path goes here* ")
I don't know what I should write for the path because if I write
os.loadAPI("rom/apis/t")
I get a "File not found" error… I have the api in a resource pack, but I don't know how to use it or load it from the resource pack. Could anyone help me?
SpoilerMy API… very few things I know, but could someone help?


print("API Loaded. Place fuel in slot 16 if fuel is required.")

function printMessage(message)
   print(message)
end

function Fuel()
  if turtle.getFuelLevel() < 20 then
    turtle.select(16)
    turtle.refuel(1)
  end
end

function fd()
  Fuel()
  turtle.forward()
end

function bk()
  Fuel()
  turtle.back()
end

function up()
  Fuel()
  turtle.up()
end

function dn()
  Fuel()
  turtle.down()
end

function lt()
  turtle.turnLeft()
end

function rt()
  turtle.turnRight()
end
TheOddByte #19
Posted 28 July 2013 - 04:20 PM
ummm…
what the hell?
double necro?
February 2012 -> March 2012 -> yesterday!?

also, freack I'm pretty sure you can print a boolean, try

print("boolean: "..tostring(false))
Yes you can print a boolean value, But you can't write 'em…
Also why did ya all necro this post?!? D:
(I know I also kinda did that right now.. .-.)
TheOddByte #20
Posted 28 July 2013 - 04:24 PM
I'm having an issue… I'm trying to load my api from a resource pack and in
 os.loadAPI(" *path goes here* ")
I don't know what I should write for the path because if I write
os.loadAPI("rom/apis/t")
I get a "File not found" error… I have the api in a resource pack, but I don't know how to use it or load it from the resource pack. Could anyone help me?
SpoilerMy API… very few things I know, but could someone help?


print("API Loaded. Place fuel in slot 16 if fuel is required.")

function printMessage(message)
   print(message)
end

function Fuel()
  if turtle.getFuelLevel() < 20 then
    turtle.select(16)
    turtle.refuel(1)
  end
end

function fd()
  Fuel()
  turtle.forward()
end

function bk()
  Fuel()
  turtle.back()
end

function up()
  Fuel()
  turtle.up()
end

function dn()
  Fuel()
  turtle.down()
end

function lt()
  turtle.turnLeft()
end

function rt()
  turtle.turnRight()
end
Are you sure it's in the rom/apis folder? .-.
Or do you just have it on the computer?
If you do you could just do

os.loadAPI("Api Name Here")
But i'm not sure since I haven't installed the new CC version and I'm not sure how the resource packs work etc.

EDIT: Btw.. I think if you have it in the apis folder it should load automatically :)/>
Ninjawolf0007 #21
Posted 28 July 2013 - 08:20 PM
I'm having an issue… I'm trying to load my api from a resource pack and in
 os.loadAPI(" *path goes here* ")
I don't know what I should write for the path because if I write
os.loadAPI("rom/apis/t")
I get a "File not found" error… I have the api in a resource pack, but I don't know how to use it or load it from the resource pack. Could anyone help me?
SpoilerMy API… very few things I know, but could someone help?


print("API Loaded. Place fuel in slot 16 if fuel is required.")

function printMessage(message)
   print(message)
end

function Fuel()
  if turtle.getFuelLevel() < 20 then
	turtle.select(16)
	turtle.refuel(1)
  end
end

function fd()
  Fuel()
  turtle.forward()
end

function bk()
  Fuel()
  turtle.back()
end

function up()
  Fuel()
  turtle.up()
end

function dn()
  Fuel()
  turtle.down()
end

function lt()
  turtle.turnLeft()
end

function rt()
  turtle.turnRight()
end
Are you sure it's in the rom/apis folder? .-.
Or do you just have it on the computer?
If you do you could just do

os.loadAPI("Api Name Here")
But i'm not sure since I haven't installed the new CC version and I'm not sure how the resource packs work etc.

EDIT: Btw.. I think if you have it in the apis folder it should load automatically :)/>
Yea, I put it in resourcepacks/ComputerCraftCode/assets/computercraft/lua/rom/apis… It's just not always loading :/ *sigh* I'll figure it out eventually and post here once I get it…

Edit: I got it after I unzipped my resource pack with my api and then re-zipped it… hmm… I wonder if I can have a resource pack that isn't zipped for computercraft… I doubt it…
Ninjawolf0007 #22
Posted 28 July 2013 - 08:59 PM
You can't have a non-zipped resourcepack for Computercraft :(/>