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

help writting a function

Started by th3_3od, 03 October 2013 - 12:09 AM
th3_3od #1
Posted 03 October 2013 - 02:09 AM
I need help writting a function.
Okay I have been trying to come up with a function for placing a torch in the bottom right corner of a 2 high 3 wide tunnel every 9 blocks, i have the digging commands and functions, just placing the torch every 9th has proven difficult, any help would be appreciated, thanks :)/>

EDIT Heres the current code: http://pastebin.com/XDAHUxwd
Yevano #2
Posted 03 October 2013 - 04:20 PM
Show us the code you already have, and we'll be glad to give you a push in the right direction.
jay5476 #3
Posted 03 October 2013 - 07:05 PM
try doing something like this

while true do
for I = 1, 9 do
-- run your dig program
end
-- do torch placing here
end
now this will run your dig program 9 times before it runs the torch placing program make sure your dig program only digs 1 layer as it will be using it 9 times
th3_3od #4
Posted 03 October 2013 - 10:40 PM
Lol sorry forgot to include my code.. And apologies if its.. un organized its my first one so forgive me :P/>

http://pastebin.com/XDAHUxwd
AgentE382 #5
Posted 03 October 2013 - 11:00 PM
Try this:

function refuel()
  turtle.select(1)
  turtle.refuel(2)
end

function digForward()
  while turtle.detect() do
	turtle.dig
  end
end

function layer(placeLight) -- Add a parameter to tell your layer function when to place a torch.
  digForward()
  turtle.forward()
  turtle.digDown()
  turtle.turnLeft()
  digForward()
  tutrle.forward()
  turtle.digDown()
  turtle.turnRight()
  turtle.turnRight()
  if placeLight then light() end
  turtle.turnRight()
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
end

   -- this is the function i need help with, i looked up what i could but it doesnt work and i tried   variations

function light() -- Then you only have to provide basic torch-placing functionality in your light function.
  turtle.select(2)
  turtle.placeDown()
  turtle.select(1)
end

term.write("Please enter how many layers: ")
times = read()
for i = 1, times do
  layer(i % 9 == 0) -- Pass whether it is the 9th layer or not. At every multiple of 9, i % 9 == 0 will evaluate to true, telling your layer function to call your light function.
end

EDIT: Your code is very organized for a beginner. Kudos to you.
EDIT2: Eh, sorry. I mixed up some C++ with Lua for a second. Lua doesn't do short-circuit evaluation like `placeLight and light()`. Instead, use `if placeLight then light() end` Code edited to fix.
Edited on 04 October 2013 - 06:14 AM
th3_3od #6
Posted 04 October 2013 - 12:01 AM
Okay but in function layer() you edited my line of light() to "placeLight and light()" but i dont see a function for placeLight nor do i see an indication in the function for every 9th block "i = 1 times do … etc" how does it work? or is that a differenet undefined fucntion that already exists in game?

EDIT never mind, i see where you defined it, thank you :)/>
AgentE382 #7
Posted 04 October 2013 - 08:28 AM
First, please read "EDIT2" in my post above. I fixed the problem. Sorry about that. This post refers to the edited code.

placeLight is a parameter variable, which is a way of saying that `placeLight` is a named container that holds the first value passed to the `light()` function when it is called. You can pass values to functions by putting that value (or list of values) between the parentheses when you call that function. So, when I call `layer(i %9 == 0)`, placeLight receives the value of `i % 9 == 0`. In this case, placeLight will either be `true` or `false`. If it's `true`, the `if` statement will execute `light()`. Otherwise, it just skips over the entire `if` block.

For example, `print` is a function. If you call it like this:
print("Greetings, user!")
it will output "Greetings, user!" to the screen. `print()` receives a variable number of arguments and prints each of them to standard output. These:
print("My ", "keyboard ", "is ", "tinier ", "than ", "yours.")
print(true, 5, "string", nil, {}, function() end, coroutine.create(function() end))
are both valid calls to `print`.

If you want to see how it works in your program, add this line to your `light()` function:
print(placeLight)
That will take the value of the `placeLight` variable and pass it to the `print()` function. It should print 8 `false`'s followed by one `true`, until it stops.