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

Help combining turtle commands

Started by RebaWho, 16 December 2012 - 05:33 PM
RebaWho #1
Posted 16 December 2012 - 06:33 PM
Very very new to minecraft and computercraft.
Years ago I did Tcl scripts for eggdrops.

I am working on a turtle mining program.

I think I should be able to combine several commands into one,
but I can't find any docs to show me the command structure for this.

Example:
Instead of telling the turtle over and over in the script - turtle.right() turtle.down() turtle.whatever()

I think I should be able to tell the script to combine these into something like:
local b_com = {"right", "down", "whatever",}
for i = 1,5 do
turtle.[b_com]()
end

Or something like it??

Thanks in advance!
-Rebecca :)/>
theoriginalbit #2
Posted 16 December 2012 - 06:43 PM
this is your best option. it uses function pointers


local b_com = { turtle.right, turtle.down }

for _, v in pairs( b_com ) do
  v()
end

hope this helps :)/>
Cloudy #3
Posted 16 December 2012 - 09:34 PM
Nah - that's not the best way of doing it (you can't guarantee pairs order).


local function b_com(times)
  for i = 1, times do
    turtle.turnRight()
    turtle.down()
  end
end

b_com(5)
theoriginalbit #4
Posted 16 December 2012 - 09:39 PM
Nah - that's not the best way of doing it (you can't guarantee pairs order).


local function b_com(times)
  for i = 1, times do
	turtle.forward()
	turtle.turnRight()
  end
end

ahhh oops. forgot about pairs not keeping order. lol. but even that wouldn't work how they put forth.

id have the same as mine, but use your for, making it


local b_com = { turtle.right, turtle.down }

for i = 1, #b_com do
  b_com[i]()
end
Cloudy #5
Posted 16 December 2012 - 10:09 PM
They were only using tables since they didn't know about functions, by the looks of it.
Orwell #6
Posted 16 December 2012 - 10:19 PM
TheOriginalBIT's example should work just fine. But as for the approach of the OP, this is sort of what you were trying to do:

local b_com = {"right","up","down"}
local count = 5
for i=1,count do
  for _,v in ipairs(b_com) do	  -- ipairs does keep order
    turtle[v]()
  end
end
Cloudy #7
Posted 16 December 2012 - 10:20 PM
Yeah, both definitely work :P/> I just think if the functions you use are the same every time, would be faster to just hardcode into a function.
Kingdaro #8
Posted 16 December 2012 - 10:22 PM
Nah - that's not the best way of doing it (you can't guarantee pairs order).


local function b_com(times)
  for i = 1, times do
    turtle.turnRight()
    turtle.down()
  end
end

b_com(5)
For me, in almost every case, pairs has always traversed through tables in the order they have been defined/appended.

Though it is faster and usually more safe (even when there's basically no considerable risk of un-order-ness) to use a numeric for.
Orwell #9
Posted 16 December 2012 - 10:42 PM
Yeah, both definitely work :P/> I just think if the functions you use are the same every time, would be faster to just hardcode into a function.
I definitely agree. The OP had code close to a possible solution and I wanted to show how little he was off. :)/> But I also recommend just using function calls, it's basically the same and it's more intuitive. Also, for a nice example on this, look at the dance program in /rom/programs/turtle/dance. It uses a combination of both techniques and should give you a little bit of insight on this.
RebaWho #10
Posted 17 December 2012 - 05:53 AM
Thanks! :D/>

The combo one that combined what Cloudy and OriginalBIT suggested, did the trick.

Thank YOU!

P.S. Yipey! I just discovered pastebin.com ohhhhhh yeah baby! "pastebin get j0XXJcSn mult"
FTW!

Thanks again
-Reba

Cloudy and OriginalBIT combo script:
local b_com = { turtle.right, turtle.down }

for i = 1, #b_com do
b_com[i]()
end