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

Platform building program yells at me.

Started by D_MO84, 24 November 2014 - 12:24 AM
D_MO84 #1
Posted 24 November 2014 - 01:24 AM
I was trying to write a super simple platform building program for my turtle, and it tells me that it is "attempted to return nil" (at line 11 I think); or something like that. I'm a total programming n00b so I apologize for my cluelessness. Here is the code:

1 – Input Variabls for length and width of platform
2 local inVars = {…}
3
4
5 local width = inVars[1]
6 local length = inVars[2]
7
8 – place a block below turtle till length is reached
9 function buildForward()
10
11 for i=1, length do
12 turtle.placeDown()
13 turtle.forward()
14 end
15
16 end
17
18 – reorient turtle to the right
19 function spinRight()
20
21 turtle.right()
22 turtle.forward()
23 turtle.right()
24
25 end
26
27 – reorient turtle to the left
28 function spinLeft()
29
30 turtle.left()
31 turtle.forward()
32 trutle.left()
33
34 end
35
36 –Main program loop
37
38 for j=1, width do
39
40 buildForward()
41 spinRight()
42 j= j + 1
43
44 buildForward()
45 spinLeft()
46 j= j + 1
47 end

P.S. Why do my super simple programs choke, yet the crazy complex programs I find online work? Rhetorical question.
Dragon53535 #2
Posted 24 November 2014 - 02:37 AM
Are you sure you're not trying to turn left?
There's a difference between turning and going left.

turtle.turnLeft() --#Turn left
turtle.left() --#Move left while facing the same way. Or it would be. My mistake on this. This doesn't exist.
Same for right.
Edited on 24 November 2014 - 02:47 AM
D_MO84 #3
Posted 24 November 2014 - 02:46 AM
I am wanting to make the turtle to turn right/left, so I very much need to change that. However, it doesn't like something about my buildForward function. The problem I've always had when coding is that my code always seems like it should work, yet I usually mess something up and the whole code breaks. I've been reading tutorials and studying the turtle API trying to figure this out, so all words of advice and comment are very much welcome. Thanks Dragon53535.

Please disregard this post. I have figured out what I did wrong and have it all sorted out. Thanks again to Dragon53535 for pointing out that I needed turtle.turnLeft and turtle.turnRight.
Bomb Bloke #4
Posted 24 November 2014 - 03:46 AM
Or rather, you get an "attempt to call nil" when you go to use "turtle.right()" because there is no "turtle.right()". Since it doesn't exist, it's nil; thus it can't be used (or "called").
D_MO84 #5
Posted 24 November 2014 - 06:06 AM
You're very much right Bomb Bloke. I have fixed the code and even added an Inventory check function so that I can have it make a platform larger than 8 by 8. Thank you for the extra clarification though, I was wondering why it was giving me that error.
D_MO84 #6
Posted 24 November 2014 - 09:23 PM
Okay, so I've been trying to add a more effective width checking routine, so that I can make platforms in odd numbered widths. With my current version, the turtle seems to ignore the width count and continuously build back and forth till I manually terminate the program. Here is the code:

http://pastebin.com/6RURzYPv

I know I could grab any number of premade code for this, but I really would like to figure this out. I enjoy coding even though I have trouble wrapping my head around the concepts sometimes. Thanks in advance.
Bomb Bloke #7
Posted 25 November 2014 - 01:06 AM
The arguments passed to the program come in as strings, so you need to convert them:

local width = tonumber(inVars[1])
local length = tonumber(inVars[2])
D_MO84 #8
Posted 25 November 2014 - 04:06 AM
Wow! Of course it would be that simple. Thanks Bomb Bloke!