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

Cant Load API?

Started by Zenon, 20 July 2015 - 12:28 AM
Zenon #1
Posted 20 July 2015 - 02:28 AM
Hi All! I made myself a few functions that I always seem to be using, and put them into a program that I am using as an API. My own computer that I made the API on will work, however any other computers will draw errors from it; "attempt to index nil ?" Run it and see for your self

Program Code:
Spoiler

--------------------------------------------------
--API Load--
file = fs.exists("themccm")
if file == false then
fs.makeDir("themccm")
end
file = fs.exists("themccm/API")
if file == false then
shell.run("pastebin", "get", "5ww6Pb8i", "themccm/API")
end
os.unloadAPI("themccm/API")
os.loadAPI("themccm/API")
--------------------------------------------------
--Color Loading--
file = fs.exists("themccm/enderdice")
if file == false then
fs.makeDir("themccm/enderdice")
end
file = fs.exists("themccm/enderdice/textcolor")
if file == false then
colorfile = fs.open("themccm/enderdice/textcolor", "w")
colorfile.writeLine("red")
colorfile.close()
end
colorfile = fs.open("themccm/enderdice/textcolor", "r")
textcolor = colorfile.readLine()
colorfile.close()
--------------------------------------------------
--Functions--
function introPrint()
print[[
Enter 'help' For A List Of Commands
What Dice Do You Want To Roll? -In Numbers-]]
term.write("Roll A d")
end
--------------------------------------------------
--Clear--
clear()
--------------------------------------------------
--Visuals--
term.setTextColor(colors.lime)
print[[
--------------------------------------------------]]
term.setTextColor(colors.purple)
print[[
	 ----								 ----
    / 20 \    --------------------	   / 20 \
    \ 20 /   /Welcome To EnderDice\	  \ 20 /
	 ----    |For Your Dicey Needs|	   ----
	 ----    \ Made By: theMCcm   /	   ----
    / 10 \    --------------------	   / 10 \
    \ 10 /							   \ 10 /
	 ----		  Beta  V1.1			 ----]]
term.setTextColor(colors.lime)
print[[
--------------------------------------------------]]
if colors[textcolor] then
term.setTextColor(colors[textcolor])
end
introPrint()
--------------------------------------------------
--Main Code--
while true do
input = read()
inp = tonumber(input)
if type(inp) == "number" then
if inp < 1 then
term.clearLine()
print("Sorry, But Dice Need To Be Atleast One-Sided!")
elseif inp > 100000000000 then
term.clearLine()
print("That Would Crash The Program! Try A Smaller Number")
else
dice = math.random(input)
term.clearLine()
print("You Rolled A d"..inp.." And Got "..dice)
end
lineUp()
lineUp()
term.clearLine()
term.write("Roll A d")
else
if input == "exit" or input == "Exit" or input == "E" or input == "e" or input == "EXIT" then
  clear()
  break
elseif input == "help" or input == "h" or input == "Help" or input == "H" or input == "HELP" then
  lineUp()
  term.clearLine()
  term.write("Roll A d")
  lineDown()
  lineDown()
  print[[
Type 'Exit' To Exit (Duh :P/>)
Type 'Color' To Change Text Color]]
  lineUp()
  lineUp()
  lineUp()
  lineUp()
  term.write("Roll A d")
elseif input == "color" or input == "c" or input == "Color" or input == "C" or input == "COLOR" then
  lineUp()
  term.clearLine()
  term.write("Pick A Color: ")
  textcolor = read()
  if colors[textcolor] then
  term.setTextColor(colors[textcolor])
  colorfile = fs.open("themccm/enderdice/textcolor", "w")
  colorfile.writeLine("colors."..textcolor)
  end
  term.clearLine()
  lineDown()
  term.clearLine()
  lineDown()
  term.clearLine()
  lineUp()
  lineUp()
  lineUp()
  term.clearLine()
  lineUp()
  term.clearLine()
  lineUp()
  term.clearLine()
  introPrint()
else
lineUp()
term.clearLine()
term.write("Roll A d")
lineDown()
term.clearLine()
print("That Wasnt A Number! Try Again.")
lineUp()
lineUp()
term.write("Roll A d")
end
end
end
--------------------------------------------------
--Extras--

--Pastebin: www.pastebin.com/KXRzGDcC

--CompCraft Forums:
---http://www.computercraft.info/forums2/index.php?/topic/24031-enderdice/

--Thanks All And Hope You Enjoy My Dice Roller!
--------------------------------------------------
"API" Code:
Spoiler

--theMCcm's API--
function clear()
term.clear()
term.setCursorPos(1,1)
end
function lineUp()
x,y = term.getCursorPos()
y = y - 1
term.setCursorPos(1, y)
end
function lineDown()
x,y = term.getCursorPos()
y = y + 1
term.setCursorPos(1, y)
end
function lineClear()
term.clearLine()
end

Anyone know whats up and how to fix it? Thanks!
Waitdev_ #2
Posted 20 July 2015 - 02:32 AM
you never loaded the api in the first place when you wrote 'os.unloadAPI()' from here:

--------------------------------------------------
--API Load--
file = fs.exists("themccm")
if file == false then
fs.makeDir("themccm")
end
file = fs.exists("themccm/API")
if file == false then
shell.run("pastebin", "get", "5ww6Pb8i", "themccm/API")
end
os.unloadAPI("themccm/API") --you didn't load the api before this
os.loadAPI("themccm/API")
Edited on 20 July 2015 - 12:34 AM
Zenon #3
Posted 20 July 2015 - 02:36 AM
you never loaded the api in the first place when you wrote 'os.unloadAPI()' from here:

--------------------------------------------------
--API Load--
file = fs.exists("themccm")
if file == false then
fs.makeDir("themccm")
end
file = fs.exists("themccm/API")
if file == false then
shell.run("pastebin", "get", "5ww6Pb8i", "themccm/API")
end
os.unloadAPI("themccm/API") --you didn't load the api before this
os.loadAPI("themccm/API")
That isnt the issue, even without unloading it if you are on another comp, even if it has the api, it wont work
Bomb Bloke #4
Posted 20 July 2015 - 02:41 AM
So which line, exactly, is throwing the error?

Is your original computer still able to run the code ok after you reboot it?
Zenon #5
Posted 20 July 2015 - 02:44 AM
So which line, exactly, is throwing the error?

Is your original computer still able to run the code ok after you reboot it?
Any lines with the functions I defined in the API, starting with 38 but if I redifine the function that was causing the error in the program (in the case of line 38; clear()) it will work just fine.

Edit: No, the original cant after rebooting it either
Edited on 20 July 2015 - 12:49 AM
Dragon53535 #6
Posted 20 July 2015 - 02:55 AM
Lets see if I can list some errors.

Line 38+84: Should be API.clear() since you're loading the api as a table called API. Just calling clear will give you an attempt to call nil error.
Lines 78&amp;79+87+95-98+101+115-116+119+121+125+131+132: Same error. API.lineUp()
Lines 90&amp;91+111&amp;113+128: API.lineDown()


The reason why the original computer worked and the others didn't: You ran your api code and the functions were loaded into the global environment.
If you notice, you can run the functions on the current code (Without the functions inside the code, the api functions) if you run the api program first.
Zenon #7
Posted 20 July 2015 - 02:57 AM
Wait! I figured it out, you need to do "API.clear()" etc. instead of "themccm/API.clear()"

Lets see if I can list some errors.

Line 38+84: Should be API.clear() since you're loading the api as a table called API. Just calling clear will give you an attempt to call nil error.
Lines 78&amp;79+87+95-98+101+115-116+119+121+125+131+132: Same error. API.lineUp()
Lines 90&amp;91+111&amp;113+128: API.lineDown()


The reason why the original computer worked and the others didn't: You ran your api code and the functions were loaded into the global environment.
If you notice, you can run the functions on the current code (Without the functions inside the code, the api functions) if you run the api program first.

Thanks, figured that out just a second ago lol