John Lardinois
John Lardinois

Reputation: 109

Lua - can't access key value. Always returns table

So I've run into a few identical problems with Eclipse and Lua and Love2D.

In these cases, I've created a table, say fonts. It's in main.lua. In fonts{}, I create key pairs like this:

fontsize = 24
gFonts{
    ['smallFont'] = love.graphics.newFont('fontfile.ttf', fontsize)
}

Then I try to get the height of the font later using Love2D's font.getHeight(self). I do that like this:

local fontSize = gFonts['smallFont'].getHeight(self)

But it doesn't work. It tells me that getHeight is expecting a Font, but I'm giving it a Table. That's obviously not true, because Eclipse brings up the autocomplete for getHeight:Font when I press period after gFonts['smallFont'].

So then I tried assigning the smallFont to a variable called msgFont and accessing getHeight from that. Nope, didn't work. I even erased 'self'. No fix.

I also tried love.graphics.getHeight(fontcodehere), and it sort of works... but it grabs the height of the entire window, not the font.

I did manage to get it working, but I don't understand why this works and why the double font reference is necessary. I used:

local msgFont = gFonts['small']
local fontSize = msgFont.getHeight(gFonts['small'])

Why does this work? I understand the msgFont part or putting the font in the getHeight - separately... But why do they need to be used together?

Isn't,

table['key'] == 'value'

?

Why does it return a table and not a Love2D font object / registry? And why would I call

getHeight() and pass it itself as a table?

Why can't I call

getHeight() and pass it self?

I'm sorry if this is a dumb question - I've never had a formal programming education, and I get the feeling this has something to do with scope that I just don't understand properly. Maybe self isn't referring to the font, but to getHeight? How does that work?

Upvotes: 1

Views: 433

Answers (2)

Tom Blodget
Tom Blodget

Reputation: 20812

Lua does not provide an object-oriented system but it does provide an optional syntax that makes using one a bit easier.

A "method" is a function with the context of a table passed in. It is a matter of intent. In all other respects, it is an ordinary function.

It can be defined with the function expression : identifier ( … ) syntax. If so, there is an implicit first formal parameter with the name of self.

It can be called with the expression : identifier ( … ) syntax. If so, the value of expression is passed as the first actual argument.

getHeight is a function that needs a context. So,

gFonts['small']:getHeight()

which is the same as:

local msgFont = gFonts['small']
msgFont.getHeight(msgFont)

which is in effect the same as your:

local msgFont = gFonts['small']
local fontSize = msgFont.getHeight(gFonts['small'])

BTW—Since 'small' is a valid identifier, you could write:

gFonts.small:getHeight()

Upvotes: 2

lhf
lhf

Reputation: 72342

The correct call is gFonts['smallFont']:getHeight().

Upvotes: 1

Related Questions