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

Seeking opinions on my logging script.

Started by dmc, 16 March 2013 - 04:38 AM
dmc #1
Posted 16 March 2013 - 05:38 AM
Hello I'm a new user here and few days ago I found out about computercraft in minecraft and got really hooked so I made my first turtle logger and would like to know what you think about it. ( and btw it works with all kind of trees 1x and 4x big ones)

images:

How to get the code into turtle :

http://oi49.tinypic.com/2rhwg9e.jpg

First two lines correspond to : Distance(x blocks in front) , Lenght(x blocks to the left) change w/e you need for example 10x10 area you should change values
to Sizelenght =9
sizeFont=9
and it will search in area 10x10 for any trees chop them and come back to the initial position.

http://oi47.tinypic.com/2nu3o9l.jpg

screenshot:

http://oi46.tinypic.com/zv1jz7.jpg

comes back to initial position:

http://i45.tinypic.com/2hi32n6.jpg

pastebin(code) : http://pastebin.com/H2Hvf5eN

( I'm planning on adding auto-storage,auto-refill and maybe something more)

Grateful for any replies :)/>

Best Reguards
Lyqyd #2
Posted 16 March 2013 - 05:40 AM
Split into new topic. A title was provided for you.
Pharap #3
Posted 16 March 2013 - 04:24 PM
Not sure why this ended up in ask a pro if we're only supposed to be giving opinions.
It's pretty decent for a beginner, and it seems to get the job done.
One thing I will say, since functions are actually types in lua, instead of doing this:

function detectx()
			    return turtle.detect()
	    end
You would be better off doing this:

local detectx = turtle.detect
If you run this:

local detectx = turtle.detect
print("turtle.detect:")
print(tostring(turtle.detect))
print("detectx:")
print(tostring(detectx))
print("print:")
print(tostring(print))


detectx = print
print("turtle.detect:")
print(tostring(turtle.detect))
print("detectx:")
print(tostring(detectx))
print("print:")
print(tostring(print))

detectx("Hello World")
detectx = turtle.detect
print(tostring(turtle.detect()))
print(tostring(detectx()))
You will see that both detectx and turtle.detect are referencing the same function, but print is a different function.
However when you assign detectx to print, detectx is now referencing print instead, and can be used instead of print.
Sorry if you already knew this, but it's good to see we're still getting more people interested in getting involved in computercraft.

I'm assuming you've learnt a programming language like C before learning lua. (There's a few things in your code that give away that lua isn't your first language).
In fact, just to be awkward, I'm going to guess you usually use Java. I might be wrong, but it's always fun to guess lol
dmc #4
Posted 17 March 2013 - 01:54 AM
Not sure why this ended up in ask a pro if we're only supposed to be giving opinions.
It's pretty decent for a beginner, and it seems to get the job done.
One thing I will say, since functions are actually types in lua, instead of doing this:

function detectx()
				return turtle.detect()
		end
You would be better off doing this:

local detectx = turtle.detect
If you run this:

local detectx = turtle.detect
print("turtle.detect:")
print(tostring(turtle.detect))
print("detectx:")
print(tostring(detectx))
print("print:")
print(tostring(print))


detectx = print
print("turtle.detect:")
print(tostring(turtle.detect))
print("detectx:")
print(tostring(detectx))
print("print:")
print(tostring(print))

detectx("Hello World")
detectx = turtle.detect
print(tostring(turtle.detect()))
print(tostring(detectx()))
You will see that both detectx and turtle.detect are referencing the same function, but print is a different function.
However when you assign detectx to print, detectx is now referencing print instead, and can be used instead of print.
Sorry if you already knew this, but it's good to see we're still getting more people interested in getting involved in computercraft.

I'm assuming you've learnt a programming language like C before learning lua. (There's a few things in your code that give away that lua isn't your first language).
In fact, just to be awkward, I'm going to guess you usually use Java. I might be wrong, but it's always fun to guess lol

Hello, and thanks for reply,

any information is welcome here , no need to be sorry and thanks for your time! : ))

The technical question is "local variable" inside in a "function" will work in other function inside of a function?


I did some Python,C#,C,C++,java,javascript,and Lua , but still a beginner though )), haha you guessed right : D
theoriginalbit #5
Posted 17 March 2013 - 02:03 AM
Statements where you do this

local detect = turtle.detect()
if detect==true then
  -- some code
elseif detect==false then
  -- some code
end
you can actually just do this

if turtle.detect() then
  -- some code
els
  -- some code
end

Also to refine some code down you could define in your verwood function some more functions such as up and down which move the turtle and update the height variable this will cleanup the code a lot.

Something a little cleaner to read (untested if it works, I only ever use cc-emu)
Edited on 17 March 2013 - 01:30 AM
Pharap #6
Posted 17 March 2013 - 07:24 AM
Hello, and thanks for reply,

any information is welcome here , no need to be sorry and thanks for your time! : ))

The technical question is "local variable" inside in a "function" will work in other function inside of a function?


I did some Python,C#,C,C++,java,javascript,and Lua , but still a beginner though )), haha you guessed right : D

If you mean will this will work:

local func1 = function()
 local var1 = "hello"
 local func2 = function()
  print(var1)
 end
end
The answer is yes.

Another good thing to know is that since functions can be referenced by variables, you can do 'closures' (functions that return functions)

local GetFunction = function()
  local counter = 0
  return function()
    counter = counter +1
    print(counter)
  end
end


The for loops are what give away that you normally do C-like languages.
In lua, tables (when used like arrays) have a first index of 1, whereas in C-like languages (C,C++,C#,Java,Javascript etc) arrays always start at 0.
Because people usually use for loops for cycling through arrays, the number they choose to start with can give away that lua isn't their first language because most lua users have their loops start at 1, not 0.

The Java thing was a guess based on some of the naming conventions you chose.

I'm glad to hear you've done some C# though, it's my favourite (and main) language.
How much have you learnt in the other languages you know?
dmc #7
Posted 17 March 2013 - 09:58 AM
Statements where you do this

local detect = turtle.detect()
if detect==true then
  -- some code
elseif detect==false then
  -- some code
end
you can actually just do this

if turtle.detect() then
  -- some code
els
  -- some code
end

Also to refine some code down you could define in your verwood function some more functions such as up and down which move the turtle and update the height variable this will cleanup the code a lot.

Something a little cleaner to read (untested if it works, I only ever use cc-emu)

Thanks for tips, its my first code here, i'll obviously will re-do the code so it will look cleaner, it is the 1st version of my personal logger so :)/> there is lots of ways to improove always :)/>.
dmc #8
Posted 17 March 2013 - 10:07 AM
Hello, and thanks for reply,

any information is welcome here , no need to be sorry and thanks for your time! : ))

The technical question is "local variable" inside in a "function" will work in other function inside of a function?


I did some Python,C#,C,C++,java,javascript,and Lua , but still a beginner though )), haha you guessed right : D

If you mean will this will work:

local func1 = function()
local var1 = "hello"
local func2 = function()
  print(var1)
end
end
The answer is yes.

Another good thing to know is that since functions can be referenced by variables, you can do 'closures' (functions that return functions)

local GetFunction = function()
  local counter = 0
  return function()
	counter = counter +1
	print(counter)
  end
end


The for loops are what give away that you normally do C-like languages.
In lua, tables (when used like arrays) have a first index of 1, whereas in C-like languages (C,C++,C#,Java,Javascript etc) arrays always start at 0.
Because people usually use for loops for cycling through arrays, the number they choose to start with can give away that lua isn't their first language because most lua users have their loops start at 1, not 0.

The Java thing was a guess based on some of the naming conventions you chose.

I'm glad to hear you've done some C# though, it's my favourite (and main) language.
How much have you learnt in the other languages you know?

Yes I see now where you found it)) and normally they always start at 0 " true statement" :D/>.

Well basically I've started with python at my course, then we moved to C# and after that we worked a lot with the interface and then on databases and C#, my last project was C# advanced calculator, didn't work much with it but it was beautifull on my opinion, the grade was 16 from 20 from this project.

and c++ only theorically.

javascript and java im learning now at C.E.T

Can't really tell how much I know programming, but maybe after some codding I can show some Decent "logic" later on :)/> "hope so".

p.s at the moment trying to do some codding at Unity 3D < loving it.
Pharap #9
Posted 17 March 2013 - 06:15 PM
Yes I see now where you found it)) and normally they always start at 0 " true statement" :D/>.

Well basically I've started with python at my course, then we moved to C# and after that we worked a lot with the interface and then on databases and C#, my last project was C# advanced calculator, didn't work much with it but it was beautifull on my opinion, the grade was 16 from 20 from this project.

and c++ only theorically.

javascript and java im learning now at C.E.T

Can't really tell how much I know programming, but maybe after some codding I can show some Decent "logic" later on :)/> "hope so".

p.s at the moment trying to do some codding at Unity 3D < loving it.

I prefer XNA to unity because I find it easier to modify and I prefer visual studio to work with.
I've only done helloworld in Java so far since me and Java don't get on.
I wrote a very basic roguelike in C++ though.
Very basic, just a character (no pun intended) moving around an area and wrapping round if it goes off the edge.

Main reason I asked:
If you know and like C#, that thing I mentioned about functions getting stored to variables applies to C# as well.
In C# there are things called delegates which basically act as references to functions, but they are strongly typed (ie to reference a function with a delegate you have to make sure the return types and argument types match)
There are some predefined delegates in System that you can use for quickness if you have

using System;
At the top of your code.

Here's an example:

using System;
namespace test
{
public class program
{
static int main(string[] args)
{
Func<string,void> print; //defines a reference to a function that takes a string argument and returns void
print = new Func<string,void>(Console.WriteLine); //defines print as a reference to Console.WriteLine
//You may now use print as if it were the print function in lua, such as:
print("Hello World");
//the same can be applied to read:
Func<string> read;// function will return string,
read = new Func<string>(Console.ReadLine);//references standard read line
string input = read();//like a strongly typed lua
}
}
}

It can come in quite handy for/with callbacks (which get used in javascript a lot), events (which you'll know about if you've done any EDP or worked with windows forms) and lambda expressions (technically functional programming, but C# has support for them).
They're also really good for modifiable objects. For example if you have an object that has a property of the type Func<string,void>, you can let other code change that object's function for something else, essentially just tacking on a new one. This also works like lua's ability to tack functions onto tables to emulate objects having functions.
Just something to toy with when you get chance. After all, the more you know about a language, the more possibilities there are.