Jump to content




Not Loading API, I think?

lua api

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

#1 UltraNoobian

  • Members
  • 6 posts
  • LocationAustralia

Posted 09 April 2013 - 02: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.

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 April 2013 - 03: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.

#3 JokerRH

  • Members
  • 147 posts

Posted 09 April 2013 - 09:57 PM

View Posttheoriginalbit, on 09 April 2013 - 03:55 PM, said:

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

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 April 2013 - 10:16 PM

View PostJokerRH, on 09 April 2013 - 09:57 PM, said:

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 by theoriginalbit, 09 April 2013 - 10:26 PM.


#5 UltraNoobian

  • Members
  • 6 posts
  • LocationAustralia

Posted 09 April 2013 - 11:22 PM

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.

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 April 2013 - 11:26 PM

View PostUltraNoobian, on 09 April 2013 - 11:22 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users