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

On defining functions

Started by CodeBlue1, 24 October 2012 - 01:21 AM
CodeBlue1 #1
Posted 24 October 2012 - 03:21 AM
Hi fellows, I am very much a noob to this amazing mod and programming in general, but I am having a lot of fun with it anyway.

My question is as to whether or not is possible to define functions without having them execute immediately.

The following is my attempt at a construction program. It is a work is progress, but eventually the goal is to have the user input a height, length, and width for a building which the Turtle would then construct.


local lcount = 0  -- This will keep track of the building's length
local wcount = 0  -- This will keep track of the building's width
local hcount = 0  -- This will eventually keep track of the building's height
local userl = 20  -- This will eventually determine how long the building will be via user input
local userw = 16  -- This will eventually determine how wide the building will be via user input
local userh = 0   -- This will eventually determine how tall the building will be via user input

local buildLong = buildLong()  -- This is where I tried to define a function to be used as a part local function build() by naming it as a variable. This did not work, and its failure is the basis of my post here.
   turtle.placeDown()
   turtle.forward()
   lcount = lcount+1
end

local buildWide = buildWide()
   turtle.placeDown()
   turtle.forward()
   wcount = wcount+1
end

local build = build()  -- This is supposed to be the main function, but I had some earlier issues with syntax. I do assume it would work, however, if I could get the functions within it to act right.
   if lcount < userl then
	  buildLong()
	  lcount = 0
	  turtle.turnRight()
   end
   if wcount < userw then
	  buildWide()
	  wcount = 0
	  turtle.turnRight()
   end
   if lcount < userl then
	  buildLong()
	  lcount = 0
	  turtle.turnRight()
   end
   if wcount < userl then
	  buildWide()
	  wcount = 0
	  turtle.turnRight()
   end
end

build()  -- I didn't really expect this to work. Eventually I want to have it check height against user input and then either build another layer or return to start.
end

Thank you to anyone who reads this, I appreciate your time.
PixelToast #2
Posted 24 October 2012 - 03:50 AM
thats not how you define functions
this is how:

<local?> function <name>(<args>)
	 <body>
end
-- OR --
<local?> <name> = function(<args>)
	 <body>
end
CodeBlue1 #3
Posted 24 October 2012 - 04:13 AM
So it should look like this? :


local function buildLong()
   turtle.placeDown()
   turtle.forward()
   lcount = lcount+1
end

I had started out with that and then confused myself, apparently.

I will keep working on it.

Thanks, friend!
PixelToast #4
Posted 24 October 2012 - 04:30 AM
So it should look like this? :


local function buildLong()
   turtle.placeDown()
   turtle.forward()
   lcount = lcount+1
end

I had started out with that and then confused myself, apparently.

I will keep working on it.

Thanks, friend!
yup, that sould work