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

Not Loading API, I think?

Started by UltraNoobian, 09 April 2013 - 01:12 PM
UltraNoobian #1
Posted 09 April 2013 - 03:12 PM
Hello there, I'm newbie and i'm having some problems calling functions in my program that are in another API.

'tunnel' Program
http://pastebin.com/CqBSGd2F

and the API
http://pastebin.com/k8NAxnFc

The error

tunnel: 66: attempt to index ? (a nil value)
== I replaced that line with the code in the function
-if (turt.hasModem() == true) then
+if(peripheral.getType("right") == "modem") then

THEN
tunnel: 84: attempt to index ? (a nil value)
turt.straightCut(height)

Attempts to load the API via 'lua' in computer throws this error

bios:338: [string "turt"]: 130: '<name>' expected
false

Steps attempted
1. So I have tried replacing code for line 66, and that did work but then it just fell through at line 84 again
2. I know the API does not load properly and throws a hissy fit at line 130 but I don't see what's wrong

				for (slot = 1, 15, 1) do

Much help would be much appreciated, Thank you in advance.

Sincerely,

UltraNoobian.
theoriginalbit #2
Posted 09 April 2013 - 04:55 PM
Ok so you have come from another programming language haven't you? You're putting brackets around all the conditionals and such.

The problem is that in Lua For loops do not need brackets around its conditional, and when the brackets are actually there it will error with the message you are getting. Remove the brackets and you will be good.

On a side note. The reason you are getting the error in the main program is because the APIs are loaded safely meaning if they have an error it just prints it and moves on. Then because the API hasn't loaded the main program errors because it cannot call the API.
JokerRH #3
Posted 09 April 2013 - 10:57 PM
The problem is that in Lua For loops do not need brackets around its conditional, and when the brackets are actually there it will error with the message you are getting. Remove the brackets and you will be good.


local bool = (1 == 0)
print(bool)
--> false

bool = (1 < 15)
print(bool)
--> true

if true then print("Test") end
--> Test

if (1 == 1) then print("Test") end
--> Test 

@TheOriginalBit: Try it, it should work
theoriginalbit #4
Posted 09 April 2013 - 11:16 PM

local bool = (1 == 0)
print(bool)
--> false

bool = (1 < 15)
print(bool)
--> true

if true then print("Test") end
--> Test

if (1 == 1) then print("Test") end
--> Test

@TheOriginalBit: Try it, it should work
yes they work, I do not even need to try them to know that they work. but you are missing the point of what I was saying. I was saying the FOR LOOP cannot have brackets around it. try this and you will see…

for (i = 1, 2) do
  write( "odd that worked" )
end
you can however have this, why you would idk, but you can.

for i = 1, (2) do
  write( "that worked" )
end

EDIT: An explanation why… using the ( ) makes the compiler think that it is a single statement, which in things such as if statements isn't a problem. however in a for loop this is what it is expecting

<keyword-for> <variable-name> <assignment-operator> <starting-value> <statement-separator> <finishing-value> (optional)<statement-separator> (optional, required if previous)<increment-amout> <keyword-do>
which makes

for i = 1, 5 (optional), 2 do
Edited on 09 April 2013 - 09:26 PM
UltraNoobian #5
Posted 10 April 2013 - 12:22 AM
Thank you very much for your enlightenment dear theoriginalbit, It is true that I come from another programming language, C# to be exact, so that will be a habit I will need to cull.

I will remove the brackets and try and run the program again.

That would also explain why the other for statements work.
theoriginalbit #6
Posted 10 April 2013 - 12:26 AM
Thank you very much for your enlightenment dear theoriginalbit, It is true that I come from another programming language, C# to be exact, so that will be a habit I will need to cull.
I thought so :P/> yeh it was a hard one to cull. took me about a week or so to stop it, just like using the { } hard habits to break.