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

Railroad layer

Started by ryndan, 17 August 2013 - 04:36 PM
ryndan #1
Posted 17 August 2013 - 06:36 PM
Title: Railroad layer

I've been at it for a few hours, configuring the code, only making it worse. Now i don't even remember the one that was "close" to being finished. I got the code to repeat itself before, but now i can't even get it to do that. (so much for googling after other ways to deal with it)

What i want this code to do is, that the turtle will lay 10 ordinary railroad tracks and then 1 powered railroad track on each side of it, and a redstone torch in between, then repeat that.

So, what's wrong with this code? (or should i say right? lol)


Spoiler

i=0
a=0
If i<22 then
turtle.turnLeft()
turtle.select(1)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.place()
turtle.turnLeft()
turtle.forward
Print("laying rail")
a=a+1
I=i+1
end
if a==10 then
turtle.turnLeft()
turtle.select(2)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
turtle.select(3)
turtle.place
turtle.turnLeft()
turtle.turnLeft()
print("laying powerrail")
a=0
i=i+1
end

and yeah. I'm totally new at this. Made my first script earlier today
Edited by
Bubba #2
Posted 17 August 2013 - 07:21 PM
Well right off the bat, I'm noticing that you capitalized the if statement. if statements should not be capitalized because Lua is case sensitive.

--# Use this:
if expression then
  --# Do stuff
end
--# Don't use this:
If expression then
  --# The program will error before this
end

The next thing I would suggest is that you use for loops in order to repeat an action. For loops are awesome, and you can find out all about them here. An example of a for loop would be:

for n=1,10 do
  print("Moving forward")
  turtle.forward()
end

Another cool thing about for loops is that you can nest them, just like any other block of Lua code. Nesting is where you put a block of code into another block of code to run. For example:

for x=1, 10 do
  for y=1, 10 do
	print(x.." times "..y.. " = "..x*y)
  end
end

Using nesting, you can easily achieve what you want here. However, sometimes it's nice to be able to repeat code over and over again, but separated by other statements. To do this we can use functions.

function track() --# This is where we define what the function will do when we run it.
  for n=1,10 do
	turtle.forward()
	turtle.placeDown()
  end
end

track() --# This is where we actually run the function.

Functions are also awesome in that they can take input. For example:

function track(length) --# This function takes an argument and stores it in a variable called length, which we can use for the rest of the function
  for n=1, length do
	turtle.forward()
	turtle.placeDown()
  end
end

track(10)

Hopefully this will get you started with this :)/> If you need any more help feel free to ask.
ryndan #3
Posted 17 August 2013 - 09:24 PM
Ah, it isn't capitalized in the code. We tried allowing that copy&amp;paste-stuff for the server but crashed it, so we skipped it and i had to copy it all by hand :P/>

Kinda tired right now, so i won't test it out atm. First thing tomorrow tho. Thanks for the help, and i'll let you know tomorrow if i get it to work or not ;)/>
ryndan #4
Posted 18 August 2013 - 12:00 AM
Didn't end up getting any sleep.. yet atleast, started messing around with it and without any real trouble.. i succeeded

Spoilerfunction track1()
for a=1,10 do
turtle.turnLeft()
turtle.select(1)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.place()
turtle.turnLeft()
turtle.forward()
end
end
function track2()
for b=1,1 do
turtle.turnLeft()
turtle.select(2)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
turtle.select(3)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
end
end
track1(10)
track2(1)
track1(10)
track2(1)
track1(10)
track2(1)

However, i did not get the input-version of functions though. I wrote "Command number", but it ended up repeating it ten times as in "track(10)". Tried changing that to "track(lenght)" and got some errorcode. Don't remember which, but if you need to, i can try again :)/>
albrat #5
Posted 18 August 2013 - 08:09 PM

--// the following lines allow you to call your program with arguments.
local targs = {...} --// capture the arguments
if targs == nil then --// if no argument handed to program... then 1 iteration
  x = 1
else
  x = targs[1]
end
--// the above code checks and sets value x which is used in our main program loop at the end...

function track1(length)
if length == nil then length = 1 end
for a=1,length do
turtle.turnLeft()
turtle.select(1)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.place()
turtle.turnLeft()
turtle.forward()
end
end

function track2(length)  --// captures the varible length as an input.
if length == nil then length = 1 end --// make sure that the input is not nil (error catch)
for b=1,length do --// loop the command for length
turtle.turnLeft()
turtle.select(2)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
turtle.select(3)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
end
end

for loop = 1, x do
track1(10) --// hands 10 to function track1()
track2(1) --// hands 1 to function track2()
end

You wrote code that handed values to the functions but your functions did not capture the values / varibles and use them. Instead you had fixed varibles.

These changes make the functions work as adjustable functions. ( eg. You could lay 8 tracks then lay 5 powered rails, then 10 track and 1 powered rail… By sending track1(8) track2(5) track1(10) track2(1) .. )
gives you flexibility in laying of track.

You could also program in a long booster section etc.

Also if you wanted to you could make the turtle run "x" amount of iterations…
(I changed the code already to handle that as a possibility…)

To use your turtle with arguments you would call your program from the turtle like this…


say your program is called "Track"
you would type

Track 10

The program would repeat 10 times and stop.