Jump to content




[Lua][Question]Function: how to


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

#1 Teraminer

  • New Members
  • 175 posts
  • LocationDon't look behind you..

Posted 05 April 2012 - 06: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.

Quote

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

Quote

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

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 05 April 2012 - 07: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.

#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 06 April 2012 - 12: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 :)/>

#4 Teraminer

  • New Members
  • 175 posts
  • LocationDon't look behind you..

Posted 06 April 2012 - 07:51 AM

Thanks!!!(I knew what they were but not how to use them..).
Unfurtenly I did not get the (param1, param2, ...) part..

#5 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 06 April 2012 - 11:04 AM

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.

#6 Teraminer

  • New Members
  • 175 posts
  • LocationDon't look behind you..

Posted 06 April 2012 - 03:41 PM

Then can I put no variabrates?

#7 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 06 April 2012 - 05: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()


#8 Teraminer

  • New Members
  • 175 posts
  • LocationDon't look behind you..

Posted 07 April 2012 - 09:17 AM

thanks!

#9 Teraminer

  • New Members
  • 175 posts
  • LocationDon't look behind you..

Posted 16 June 2012 - 01:49 PM

Someone delete this topic.

#10 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 16 June 2012 - 03:56 PM

If the topic gets deleted then how would other people learn from it?

#11 Teraminer

  • New Members
  • 175 posts
  • LocationDon't look behind you..

Posted 16 June 2012 - 05:44 PM

well yeah you're right.. :(/>





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users