Object variable is nil except for when tested with error()

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
drikdrok
Prole
Posts: 36
Joined: Sun Mar 15, 2015 9:53 am
Contact:

Object variable is nil except for when tested with error()

Post by drikdrok »

For context I'm trying to translate this code into Lua/Love2Dhttps://github.com/OneLoneCoder/videos/ ... _Part2.cpp

I'm reading a .obj file and converting it into triangles. Here's the code that is relevant.

Note I'm using Middleclass

Code: Select all

local vec3d = class("vec3d")

function vec3d:initialize(x, y, z)
	self.x, self.y, self.z = x, y, z
end

local triangle = class("triangle")

function triangle:initialize(x1, y1, z1, x2, y2, z2, x3, y3, z3)
	self.p = {vec3d:new(x1, y1, z1), vec3d:new(x2, y2, z2), vec3d(x3, y3, z3)} --Creates a triangle with 3 points
end

local mesh = class("mesh")

function mesh:initialize()
	self.tris = {}

	self.LoadFromObjectFile = function(filename)

		local f = love.filesystem.newFile(filename)
		if f == nil then return false end


		--Local cache of verts
		local verts = {}

		--This differs quite a lot from the original code becaue it is very c++ specific
		for line in f:lines() do
		
			local words = {}
			for word in line:gmatch("%S+") do table.insert(words, word) end

			if words[1] == "v" then 
				local v = vec3d:new(words[2], words[3], words[4])

				table.insert(verts, v)
			end

			if words[1] == "f" then 
				local f1 = {1, 1, 1} -- the c++ line is "int f[3]; -- How does this make sense as the ifstream is also called f?"
				f1[1], f1[2], f1[3] = words[2], words[3], words[4]


				local x1 = verts[f1[1] - 1].x -- This is where the problem is

				table.insert(self.tris, triangle:new(verts[f1[1] - 1].x, verts[f1[2] - 1], verts[f1[3] - 1])) -- This line is not finished ignore it
			end
		end	

		return true
	end
end
The problem comes at the very end,

Code: Select all

local x1 = verts[f1[1] - 1].x -- This is where the problem is
When running, verts[f1[1] - 1].x is nil. Except that it isn't? because if you say error( verts[f1[1] - 1].x) it will indeed error out the correct number. How is this possible? How can a variable be nil except when putting it through error(), and how can I fix it?
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Object variable is nil except for when tested with error()

Post by pgimeno »

error() is not a very efficient way of debugging. You're forcing an error on the first loop iteration, and that's not necessarily the loop iteration in which the actual error you're getting is happening.

If you're on Linux or Mac, open love from a terminal. If you're on Windows, try using lovec.exe instead of love.exe. That should allow you to see the console output, and to use print() instead of error() to display stuff.

Failing that, use a debugging library such as lovebird which allows you to see the output of print() statements in a web browser.(*) Or some other debugging library that allows you to redirect the output of your program somewhere you can see. Or you can use an IDE such as ZeroBrane Studio which comes with an integrated debugger.

(*) Edit: Actually, lovebird seems to fail to send the updates in case of an error, therefore you can't see any print statements that are immediately followed by an error.

Failing that, you can write your own logging library, to print stuff to a file that you can open in an editor and view the complete output (and not just the first output).

As for this:
drikdrok wrote: Mon Jul 30, 2018 3:53 pm

Code: Select all

				local f1 = {1, 1, 1} -- the c++ line is "int f[3]; -- How does this make sense as the ifstream is also called f?"
Just like in Lua, in C and C++ the variables are local to the scope they are declared in.

Code: Select all

// C example
#include <stdio.h>
int main()
{
    int a = 2;
    {
        int a = 3;
        printf("%d\n", a); // prints 3
    }
    printf("%d\n", a); // prints 2
    return 0;
}
In Lua:

Code: Select all

local a = 2
do
    local a = 3
    print(a) -- prints 3
end
print(a) -- prints 2
User avatar
drikdrok
Prole
Posts: 36
Joined: Sun Mar 15, 2015 9:53 am
Contact:

Re: Object variable is nil except for when tested with error()

Post by drikdrok »

Thanks for the quick reply! I'll report back when I've tested some of your suggestions.

pgimeno wrote: Mon Jul 30, 2018 4:21 pm
As for this:
drikdrok wrote: Mon Jul 30, 2018 3:53 pm

Code: Select all

				local f1 = {1, 1, 1} -- the c++ line is "int f[3]; -- How does this make sense as the ifstream is also called f?"
Just like in Lua, in C and C++ the variables are local to the scope they are declared in.

Code: Select all

// C example
#include <stdio.h>
int main()
{
    int a = 2;
    {
        int a = 3;
        printf("%d\n", a); // prints 3
    }
    printf("%d\n", a); // prints 2
    return 0;
}
In Lua:

Code: Select all

local a = 2
do
    local a = 3
    print(a) -- prints 3
end
print(a) -- prints 2
This makes total sense, I don't even understand how I was confused now that you explained it :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 10 guests