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?
The correct call is
gFonts['smallFont']:getHeight().