Page 1 of 1

Issues with Metatables and Logging

Posted: Thu Sep 05, 2013 10:13 pm
by khamarr3524
Hello, I'm currently working on a project where I have a file Unit.lua containing a global table tUnit with tUnit.__index = tUnit set. tUnit is used to create table instances of units through passing a table through tUnit:new{args}. For some reason, using

Code: Select all

function tUnit:new(t)
    setmetatable(t, self)
it says t is a nil value.

I've extensively read about metatables and this is baffling in how irritating this is. tUnit has various functions defined and throughout tUnit:new(t) I use the self operator to initialize values. No matter what I do, t is continuously nil, and the meta table breaks. Any ideas? What am I doing wrong here?

Secondly, I am trying to set up a logging system and need to know how I can reopen a log that is already created and write to it. Do I just use love.filesystem.newFile with the same name? It works for the first line, but I don't know anything else because the tUnit class is breaking.

Re: Issues with Metatables and Logging

Posted: Thu Sep 05, 2013 10:48 pm
by kikito
Hi there, welcome to LÖVE!

Your problem probably lies on a different line than the 2 you showed us.

Please give us the complete stack trace and all the source code. Ideally, give us a *.love file so we can execute it quickly and trace it ourselves. Otherwise it's very difficult to help you.

Good luck, in any case ^^ !

Re: Issues with Metatables and Logging

Posted: Fri Sep 06, 2013 2:29 am
by khamarr3524
Here's the .love. I believe a lot of the code is probably messy and needs to be redone entirely, it's probably the cause of my problem. I've been tweaking it for a few days now.

Edit: Most of my questions were really on how to best implement these structural ideas. I would like to find the best implementations and what should be done. Sometimes just patching code can frustrate me and make it seem like the concepts behind it are muddy. More often than not, because of how modular I make my code I can simply scrap a "module" or section of the code to improve the interfacing capacity, make it more readable, modifiable, and be more conceptually sound.

Re: Issues with Metatables and Logging

Posted: Fri Sep 06, 2013 9:13 pm
by kikito
tUnit.new expected two parameters (tUnit and t) but you only gave it one (t).

Your issue is that in main.lua you wrote:

Code: Select all

 tUnit.new{ ... }
Instead, you should write this:

Code: Select all

 tUnit:new({ ... })
That is the same as writing this:

Code: Select all

 tUnit.new(tUnit, { ... })
Which is what you wanted.

Re: Issues with Metatables and Logging

Posted: Fri Sep 06, 2013 10:39 pm
by khamarr3524
Ah, i forgot that .syntax doesn't carry the class over. Thanks so much, I knew it was simple.

Re: Issues with Metatables and Logging

Posted: Sun Sep 08, 2013 4:03 pm
by Engineer
kikito wrote:tUnit.new expected two parameters (tUnit and t) but you only gave it one (t). ]
Instead, you should write this:

Code: Select all

 tUnit:new({ ... })
That is the same as writing this:

Code: Select all

 tUnit.new(tUnit, { ... })

Code: Select all

 tUnit:new { } 
This is also valid.