I need help with a table within a table

Started by MarsOdin, Mar 21, 2019, 02:53 am

Previous topic - Next topic

MarsOdin

Hi, this is my first post on this forum. I am a noob to coding but the Computercraft community helped me out a lot through the old forum (I never registered, I always found in the forums the answers to my problems).

Now I have another problem I can't solve.

I have the following table with other tables in it:

{
  inv = {
    {
      q = 200,
      name1 = "Food supply",
      wgt = 10,
      id = 7,
      name2 = "Food supplies",
      size = 10,
    },
    {
      q = 2,
      name1 = "STL-Fuel cell",
      wgt = 100,
      id = 8,
      name2 = "STL-Fuel cells",
      size = 200,
    },
  },
  stlFuelTank = {
    max = 300,
    current = 100,  --  <--- I want to make a reference to this variable here.
    name = "STL-Fuel tank",
  },
  ftlFuelTank = {
    max = 50,
    current = 8,
    name = "FTL-Fuel tank",
  },
  name = "the ship",
  id = 12,
}

How can I make a reference to table.stlFuelTank.current ? I tried term.write(table.stlFuelTank[current]) and term.write(table.stlFuelTank.current) but none of these work. I get the attempt to index ? (a nil value) error.

SquidDev

table.stlFuelTank.current should be correct. Would you be willing to post your whole program - just so we can test ourselves.
GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod

MarsOdin

Mar 21, 2019, 03:12 pm #2 Last Edit: Mar 21, 2019, 03:14 pm by SquidDev
Quote from: SquidDev on Mar 21, 2019, 08:26 amtable.stlFuelTank.current should be correct. Would you be willing to post your whole program - just so we can test ourselves.

Thank you SquidDev!

Here is the whole program.
table.stlFuelTank.current worked after all. As you can see I was importing the table from a file and I was referring to a wrong inventory ID.
Instead of term.write(inventory[tempData.shipId].stlFuelTank.current) I was using term.write(inventory[shipId].stlFuelTank.current), where shipId is not a defined variable.

Btw, I had the same issue with the ftlFuelTank.


Code Select
term.clear()

-----------------------------------------------------------------------------
-- LOAD DATA
-----------------------------------------------------------------------------
local tempData, sectorObjects, sectorObject, inventory = {}, {}, {}, {}

function loadAll()

    local file = fs.open("SpaceAdventure/data/tempData.lua","r")
    local data = file.readAll()
    file.close()
    tempData = textutils.unserialize(data)

    local file = fs.open("SpaceAdventure/world/sectorObjects.lua","r")
    local data = file.readAll()
    file.close()
    sectorObjects = textutils.unserialize(data)

end
loadAll()

function loadAll2()

    for i = 1, #sectorObjects do
        local file = fs.open("SpaceAdventure/world/"..tostring(sectorObjects[i])..".lua","r")
        local data = file.readAll()
        file.close()
        sectorObject[i] = textutils.unserialize(data)
    end

    for i = 1, #tempData.inventoryId do
        local file = fs.open("SpaceAdventure/inventories/"..tostring(tempData.inventoryId[i])..".lua","r")
        local data = file.readAll()
        file.close()
        inventory[tempData.inventoryId[i]] = textutils.unserialize(data)
    end

end
loadAll2()

-----------------------------------------------------------------------------
-- SAVE DATA FUNCTIONS
-----------------------------------------------------------------------------
function saveTempData(tempDataTable)
    local file = fs.open("SpaceAdventure/data/tempData.lua","w")
    file.write(textutils.serialize(tempDataTable))
    file.close()
end

function saveInventory(inventoryTable,inventoryId)
    local file = fs.open("SpaceAdventure/inventories/"..inventoryId..".lua","w")
    file.write(textutils.serialize(inventoryTable))
    file.close()
end

-----------------------------------------------------------------------------
-- SCREEN TITLE
-----------------------------------------------------------------------------
function ScreenTitle(PosX,title,additional)
    term.setCursorPos(1,1)
    term.setBackgroundColor(2048)
    for i = 1,52 do
        term.write(" ")
    end
    term.setCursorPos(PosX,1)
    term.setTextColor(16)
    term.write(title.." "..additional)
end
ScreenTitle(20,"SET DESTINATION"," ")

-----------------------------------------------------------------------------
-- CONSUMABLE RESOURCES
-----------------------------------------------------------------------------

function consumableResourcesInfo()
    term.setBackgroundColor(32768)
    term.setTextColor(512)
    term.setCursorPos(2,17)
    term.write("FTL-Fuel:")
    term.setTextColor(1)
    term.write(inventory[tempData.shipId].ftlFuelTank.current)
    term.setTextColor(512)
    term.setCursorPos(15,17)
    term.write("STL-Fuel:")
    term.setTextColor(1)
    term.write(inventory[tempData.shipId].stlFuelTank.current)
    term.setTextColor(512)
    term.setCursorPos(28,17)
    term.write("Food:")
    term.setTextColor(1)
    for i = 1,#inventory[tempData.shipId].inv do
        if inventory[tempData.shipId].inv[i].name1 == "Food supply" then
            term.write(inventory[tempData.shipId].inv[i].q)
        end

    end
end
consumableResourcesInfo()

-----------------------------------------------------------------------------
-- MAP
-----------------------------------------------------------------------------
local coord = 1
local coordPosition = {
    [1] = {text="01", PosX=2 ,PosY=3},
    [2] = {text="02", PosX=5 ,PosY=3},
    [3] = {text="03", PosX=8 ,PosY=3},
    [4] = {text="04", PosX=11 ,PosY=3},
    [5] = {text="05", PosX=14 ,PosY=3},
    [6] = {text="06", PosX=17 ,PosY=3},
    [7] = {text="07", PosX=20 ,PosY=3},
    [8] = {text="08", PosX=2 ,PosY=5},
    [9] = {text="09", PosX=5 ,PosY=5},
    [10] = {text="10", PosX=8 ,PosY=5},
    [11] = {text="11", PosX=11 ,PosY=5},
    [12] = {text="12", PosX=14 ,PosY=5},
    [13] = {text="13", PosX=17 ,PosY=5},
    [14] = {text="14", PosX=20 ,PosY=5},
    [15] = {text="15", PosX=2 ,PosY=7},
    [16] = {text="16", PosX=5 ,PosY=7},
    [17] = {text="17", PosX=8 ,PosY=7},
    [18] = {text="18", PosX=11 ,PosY=7},
    [19] = {text="19", PosX=14 ,PosY=7},
    [20] = {text="20", PosX=17 ,PosY=7},
    [21] = {text="21", PosX=20 ,PosY=7},
    [22] = {text="22", PosX=2 ,PosY=9},
    [23] = {text="23", PosX=5 ,PosY=9},
    [24] = {text="24", PosX=8 ,PosY=9},
    [25] = {text="25", PosX=11 ,PosY=9},
    [26] = {text="26", PosX=14 ,PosY=9},
    [27] = {text="27", PosX=17 ,PosY=9},
    [28] = {text="28", PosX=20 ,PosY=9},
    [29] = {text="29", PosX=2 ,PosY=11},
    [30] = {text="30", PosX=5 ,PosY=11},
    [31] = {text="31", PosX=8 ,PosY=11},
    [32] = {text="32", PosX=11 ,PosY=11},
    [33] = {text="33", PosX=14 ,PosY=11},
    [34] = {text="34", PosX=17 ,PosY=11},
    [35] = {text="35", PosX=20 ,PosY=11},
    [36] = {text="36", PosX=2 ,PosY=13},
    [37] = {text="37", PosX=5 ,PosY=13},
    [38] = {text="38", PosX=8 ,PosY=13},
    [39] = {text="39", PosX=11 ,PosY=13},
    [40] = {text="40", PosX=14 ,PosY=13},
    [41] = {text="41", PosX=17 ,PosY=13},
    [42] = {text="42", PosX=20 ,PosY=13},
    [43] = {text="43", PosX=2 ,PosY=15},
    [44] = {text="44", PosX=5 ,PosY=15},
    [45] = {text="45", PosX=8 ,PosY=15},
    [46] = {text="46", PosX=11 ,PosY=15},
    [47] = {text="47", PosX=14 ,PosY=15},
    [48] = {text="48", PosX=17 ,PosY=15},
    [49] = {text="49", PosX=20 ,PosY=15}
}
term.setTextColor(128)
for i = 1,49 do
    term.setBackgroundColor(32768)
    term.setCursorPos(coordPosition[i].PosX,coordPosition[i].PosY)
    print(coordPosition[i].text)
end

-- SYSTEM OBJECTS -----------------------------------------------------------
term.setTextColor(32768)
for i = 1, #sectorObject do
    term.setCursorPos(sectorObject[i].PosX,sectorObject[i].PosY)
    term.setBackgroundColor(sectorObject[i].color)
    print(sectorObject[i].sector)
end

-- MAP LEGEND ---------------------------------------------------------------
local legendSystemObject = {
    [1] = {name="Current Pos: ", color=32, PosX=24, PosY=4},
    [2] = {name="Sun", color=16, PosX=24, PosY=6},
    [3] = {name="Rock Planet", color=4096, PosX=24, PosY=8},
    [4] = {name="Ocean Planet", color=2048, PosX=24, PosY=10},
    [5] = {name="Ice Planet", color=8, PosX=24, PosY=12},
    [6] = {name="Asteroid", color=1024, PosX=24, PosY=14}
}
function mapLegend(cursorPosX,cursorPosY,color,text)
    term.setCursorPos(cursorPosX,cursorPosY)
    term.setBackgroundColor(color)
    term.write(" ")
    term.setTextColor(color)
    term.setBackgroundColor(32768)
    print(" "..text)
end
for i = 1,#legendSystemObject do
    mapLegend(legendSystemObject[i].PosX,legendSystemObject[i].PosY,legendSystemObject[i].color,legendSystemObject[i].name)
end

-----------------------------------------------------------------------------
-- POSITION
-----------------------------------------------------------------------------
function CurrentPos()
    term.setCursorPos(coordPosition[tempData.sector].PosX,coordPosition[tempData.sector].PosY)
    term.setTextColor(32768)
    term.setBackgroundColor(32)
    print(coordPosition[tempData.sector].text)
end
CurrentPos()

function LocationInSystem()
    term.setBackgroundColor(32768)
    term.setCursorPos(38,4)
    term.setTextColor(128)
    term.write(" Space")
    term.setCursorPos(38,4)
end
LocationInSystem()

for i = 1, #sectorObject do
    if tempData.sector == sectorObject[i].sector or "0"..tempData.sector == sectorObject[i].sector then
        term.setTextColor(sectorObject[i].color)
        term.write(" "..sectorObject[i].type)
        OverObject = true
    end
end

local textSector = {
    [1] = {sector=tempData.sector+1},
    [2] = {sector=tempData.sector+7},
    [3] = {sector=tempData.sector-1},
    [4] = {sector=tempData.sector-7}
}
for i = 1,4 do
    if textSector[i].sector >= 50 or textSector[i].sector <= 0 or textSector[i].sector == 25 then textSector[i].sector = "-" end
end
for i = 7,49,7 do
    if tempData.sector == i then textSector[1].sector = "-" end
end
for i = 1,43,7 do
    if tempData.sector == i then textSector[3].sector = "-" end
end

-----------------------------------------------------------------------------
-- MENU
-----------------------------------------------------------------------------
local sId = 1
local runMenu = true

function printMenu(menu,textColor,textBackgroundColor,selectionColor)
    term.setTextColor(textColor)
    for i=1,#menu do
        term.setCursorPos(menu[i].PosX,menu[i].PosY)
        if i == sId then
            term.setBackgroundColor(selectionColor)
            print(menu[i].text)
        else
            term.setBackgroundColor(textBackgroundColor)
            print(menu[i].text)
        end
    end
    term.setBackgroundColor(textBackgroundColor)
end
function onKeyPressedVertical(key,menu)
    if key == keys.w then
        if sId > 1 then
            sId = sId-1
        end
    elseif key == keys.s then
        if sId < #menu then
            sId = sId+1
        end
    elseif key == keys.space then
        menu[sId].handler()
        sId = 1
    end
end

function handlerBack()
    runMenu = false
    shell.run("/SpaceAdventure/screens/Map.lua")
end

local noFood = true
function handlerSectorSelection()
    term.setCursorPos(1,19)
    term.setBackgroundColor(32768)
    if textSector[sId].sector == "-" then
        term.setTextColor(16384)
        term.write(" THIS OPTION IS UNAVAILABLE                      ")
    else
        for i = 1,#inventory[tempData.shipId].inv do
            if inventory[tempData.shipId].inv[i].name1 == "Food supply" then
                noFood = false
                if inventory[tempData.shipId].inv[i].q < 4*#tempData.crewMember then
                    term.setTextColor(16384)
                    term.write(" NOT ENOUGH FOOD FOR THIS TRIP                    ")
                elseif inventory[tempData.shipId].stlFuelTank.current < 10 then
                    term.setTextColor(16384)
                    term.write(" NOT ENOUGH STL-FUEL FOR THIS TRIP                    ")
                else
                    inventory[tempData.shipId].inv[i].q = inventory[tempData.shipId].inv[i].q - 4*#tempData.crewMember
                    inventory[tempData.shipId].stlFuelTank.current = inventory[tempData.shipId].stlFuelTank.current - 10
                    saveInventory(inventory[tempData.shipId],tempData.shipId)
                    if sId == 1 then tempData.sector = tempData.sector+1 end
                    if sId == 2 then tempData.sector = tempData.sector+7 end
                    if sId == 3 then tempData.sector = tempData.sector-1 end
                    if sId == 4 then tempData.sector = tempData.sector-7 end
                    tempData.sectorType = "Space"
                    for i = 1,#sectorObject do
                        if tempData.sector == sectorObject[i].sector or "0"..tempData.sector == sectorObject[i].sector then
                            tempData.sectorType = sectorObject[i].type
                        end
                    end
                    saveTempData(tempData)
                    runMenu = false
                    if math.random(1,2) ~= 1 then
                        shell.run("/SpaceAdventure/screens/Map.lua")
                    else
                        shell.run("/SpaceAdventure/screens/Event.lua")
                    end
                end
            end
        end
    end
end

while runMenu == true do
    newMenu = {
        [1] = {text=" Sector "..textSector[1].sector.." ", handler=handlerSectorSelection, PosX=41, PosY=7},
        [2] = {text=" Sector "..textSector[2].sector.." ", handler=handlerSectorSelection, PosX=41, PosY=9},
        [3] = {text=" Sector "..textSector[3].sector.." ", handler=handlerSectorSelection, PosX=41, PosY=11},
        [4] = {text=" Sector "..textSector[4].sector.." ", handler=handlerSectorSelection, PosX=41, PosY=13},
        [5] = {text=" BACK ", handler=handlerBack, PosX=41, PosY=17}
    }
    printMenu(newMenu,32,32768,2048)
    event, key = os.pullEvent("key")
    onKeyPressedVertical(key,newMenu)
end
If anyone wonders what this looks like:

You cannot see attachments on this board.