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

needing examples.

Started by thetrin777, 12 July 2015 - 03:16 AM
thetrin777 #1
Posted 12 July 2015 - 05:16 AM
Hello every one! I haven't been on here in a bit. But it's a new day and I am back on the lua band wagon!

So I decided to start testing my creativity on some turtles. I've got most of the basic ideas of movement and inventory interaction. I would like to start expanding my turtles to do more interesting things. However, some of the ideas I have, II've almuch fun have no idea how to make them take shape as far as the code goes. I was wondering if someone could maybe either:

1. Provide me with a "template" code to get started with
Or
2. Point me towards some good resources that will help me achieve my goals.


That said let's begin.

1. How would I go about making a turtle remember it's position in the world (coordinate wise) and return to that spot once prompted to?

2. When making a turtle pull from or drop into a chest, is there a way to specify which slot in the chest it pulls from/drops to? Is there a way to specify the quantity?

3. Let's say I have a turtle building a house. Is there a way to monitor the materials it has on hand, and have it sent back to a monitor of some sort?

4. Let's say a turtle is building, but runs into a problem. Is there a way to have it pick up where it stopped once the issue is resolved?

For now that's all I got. Thanks in advance for reading this, and all help is massively appreciateed. BTW, sorry if some of this has been discussed elsewhere. I really wasn't sure how to search for this stuff. If you have links to any reference material on these matters, feel free to share. i want to learn as much as possible and get better at coding. its so much fun, but it gets challenging.
KingofGamesYami #2
Posted 12 July 2015 - 05:58 PM
1. How would I go about making a turtle remember it's position in the world (coordinate wise) and return to that spot once prompted to?

There are a couple of ways to do this.

A) Check out GPS, specifically gps.locate. It will query nearby computers that are hosting the gps, and find it's position based upon the distance from those computers. This will not work if you are out of range of the hosts. After you find out where the turtle is, you can move it forward and locate again to find the direction it is facing. You can then point your turtle towards the point you want to go, and move there.

B)/> Overriding the movement functions of the turtle (turtle.forward, turtle.turnRight, …) will allow you to record where the turtle is at any time. Using this, you can assume the turtle starts at 0,0,0, attempt to use gps to locate the initial position, or prompt the user for coordinates. This has the benefit of being totally self-sufficient, with no gps network needed. I've written an API that uses this technique, if you wish to use it. In addition to using this technique, my API also saves the coordinates to a file. This allows the coordinates to be preserved between shut-downs of the turtle (..which happens when the chunk unloads).

2. When making a turtle pull from or drop into a chest, is there a way to specify which slot in the chest it pulls from/drops to? Is there a way to specify the quantity?

turtle.drop and turtle.suck both accept a single parameter, the number of items it will attempt to drop/suck. Unfortunately, with vanilla CC, there isn't a way to specify the slot. However, if you have OpenPeripherals, you can wrap the chest as a peripheral and use it's methods to do this.

3. Let's say I have a turtle building a house. Is there a way to monitor the materials it has on hand, and have it sent back to a monitor of some sort?
If you have a newer version of CC, you can use turtle.getItemDetail to monitor the materials in the turtle, if you don't you can use turtle.getItemCount to detect how many of an item is in a given slot. Once the turtle knows what it contains, displaying on a monitor is a simply matter of transmitting the data via rednet to a computer, which can then display the data on the monitor.

4. Let's say a turtle is building, but runs into a problem. Is there a way to have it pick up where it stopped once the issue is resolved?

Definitely, if your program allows for this. I'd start by always checking if the turtle can move forward. turtle.forward returns true if the turtle moved forward, false if the turtle did not. A common bit of code for dealing with problems while moving is this:

local function forward()
  while not turtle.forward() do
    turtle.dig()
    turtle.attack()
  end
end

As you can see, the turtle will attempt to move forward, and if it did, everything inside the loop is essentially skipped. If it didn't move forward, it will attempt to remove the obstruction by digging it and attacking it (the attack is for entities, such as a zombie).

If the turtle runs out of a certain material, you could have a loop waiting for the user to press 's'. Or you could wait for a turtle_inventory event. Or both!

The best way to search the forums is to use google search with the site parameter. For example

Another good bet is the computercraft wiki. This page was very useful to me when I was learning. The Lua 5.1 Reference Manual is a great resource for lua in general, but the version of lua CC uses is slightly different (specifically, it's LuaJ) and contains a few bugs (mostly in the string functions).
Edited on 12 July 2015 - 03:59 PM
thetrin777 #3
Posted 12 July 2015 - 08:39 PM
Thanks for the quick reply, Yami!


1. I'll definately check into using the GPS. I've heard people talk about it, but I've never really tried it out. However, I am curious about your API. Do you have a link to where I could find it at? And, does it contain instructions on how it works?

2. I forgot about OpenPeripherals. =P I've used it before, but this is a new modpack I built myself, so I should probably get that back.

3. I guess I should have provided that detail. I'm using CC 1.73 and Minecraft 1.7.10. That's exactly what I was wondering, but I gotta brush up on my rednet networking. I haven't made alot with it, and when I did the code I used was kinda built for me. That's what the wiki is for, lol.

4. That makes sense. I actually didn't know how to use the "not" function, but I read up on it a bit. Stil not 100%, but I am gonna build a few test codes and look at some other codes on the forums and on the net.

I'll definately start referencing the wiki more, but these examples are definately moving me in the right direction. =] If you don't mind one more question, I'd appreciate it.

Idk if you've ever seen the lucky clover mod, but what it does is spits out a random item when you Shift+RightClick with it in your hand. (You get them from breaking grass like seeds) The thing is though, some items it drops out can harm you, like uranium. (You can change it in the configs, but I don't wanna =P) So, I made a basic machine using an autonomous activator to drop the item in front of a turtle, and the turtle picks up 16 items, then turns to the right and drops them into a chest, then turns back the way it was facing.

What I want to make it do is, instead of having to run the code over and over, I want the turtle to detect if there is an item in front of it. if there is, then it picks it up. Once all 16 inventory slots have an item in it, I want it to turn and empty into the chest, then turn back and wait for more items. That way, I only run it once and it works without my intervention.

Here's the code that I have so far:


function Store()
turtle.turnRight()
for i = 1,16 do
	 turtle.select(i)
	 turtle.drop()
end
turtle.turnLeft()
end
function PickUp()
for i = 1,16 do
	  turtle.suck()
end
end
Pickup()
Store()


Like I said, it works, but I wanna make it better.
KingofGamesYami #4
Posted 13 July 2015 - 12:08 AM
1. I'll definately check into using the GPS. I've heard people talk about it, but I've never really tried it out. However, I am curious about your API. Do you have a link to where I could find it at? And, does it contain instructions on how it works?

Github link.

Instructions are in the README, the actual code is in lama.lua. There's plenty of information on Hive's wiki about it, if you need further help you can ask (I did write it, after all..)
What I want to make it do is, instead of having to run the code over and over, I want the turtle to detect if there is an item in front of it. if there is, then it picks it up. Once all 16 inventory slots have an item in it, I want it to turn and empty into the chest, then turn back and wait for more items. That way, I only run it once and it works without my intervention.

So… something like this?

local function isFull() --#checks for free slots
  for i = 1, 16 do
    if turtle.getItemCount( i ) == 0 then --#if one is found
      return false --#returns false
    end
  end
  return true --#else, returns true
end

while true do --#infinitely loop
  while not isFull do --#while the inventory isn't full
    turtle.suck() --#suck items
  end
  turtle.turnRight() --#turn, drop items, turn back
  for i = 16, 1, -1 do --#this ends with slot 1 selected.  Not something necessary, but I like it
    turtle.select( i )
    turtle.drop()
  end
  turtle.turnLeft()
end

Edit: Just realized… I forgot if turtle.suck is one of the turtle commands that yields. I'm fairly certain it does, but if you get a too long without yielding error, that loop is the culprit. Simply add an os.queueEvent followed by an os.pullEvent to fix this.
Edited on 12 July 2015 - 10:12 PM
thetrin777 #5
Posted 13 July 2015 - 05:00 AM
Your API is pretty awesome! I'ma do some playing with it once I'm done with this first machine. If I end up with any questions (Which I probably will, lol) I'll start another thread for just that.


I tested the code you gave me, and it works!! …mostly. lol

Once the chest fills, the turtle starts cycling through the inventory, and turing sporadically. It's kinda funny lol.

So here's where I can quiz myself on if I'm learning or not.

My thought on the solution to this issue would be to make the turtle check the chest by wrapping it with openPeripherals, tell it to check all the inventory slots in it for if there is an item or not, and if it comes back that all of the slots are full, the turtle will print a message that says "Chest is full! Please empty before continuing…" then it checks to see if the chest has empty slots or not. And if it does, it resumes the code it was doing previously.

Am I on the right track?
KingofGamesYami #6
Posted 13 July 2015 - 05:19 AM
Yes. You may be interested to know, if you wish to exit a loop you can use the keyword 'break'. For example:


while true do
  local input = read()
  if input == "hi" then
    break
  end
end
print( "loop has ended" )

This can be useful in situations like the one you've found yourself in.