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

[SOLVED]how do I reference a table inside another?

Started by qwerty, 25 February 2020 - 07:01 AM
qwerty #1
Posted 25 February 2020 - 08:01 AM
It amuses me that I'm the only one starting threads here since the 20th, anyway- here's where I'm stuck yet again.
how do I reference a table within a different one?

this is what I got so far

sh = {} --[[sh stands for shorthand because I'm making a turtle crafting interface]]


sh["cbl"]="minecraft:cobblestone"


furnace = {
  sh:cbl, sh:cbl, sh:cbl, nil,
  sh:cbl, nil, sh:cbl, nil,
  sh:cbl, sh:cbl, sh:cbl, nil,
}
however you might notice that this doesn't work.
I've tried throwing everything but the kitchen sink of simple solutions but it looks like I'll have to use advanced techniques.

with regards
-qwerty
Edited on 25 February 2020 - 02:30 PM
Luca_S #2
Posted 25 February 2020 - 08:02 AM
You need to use "sh.cbl" instead of "sh:cbl"
qwerty #3
Posted 25 February 2020 - 08:20 AM
You need to use "sh.cbl" instead of "sh:cbl"

thanks man, works now.

do you mind allso telling me how would I leave spaces in the table so that the turtle ignores it but doesn't break the loop?
here's what I am stuck at


furnace = {
sh.cbl, sh.cbl, sh.cbl, nil,
sh.cbl, nil,	   sh.cbl, nil,
sh.cbl, sh.cbl, sh.cbl, nil,
}
function selec(table)
  for i=1,#table do
    turtle.select(i)
    --rest of code

as you can guess it doesn't work as expected and when encountering a nil it will break the for loop not finishing the selection process.

with gratitude
-qwerty
Luca_S #4
Posted 25 February 2020 - 08:49 AM
-snip-


furnace = {
sh.cbl, sh.cbl, sh.cbl, nil,
sh.cbl, nil,	   sh.cbl, nil,
sh.cbl, sh.cbl, sh.cbl, nil,
}
function selec(table)
  for i=1,#table do
	turtle.select(i)
	--rest of code

with gratitude
-qwerty

You should probably just replace nil with "minecraft:air" or some dummy item, and then check in the rest of code wether it's an actual item or "minecraft:air"
Edited on 25 February 2020 - 07:51 AM
qwerty #5
Posted 25 February 2020 - 09:12 AM
You should probably just replace nil with "minecraft:air" or some dummy item, and then check in the rest of code wether it's an actual item or "minecraft:air"

Ohhh ok then, I'll try that, thanks!

the only issue apparent here would be that the turtle selects all empty spaces and that's a no bueno in crafting.
scratch that, turtle.craft() is a stupid-to-use function.

with apreciation
-qwerty
Edited on 25 February 2020 - 08:23 AM
Luca_S #6
Posted 25 February 2020 - 10:05 AM
You should probably just replace nil with "minecraft:air" or some dummy item, and then check in the rest of code wether it's an actual item or "minecraft:air"

Ohhh ok then, I'll try that, thanks!

the only issue apparent here would be that the turtle selects all empty spaces and that's a no bueno in crafting.
scratch that, turtle.craft() is a stupid-to-use function.

with apreciation
-qwerty

If you don't want the turtle to select the empty slots you can use nil in the table and do the following to skip them:


function selec(tRecipe)
  for k, v in pairs(tRecipe) do
    turtle.select(k)
    -- The item name is now stored in v
  end
end
qwerty #7
Posted 25 February 2020 - 10:50 AM
If you don't want the turtle to select the empty slots you can use nil in the table and do the following to skip them:


function selec(tRecipe)
  for k, v in pairs(tRecipe) do
	turtle.select(k)
	-- The item name is now stored in v
  end
end
now while I've discovered that it doesn't need to skip the nil entries, this is still very useful. Thank you!

with kind regards
-qwerty