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

[Lua][Question]Function: how to

Started by Teraminer, 05 April 2012 - 05:08 PM
Teraminer #1
Posted 05 April 2012 - 07:08 PM
I have made a program (with no bugs) that cuts down trees, but I think there is another way by using something like function() or function.blahblahblah.

if
turtle.detect()
then
repeat
turtle.dig()
turtle.up()
until
not turtle.detect()
end
if
turtle.detectUp()
then
turtle.digUp()
turtle.up()
shell.run("wood")
end
if
not turtle.detect()
then
repeat
turtle.down()
until
turtle.detectDown()
end

here is what I want to do

if
turtle.detect() – I want to repace this and add function fwood see below (last script coment)
then
repeat –from repeat till not turtle.detect() I want it to be fwood function
turtle.dig()
turtle.up()
until
not turtle.detect()
end
if
turtle.detectUp()
then
turtle.digUp()
turtle.up()
shell.run("wood") –I want to remove the shell.run and replace it by "saying" to function fwood
end
if
not turtle.detect()
then
repeat
turtle.down()
until
turtle.detectDown()
end


legal:
http://en.wikipedia.org/wiki/WTFPL
Luanub #2
Posted 05 April 2012 - 08:29 PM
If you create an API and put the code to cut down the tree in a function in the API then you can call that function from within your lumberjack program. I've got an API with a harvestTree() function that you can take a look at for an example or even use it if you want. Its located here.
MysticT #3
Posted 06 April 2012 - 01:19 AM
@luanub: You don't need to put the functions in an API, you can write them in the same file as the program.

@Teraminer: A function is a block of code that you can execute. You can define them as follows:

function Name(param1, param2, ...)
  -- function code
end
where Name is the name you want for the function, and param1, param2, etc. are the parameters the function need.
Then, you can call it by its name, passing any arguments inside the brackets:

Name(arg1, arg2, ...)
Example:

function saySomething(sMsg)
  print("The message is: ", sMsg)
end
and then you call it like this:

saySomething("Hello, i'm on a function")
and it will output:
The message is: Hello, i'm on a function

Hope this helps you understand what functions are and how to use it to solve your problem. If it doesn't, just ask and we'll help you :)/>/>
Teraminer #4
Posted 06 April 2012 - 08:51 AM
Thanks!!!(I knew what they were but not how to use them..).
Unfurtenly I did not get the (param1, param2, …) part..
Luanub #5
Posted 06 April 2012 - 12:04 PM
param1 and param2 are just the variables that he assigned for the arguments for his function.

So if you wanted to make a function called fwd to move the turtle forward but with a little extra then the normal turtle.forward(), say add an argument so you can set the number of steps forward for the turtle to take. You would want to set the function up something like..


function fwd( steps )
local steps = steps or 1
for x=1, steps do
turtle.forward()
end
end

And to call it in your code you would simply do fwd( 5 ) to move the turtle 5 spaces forward.
Teraminer #6
Posted 06 April 2012 - 04:41 PM
Then can I put no variabrates?
MysticT #7
Posted 06 April 2012 - 06:05 PM
Yes, you can make functions with no parameters.
Example:

function turn360()
  for i = 1, 4 do
	turtle.turnLeft()
  end
end
But you have to put the brackets when calling it:

turn360()
Teraminer #8
Posted 07 April 2012 - 10:17 AM
thanks!
Teraminer #9
Posted 16 June 2012 - 02:49 PM
Someone delete this topic.
Cloudy #10
Posted 16 June 2012 - 04:56 PM
If the topic gets deleted then how would other people learn from it?
Teraminer #11
Posted 16 June 2012 - 06:44 PM
well yeah you're right.. :(/>/>