Issues with Metatables and Logging

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
khamarr3524
Prole
Posts: 41
Joined: Thu Sep 05, 2013 8:48 pm

Issues with Metatables and Logging

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Issues with Metatables and Logging

Post 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 ^^ !
When I write def I mean function.
khamarr3524
Prole
Posts: 41
Joined: Thu Sep 05, 2013 8:48 pm

Re: Issues with Metatables and Logging

Post 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.
Attachments
Labyrinth of Death.love
(20.21 KiB) Downloaded 110 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Issues with Metatables and Logging

Post 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.
When I write def I mean function.
khamarr3524
Prole
Posts: 41
Joined: Thu Sep 05, 2013 8:48 pm

Re: Issues with Metatables and Logging

Post by khamarr3524 »

Ah, i forgot that .syntax doesn't carry the class over. Thanks so much, I knew it was simple.
Engineer
Prole
Posts: 13
Joined: Mon Aug 05, 2013 9:30 pm

Re: Issues with Metatables and Logging

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 221 guests