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

Is it possible to queue functions?

Started by SoliceEclipse, 12 November 2014 - 04:23 AM
SoliceEclipse #1
Posted 12 November 2014 - 05:23 AM
I'm a bit of a dwarf when it comes to minecraft but digging underground rooms by hand can be tedious so I wrote a turtle program that makes rooms and hallways for me. My question is, "Is it possible to queue several rooms and have the turtle dig them out one at a time?" The number of rooms would be user defined and the dimensions of each room would also be user defined.

Basically, in the event my base needs an expansion, I want to be able to place down a turtle, tell it I want a hallway of x dimensions, a room at the end of it with y dimensions, another hallway off of that with z dimensions, and so on.

I already have the code to dig the rooms themselves, I just need a way to queue them up before the turtle starts digging.
I also have a basic menu that asks for the dimensions of the room.
As my code stands, the turtle asks for dimensions, digs out the room, then when it's done digging, asks for dimensions again. I want to give it the dimensions for all of the rooms (however many I happen to need at the time) and then have it start digging so I don't have to babysit it as much.

I'm fairly new to programming so I'm sure there is a simple solution, I just have no clue what it is.
Bomb Bloke #2
Posted 12 November 2014 - 06:14 AM
The short answer is yes - the average script already is a bunch of queued functions, after all.

The longer answer is that there's many different ways to do this, and the best is purely up to personal taste.

I'd rig the script to support command line arguments. You know how when you run the default "excavate" script, you're expected to add a number to the command to indicate the size of the quarry? The idea would be to get your script to work the same way, thus allowing you to write another script which runs the first over and over again.

For eg:

local tArgs = {...}  -- Get all arguments passed to the script, bung them in a table.

local width, height, length

if not tArgs[1] then
  -- First argument wasn't specified, ask for width:
  print("Width?")
  width = tonumber(read())
else
  -- Get the data from the command line:
  width = tonumber(tArgs[1])
end

if not tArgs[2] then
  -- Second argument wasn't specified, ask for height:
  print("Height?")
  height = tonumber(read())
else
  height = tonumber(tArgs[2])
end

(and so on)

So say you run the script like this:

myScript 4 5 3

It'll set width to 4, height to 5 and length to 3. If you just run it like this:

myScript

… then it'll ask you to type the figures in one at a time.

This allows you to make a script like this:

shell.run("myScript 5 3 5")
(move turtle to position for next room)
shell.run("myScript 4 7 3")
(move turtle to position for next room)
(etc)

Again, there are lots of other ways to do this - but hopefully this gets you started. :)/>
SoliceEclipse #3
Posted 12 November 2014 - 07:16 AM
I ended up getting it to work the way I want it to :3 thank you for your time.