Jump to content




How to use tables with rednet to make AutoCrafting system

turtle peripheral wireless

7 replies to this topic

#1 Angry_Dragonoid

  • Members
  • 17 posts
  • LocationGoshen, IN

Posted 09 October 2015 - 09:44 PM

Hey,
I was thinking. Me and my friend are playing on a server with each other, and we are mainly using Industrial Craft 2 for our machinery. I wanted to some up with a system for a turtle that you can have a GUI type thing, and originally, this has no recipes. You can add recipes by selecting, "New Recipe". This will then display a message saying, "Please create the item in my inventory, then click, "Add Recipe". This will then allow the player to create the recipe in the turtle's inventory and then the player can click well...as above. Anyway, This will then save the recipe into the turtle somehow. If this is not possible then I will not bother with it anymore. But if this is possible, please reply and I will state the rest of my idea. If this is possible, and I can get tables explained how to use and what they're used for, what I could make with them, etc. Also Rednet...idk if I'm just not understanding the wiki, or if I'm missing something. What I want is to be able to send requests to turtles for item sorting/retrieving.

Thanks in advance!
Angry_Dragonoid

#2 TYKUHN2

  • Members
  • 210 posts
  • LocationSomewhere in this dimension... I think.

Posted 09 October 2015 - 09:57 PM

rednet.open("left")
rednet.send(0, "Hi!")
rednet.recieve(5)
The code above opens a wireless modem on the left side, sends the "Hi!" message to computer number 0 (using the getId console command) and waits 5 seconds to recieve a message before closing.
Rednet.send can send tables.
local recipees = {}
function addRecipee(product, ingredients)
	recipees[product] = ingredients
end
Inserts the ingredients under a key of the product into a table.
recipees[product]
Returns table of ingredients
Probably don't do the same function I did, it is a bit too small. The GUI requires design choices so I cannot really help you there.
There are crafty turtles that craft items so they should be the only things that need retrieving and can be interfaced with.
rednet.send(0, recipees)
Sends the recipees table to computer number 0
Any questions? Your question is a bit long and varied for me so I may have missed something.

Edited by TYKUHN2, 09 October 2015 - 10:17 PM.


#3 valithor

  • Members
  • 1,053 posts

Posted 09 October 2015 - 10:16 PM

View PostTYKUHN2, on 09 October 2015 - 09:57 PM, said:

local recipees = {}
function addRecipee(product, ingredients)
	table.insert(recipees, product, ingredients)
end

Might want to check out how table.insert works again.

It has 3 possible arguments, but only 2 are required.

argument 1 is always the table that you want to insert into
argument 2 is either the thing you want to insert into the table, or the numerical value that you want to insert the third argument as
argument 3 should only be there if you put a number as argument 2. This argument is what you want to insert into the table at the given value.


As for the question. From what I understand, you are wanting to know if the idea to have a computer running one program, and a turtle running another, where on the computer there is a gui, where you can insert new recipes, ask for a given recipe to be made, or sort/transport items, and the turtle will then go and do it. Simply put yes this is possible, however, if you do not have any coding experience I would suggest starting on something a little more basic.

Tables:
Tables are a incredibly powerful part of LUA. You can think of them as a variable with a lot of smaller variables inside of it, which are sorted by a key, value system. The key is the name of the variable inside of the table, and the value is just what the variable is equal to.

Example:
myTable = {}
myTable["this is a key"] = "this is a value" --# assigning the value "this is a value" to the key "this is a key"

print(myTable["this is a key"]) --# will print "this is a value"

Rednet:

So as TYKUHN2 said you can send tables in rednet, which makes it easy to transfer the information from the computer to the turtle. It is simply:

On the turtle:
rednet.open(side) --# side is the side the wireless modem is on, this only needs to be called once at the beginning of the program

id, message = rednet.receive() --# message will equal the first message the computer receives.  It is generally good to check the id to make sure it is from the correct computer.
--# id is the computer id of the computer that sent the message

on the computer:
myTable = {}

--# put stuff in the table somewhere around here

rednet.send(id, myTable)  --# sending the myTable table, to the computer with id "id", you would need to change the id variable here to the id of the turtle

Storing a table:
You change a table into text form by using textutils.serialize(tbl)

example:
myTable  = {"hi", "bye", "another value"}

print(textutils.serialize(myTable)) --# will print a text form of the table

To save the table:
myTable = {"hi", "bye", "another value"}

h = fs.open("file","w") --# opening file "file" for writing "w"
h.write(textutils.serialize(myTable)) --# writing the table to the file
h.close() --# closing and saving the file

To access the saved file:
h = fs.open("file","r") --3 opening file "file" for reading "r"
contents = h.readAll() --# reading all of the contents of the file, and saving it to the a variable
h.close() --# closing the file

myTable = textutils.unserialize(contents) --# transforming the text back into a table

Edited by valithor, 09 October 2015 - 10:17 PM.


#4 TYKUHN2

  • Members
  • 210 posts
  • LocationSomewhere in this dimension... I think.

Posted 09 October 2015 - 10:17 PM

What error was there again? :P

#5 valithor

  • Members
  • 1,053 posts

Posted 09 October 2015 - 10:20 PM

View PostTYKUHN2, on 09 October 2015 - 10:17 PM, said:

What error was there again? :P

tbl = {}

table.insert(tbl, "key", "value") --# errors, bad argument #2 to 'insert' (number expected got string)

table.insert can only be used for numerical entries

example:
tbl = {}

table.insert(tbl, "hi")
table.insert(tbl, "bye")

print(tbl[1]) --# prints hi
print(tbl[2]) --# prints bye

table.insert(tbl, 3, "another value")

print(tbl[3]) --# prints "another value"

Edited by valithor, 09 October 2015 - 10:22 PM.


#6 Angry_Dragonoid

  • Members
  • 17 posts
  • LocationGoshen, IN

Posted 10 October 2015 - 03:13 AM

The thing is...I didn't want a computer for the main GUI...I wanted a turtle. And what you would would do would take the items for the recipe, and put them in order as if you were crafting them. Then the turtle would read the items, and save them into a table somewhere. Thus allowing you to choose the recipe later, and the turtle would send out a rednet signal to other turtles all placed under barrels. They will then take out the needed items and send them into a pipe system of some kind. Then the main tutrtle would wait for the item before it sends the next request. Thing is...that would both be both a lot of turtles, and a lot of id changing in the programs. You can probably set something to auto detect the id and use the id accordingly. My idea would also save on fuel for turtles. But unless playing on infinite range configuration you would need the storage to be fairly close to the main turtle. Ill work on a bit of a presentation when incandescent with pictures of my ideas. Maybe then you can help me come up with the proper code forvthis. I am fairly advanced in luau already. I have done a lot of automating with it. And I think this could great with some things like my mining system and ore processing system. Basically the Snatcher turtles are just a barrel with a turtle under it, with a cheat under it, with piping from that. Then the main turtle will have chest either above or below it. Thanks in advance! Angry_Dragonoid

#7 TYKUHN2

  • Members
  • 210 posts
  • LocationSomewhere in this dimension... I think.

Posted 10 October 2015 - 03:57 AM

rednet.open("left")
rednet.send(0,"redstone", "Send")
Sends a message on the left side wireless modem (which I think turtles are the.... back? maybe?) and the message is "redstone" with a protocol of "send" which could be read as "Send redstone" in a program.
local recipees = {}
function addRecipee(product, ingredients)
#--Some more code
recipees[product] = ingredients end
where product is the name the turtle gives the item and ingredients is also the name the turtle gives the ingredients
Example ingredients is a Sign (Where the names are psuedo-names not the actual names)
ingredients = {
"planks", "planks", "planks",
"planks", "planks", "planks",
"", "stick", ""
"", "stick", ""
}
recipees["sign"] = ingredients #-- see above.

PS:
The person who recommended mashing the lightswitch button...
It didn't work :(

Edited by TYKUHN2, 10 October 2015 - 03:57 AM.


#8 valithor

  • Members
  • 1,053 posts

Posted 10 October 2015 - 04:29 AM

It may not be your intention, but it sounds more or less like you are making a program request rather than a request for help in writing it. Very few times will someone write a program for you in this section (point of the mod is to learn how to code :P), but we are more than happy to help you overcome issues you have while you make it. With that said I will provide you with a little snippet to get started.

What the goal of these functions are is explained below:
local function scanSlot(slot)
  local item = turtle.getItemDetail(slot)
  return item.name or nil
end

local function scanRecipe(product)
  local recipe = {}
  for i = 1, 3 do
	recipe[i] = scanSlot(i)
  end
  for i = 5, 7 do
	recipe[i] = scanSlot(i)
  end
  for i = 9,11 do
	recipe[i] = scanSlot(i)
  end
  return {product,recipe}
end

local function countScanned(tbl)
  local counted = {}
  for k,v in pairs(tbl) do
	if counted[v] then
	  counted[v] = counted[v]+1
	else
	  counted[v] = 1
	end  
  end
  return counted
end

local function make(recipe)
  local slots = {}

  for i = 1, 16 do
	slots[i] = scanSlot(i)
  end

  for k,v in pairs(recipe) do
	local item = v
	for i = 1, 16 do
	  if slots[i] == item then
		turtle.select(i)
		turtle.transferTo(k,1)
		break
	 end
	end
  end
end

So now that I have these 4 functions let me explain what each of them do.

The first one, "scanSlot" is just a utility function, which is used by scanRecipe and make in order to cut down the number of lines.
The second one will loop through the 3x3 square of slots in the top left of the turtle (these are the ones that are used by crafty turtles to craft), and records them to a table, which will keep track of what item was in what slot.
The third function makes it to where you can count the total amount of each item that the recipe needs. This will be useful for when you are requesting the items from the other turtles.
The last function assembles the recipe in the turtle assuming it has enough of each item to craft it.

Although I did not put in any error checking in these functions, it would probably be smart to.

Example of them in use:
local iron_pickaxe = scanRecipe("iron_pickaxe") --# iron_pickaxe contains a table which has the name of the item in it and the formed recipe
local iron_pickaxeCounted = countScanned(iron_pickaxe[2]) --# iron_pickaxeCounted contains a table with the total amount of each item in it, in this example it will have 2 sticks and 3 iron ingots

--# assuming the turtle has the items

make(iron_pickaxe[1]) --# assembles the pickaxe in the turtles inventory (this function does not have error checking that it probably should have, was trying to keep it simple)

With these four functions, you have more than enough information to make a significant amount of progress into your program. The only thing we have not talked about is a GUI, but i personally hate dealing with them, so I will leave that up to someone else.

Edited by valithor, 10 October 2015 - 04:33 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users