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

Lua noob seeking help!

Started by crud41, 24 December 2012 - 06:33 PM
crud41 #1
Posted 24 December 2012 - 07:33 PM
I have been searching for information everywere on a few topics and I couldent find any information maybe I can get some answers here:


What does this do?:
how is it used?
I know that print means what is says but I do not understand what the local args =(..) means mostly confused about what the … means does it mean the same thing as * in other codes?

local args = {...}
if #args ~= 1 then
	print('Usage: lumberjack <size>')
	return

———————————————————————————————————————————————————–
Second question:

What would this do? not even sure what it could mean.

for n = 1, args[1], 1 do

————————————————————————————————————————————————————
Third Question:

If I just open the prompt in game, and I type "turtle.down" it would move down a single square how come when it is in
this format it goes on endlessly? is it because of the detectdown prompt? also can I get some help on turtle.compare?
how do I choose the slot it compares to? do I have to do turtle.select(2) to select slot 2?

function up()
	while not turtle.up() do
		turtle.digUp()
	end
end

function cutTree()
	while turtle.compare() do
		turtle.dig()
		up()
	end
	while not turtle.detectDown() do
		turtle.down()
	end
end
———————————————————————————————————————————————————–
If I could get some answers that would be nice :)/>
The code I am referencing is here:

————————————————————————————————————————————————————

function up()
	while not turtle.up() do
		turtle.digUp()
	end
end

function cutTree()
	while turtle.compare() do
		turtle.dig()
		up()
	end
	while not turtle.detectDown() do
		turtle.down()
	end
end

function search(dist)
	while dist > 0 do
		if turtle.compare() then
			cutTree()
		elseif not turtle.forward() then
			up()
		else
			dist = dist - 1
			while not turtle.detectDown() do
				turtle.down()
			end
		end
	end
end

local args = {...}
if #args ~= 1 then
	print('Usage: lumberjack <size>')
	return
end

turtle.select(1)
if turtle.getItemCount(1) == 0 then
	print('ERROR: Requires unrefined wood block in slot #1.')
	return
end

for n = 1, args[1], 1 do
	search(n)
	turtle.turnRight()
	search(n)
	turtle.turnRight()
end
crud41 #2
Posted 24 December 2012 - 07:35 PM
Also how do I use the function tag to its fullest?

function up()
while not turtle.up() do
turtle.digUp()
end
end

like right here this cide says for the function up() which isnt a turtle command.
Also, Will I get errored out if I use excessive end tags?
ChunLing #3
Posted 24 December 2012 - 07:54 PM
When you run a program, it is packed as a function and passed any command line parameters as an argument table.

We create a variable and assign it that argument table by using "local args = {…}"

If you do not know about tables, what they are, how to use them, etc, then please read up on Lua. They are far too involved a topic for us to explain them from scratch.

The same applies to for loops and all other control structures.

The shell allows you to run programs, files containing Lua code. You can get help on these by using the "help" program, you can list them by using the "programs" program, and you can view them by using the edit program.

You can also write your own programs (using edit when in-game or by opening the computer's files in saves/savename/computer/ID, where savename is the name of the save and ID is the ID of the computer, in another editor).

The function tag is use to declare and assign a variable which contains a function, which you can then call elsewhere. Functions are essential to writing anything but the simplest programs, but as there is no limit to how useful they can be, there is no "to their fullest".

You will get an "EOF expected" error if you use too many end tags.
crud41 #4
Posted 24 December 2012 - 08:02 PM
When you run a program, it is packed as a function and passed any command line parameters as an argument table.

We create a variable and assign it that argument table by using "local args = {…}"

If you do not know about tables, what they are, how to use them, etc, then please read up on Lua. They are far too involved a topic for us to explain them from scratch.

The same applies to for loops and all other control structures.

The shell allows you to run programs, files containing Lua code. You can get help on these by using the "help" program, you can list them by using the "programs" program, and you can view them by using the edit program.

You can also write your own programs (using edit when in-game or by opening the computer's files in saves/savename/computer/ID, where savename is the name of the save and ID is the ID of the computer, in another editor).

The function tag is use to declare and assign a variable which contains a function, which you can then call elsewhere. Functions are essential to writing anything but the simplest programs, but as there is no limit to how useful they can be, there is no "to their fullest".

You will get an "EOF expected" error if you use too many end tags.
Alright, I will read up. Thanks for the links