-2

For some reason, when I access only my main table, it's fine, but when I try to access it using a variable, it's somehow nil? Here's the function that's giving the error:

function parseMtl(mtlFilePath, mtlTbl) -- Parses the mtl file at mtlFIlePath
    local step1 = {} -- Table to store the split lines

    local mtlID = 0 -- The ID of the material
    local mtlList = {} -- The list of materials
    local faceColors = {} -- The returned colors of the polygons
    local fC -- A test variable
    contents, size = love.filesystem.read(mtlFilePath, all) -- Read the .mtl file at mtlFilePath
    step1 = split(contents, "\n") -- Split the contents into step1 by the newline character
    for i=1,#step1 do
        if (starts(step1[i], "newmtl")) -- Create a new material
        then
            mtlID = mtlID + 1
            mtlName = split(step1[i], "%s")[2]
            table.insert(mtlList, {mtlName, mtlID})
        end
        if (starts(step1[i], "Kd")) -- If it's a color value, put value into the list of materials
        then
            
            R = split(step1[i], "%s")[2] * 255
            G = split(step1[i], "%s")[3] * 255
            B = split(step1[i], "%s")[4] * 255
            table.insert(mtlList[mtlID], {R, G, B})
        end
        for i=1,#mtlTbl do -- Convert the mtlTbl values into 'Vertex ID, Color' format
            fC = mtlList[mtlTbl[i][2]]
            table.insert(faceColors, {i, fC})
        end
    end
    return faceColors
end

The part that's doing weird stuff is the table.insert(faceColors, {i, fC}), it's somehow returning nil when I put fC in a table, when I print the fC value directly it's not nil. I have no idea why it's doing this..

Nifim
  • 4,758
  • 2
  • 12
  • 31
NickKnack
  • 119
  • 1
  • 8
  • Are you not using `love2d`? your code appears to be using its filesystem library, `love.filesystem.read(mtlFilePath, all)` – Nifim Jun 29 '21 at 18:14
  • @Nifim Yes, I'm using `love2d`, I wasn't sure whether I should specify that or not, as the base Lua functions and the stuff I'm using here have nothing to do with `love2d`. – NickKnack Jun 29 '21 at 18:17
  • it is good to tag that, while it might appear to be an issue that is environment agnostic, it might not be. The information provides more context to your issue. – Nifim Jun 29 '21 at 18:20
  • Makes sense, I'm guessing you just tagged it? As I didn't put that there. – NickKnack Jun 29 '21 at 18:22
  • 1
    Read about, and then create a [mcve]. There is not enough information here to solve your problem. How is `starts` defined? How is `split` defined? How are `mtlTbl`s defined? What does an example input file look like? I suspect that one of the above functions does not work as expected, or the input does not quite match code expectations. If you create a _simple_ [mcve], using some dummy input, that has the same problem (without using Love2d), you will probably figure out what you are doing wrong all by yourself. – ad absurdum Jul 04 '21 at 19:45
  • This is the minimal example... I had to rewrite about 94% of it to get it to this point, and I'm not going to spend another week rewriting it again.. – NickKnack Jul 05 '21 at 12:20

1 Answers1

0

Were you trying to use the return value of table.insert?

table.insert is void, so it always returns nil.

How are you accessing your color data?

The way you set up your data, you would access it with:

local faceColors = parseMtl(...)
for i = 1, #faceColors do
    print(faceColors[i][2])
end
warmCabin
  • 139
  • 1
  • 9
  • I'm not using the return value of `table.insert`, and that is not how I was accessing the values (directly, at least). I'm putting those values into another table in my main script, to be used alongside my rendering function. It does end up to be the equivalent of that, though. – NickKnack Jul 05 '21 at 11:55
  • Considering the nature of your problem here, you should share more details about that in your question. Still, it would be something like: `local faceColors = outerTable[whateverKey]; for i = 1, #faceColors do ... end` – warmCabin Jul 06 '21 at 15:34