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

Creating and using tables.

Started by DjTranceFire, 14 February 2015 - 10:43 AM
DjTranceFire #1
Posted 14 February 2015 - 11:43 AM
Hey there.
I'm currently trying to setup a room with CC to control powered spawner.
The idea behind this is to check the content of my ME for mob drops.
If there are not enough items from one type CC should give an rs output to start a spawner till enough of the items are inside of my ME.
For now everything is working fine but i'm stuck at creating and using tables for all the different items.
Currently my script looks like this:
Spoiler

local sSide = "left"
local me = peripheral.find("tileinterface")

local test = {
	color = colors.red,
	id = "Thaumcraft:ItemManaBean",
	nbt = "ad5cc1bd74d2230c5c483e4c1bbc7948",
	dmg = 0,
	qty = 10
}

local function updateList()
	local newlist = me.getAvailableItems()
	for num,item in pairs(newlist) do
		if test.nbt == "" then
			if item.fingerprint.id == test.id and item.fingerprint.dmg == test.dmg and item.size < test.qty then
				rs.setBundledOutput(sSide,colors.combine(rs.getBundledOutput(sSide),test.color))
			elseif item.fingerprint.id == test.id and item.fingerprint.dmg == test.dmg and item.size >= test.qty then
				rs.setBundledOutput(sSide,colors.subtract(rs.getBundledOutput(sSide), test.color))
			end
		else
			if item.fingerprint.id == test.id and item.fingerprint.dmg == test.dmg and item.fingerprint.nbt_hash == test.nbt and item.size < test.qty then
				rs.setBundledOutput(sSide,colors.combine(rs.getBundledOutput(sSide), test.color))
			elseif item.fingerprint.id == test.id and item.fingerprint.dmg == test.dmg and item.fingerprint.nbt_hash == test.nbt and item.size >= test.qty then
				rs.setBundledOutput(sSide,colors.subtract(rs.getBundledOutput(sSide), test.color))
			end
		end
	end
end


local function startScan()
	rs.setBundledOutput(sSide, 0)
	while true do
		updateList()
		sleep(1)
	end
end

startScan()

With this script i can check the item inside "test" and its working.
The problem is that i need more then one item so i need more then one table.
It would look something like this:

Spoiler

leather = {
	color = colors.red,
	id = "minecraft:leather",
	nbt = "",
	dmg = 0,
	qty = 64
}

feather = {
	color = colors.green,
	id = "minecraft:feather",
	nbt = "",
	dmg = 0,
	qty = 64
}

blazerod = {
	color = colors.brown,
	id = "minecraft:blaze_rod",
	nbt = "",
	dmg = 0,
	qty = 128
}

witherskull = {
	color = colors.blue,
	id = "minecraft:skull",
	nbt = "",
	dmg = 1,
	qty = 30
}

inksack = {
	color = colors.purple,
	id = "minecraft:dye",
	nbt = "",
	dmg = 0,
	qty = 128
}

wool = {
	color = colors.cyan,
	id = "minecraft:wool",
	nbt = "",
	dmg = 0,
	qty = 128
}

bones = {
	color = colors.lightGray,
	id = "minecraft:bone",
	nbt = "",
	dmg = 0,
	qty = 128
}

gunpowder = {
	color = colors.gray,
	id = "minecraft:gunpowder",
	nbt = "",
	dmg = 0,
	qty = 256
}

slimeball = {
	color = colors.pink,
	id = "minecraft:slime_ball",
	nbt = "",
	dmg = 0,
	qty = 128
}

ghasttear = {
	color = colors.lime,
	id = "minecraft:ghast_tear",
	nbt = "",
	dmg = 0,
	qty = 64
}

pinkslimeball = {
	color = colors.yellow,
	id = "MineFactoryReloaded:pinkslime",
	nbt = "",
	dmg = 0,
	qty = 72
}

mana_ignis = {
	color = colors.lightBlue,
	id = "Thaumcraft:ItemManaBean",
	nbt = "ad5cc1bd74d2230c5c483e4c1bbc7948",
	dmg = 0,
	qty = 256
}

mana_terra = {
	color = colors.lightBlue,
	id = "Thaumcraft:ItemManaBean",
	nbt = "bb9211b894dea99a6b674ebe63759333",
	dmg = 0,
	qty = 256
}

mana_aer = {
	color = colors.lightBlue,
	id = "Thaumcraft:ItemManaBean",
	nbt = "64630f917da3a7214b1ef2c91a90d090",
	dmg = 0,
	qty = 256
}

mana_aqua = {
	color = colors.lightBlue,
	id = "Thaumcraft:ItemManaBean",
	nbt = "358c318e194980b0c0835a9f1ce41750",
	dmg = 0,
	qty = 256
}

mana_ordo = {
	color = colors.lightBlue,
	id = "Thaumcraft:ItemManaBean",
	nbt = "a77c4b35bbb1a4c84b15122dd52354bf",
	dmg = 0,
	qty = 256
}

mana_perditio = {
	color = colors.lightBlue,
	id = "Thaumcraft:ItemManaBean",
	nbt = "18add493aeeb97a3fe71cf8130fe8675",
	dmg = 0,
	qty = 256
}


The biggest problem for me on this point is to check every item i need.
I have no clue how to create a single table that contains all these items and run the "updateList()" function for all of these.
I realy hope someone here can help me to fix this problem and hopefully finally to understand this tables.. :/

Sorry for my bad english, i hope its not to hard to understand.
valithor #2
Posted 14 February 2015 - 04:46 PM
It occurs to me that you did not post the entire script, but you should be able to do something like this.

local items = {leather ={
		color = colors.red,
		id = "minecraft:leather",
		nbt = "",
		dmg = 0,
		qty = 64
},

feather = {
		color = colors.green,
		id = "minecraft:feather",
		nbt = "",
		dmg = 0,
		qty = 64
},

blazerod = {
		color = colors.brown,
		id = "minecraft:blaze_rod",
		nbt = "",
		dmg = 0,
		qty = 128
},

witherskull = {
		color = colors.blue,
		id = "minecraft:skull",
		nbt = "",
		dmg = 1,
		qty = 30
},

inksack = {
		color = colors.purple,
		id = "minecraft:dye",
		nbt = "",
		dmg = 0,
		qty = 128
},

wool = {
		color = colors.cyan,
		id = "minecraft:wool",
		nbt = "",
		dmg = 0,
		qty = 128
},

bones = {
		color = colors.lightGray,
		id = "minecraft:bone",
		nbt = "",
		dmg = 0,
		qty = 128
},

gunpowder = {
		color = colors.gray,
		id = "minecraft:gunpowder",
		nbt = "",
		dmg = 0,
		qty = 256
},

slimeball = {
		color = colors.pink,
		id = "minecraft:slime_ball",
		nbt = "",
		dmg = 0,
		qty = 128
},

ghasttear = {
		color = colors.lime,
		id = "minecraft:ghast_tear",
		nbt = "",
		dmg = 0,
		qty = 64
},

pinkslimeball = {
		color = colors.yellow,
		id = "MineFactoryReloaded:pinkslime",
		nbt = "",
		dmg = 0,
		qty = 72
},

mana_ignis = {
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "ad5cc1bd74d2230c5c483e4c1bbc7948",
		dmg = 0,
		qty = 256
},

mana_terra = {
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "bb9211b894dea99a6b674ebe63759333",
		dmg = 0,
		qty = 256
},

mana_aer = {
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "64630f917da3a7214b1ef2c91a90d090",
		dmg = 0,
		qty = 256
},

mana_aqua = {
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "358c318e194980b0c0835a9f1ce41750",
		dmg = 0,
		qty = 256
},

mana_ordo = {
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "a77c4b35bbb1a4c84b15122dd52354bf",
		dmg = 0,
		qty = 256
},

mana_perditio = {
		color = colors.lightBlue,
		id = "Thaumcraft:ItemManaBean",
		nbt = "18add493aeeb97a3fe71cf8130fe8675",
		dmg = 0,
		qty = 256
}}

local function updateList()
		local newlist = me.getAvailableItems()
		for num,item in pairs(newlist) do
		   for n,i in pairs(items) do
				if test.nbt == "" then
						if item.fingerprint.id == i.id and item.fingerprint.dmg == i.dmg and item.size < i.qty then
								rs.setBundledOutput(sSide,colors.combine(rs.getBundledOutput(sSide),test.color))
							   break
						elseif item.fingerprint.id == i.id and item.fingerprint.dmg == i.dmg and item.size >= i.qty then
								rs.setBundledOutput(sSide,colors.subtract(rs.getBundledOutput(sSide), test.color))
​								break
						end
				else
						if item.fingerprint.id == i.id and item.fingerprint.dmg == i.dmg and item.fingerprint.nbt_hash == i.nbt and item.size < i.qty then
								rs.setBundledOutput(sSide,colors.combine(rs.getBundledOutput(sSide), test.color))
​								break
						elseif item.fingerprint.id == i.id and item.fingerprint.dmg == i.dmg and item.fingerprint.nbt_hash == i.nbt and item.size >= i.qty then
								rs.setBundledOutput(sSide,colors.subtract(rs.getBundledOutput(sSide), test.color))
​								break
						end
				end
		end
end

What this will do is loop through the newList table (which you did not give a example of), and compare the things in newexample to each of the items in the items tables.

Notice I changed anywhere it said test. something in the new function to i. something (i will be equal to each of the tables in the table at one point so it will loop through and check each one).

edit:

Sigh formatting appears to be messed up and remove formatting button is not removing…
I think I have removed all of the formatting codes, but I might have missed some. If you see any formatting code just ignore it example:
 [color] [font]
Edited on 14 February 2015 - 03:53 PM
DjTranceFire #3
Posted 15 February 2015 - 08:55 PM
It occurs to me that you did not post the entire script
Thats not correct, i posted everything i'm using at this point. ;)/>

but you should be able to do something like this.
Thats working fine! :)/>


loop through the newList table (which you did not give a example of),
Sorry, thought it would make no difference related to my problem because its only the default table that the ME gives (newlist = me.getAvailableItems()).

Notice I changed anywhere it said test. something in the new function to i. something (i will be equal to each of the tables in the table at one point so it will loop through and check each one).
Got it.. And i think i'm starting to understand how to use tables. Will try it on a few other things. Lets hope the best! :D/>
For everyone getting here with google or the boardsearch. The changed code from valithor is working fine, you just have to change a few "test." he missed.
Thank you very much, now i can take a look at my cable setup and finally start my mobfarm! :)/>