help with OOP class instantiation with setmetatable

I’m scratching my head on this one. I’ve followed a few tutorials on OOP, specifically using metatables and inheritance. Anyways, based on the tutorials I’ve been following, this seems like the correct format of a class (“entities/PowerUp.lua”):

[lua]PowerUp = {}
PowerUp_mt = { __index = PowerUp }

function PowerUp:new()
local powerup = {}
setmetatable( powerup, PowerUp_mt );
return powerup;
end

function PowerUp:className()
print( “PowerUp” );
end

function PowerUp:doSomething()
print( “Doing something” );
end[/lua]

However, when I try to instantiate an instance of this class with the following code in my “main.lua” file:

[lua]local PowerUp = require(“entities.PowerUp”)
local pu = PowerUp:new();[/lua]

I get the following error:

Runtime Error: attempt to index upvalue ‘PowerUp’ (a boolean value)

Just not sure what I’m doing wrong, and hope someone can point me in the right direction.
[import]uid: 49447 topic_id: 16309 reply_id: 316309[/import]

How about:

[lua]require(“entities.PowerUp”)

local pu = PowerUp:new()[/lua] [import]uid: 9048 topic_id: 16309 reply_id: 60976[/import]

thank you so much! The Corona tutorial/blog post was really throwing me off. I wonder if the code is outdated, or if it was wrong in the first place. From what I can tell, I was using the same code.

http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/

thanks again [import]uid: 49447 topic_id: 16309 reply_id: 60977[/import]

You were so close. :slight_smile:

Check the very last line in their dog.lua file. That’s what you’re missing. [import]uid: 9048 topic_id: 16309 reply_id: 60982[/import]

Looks like ‘me’ beat me to it :slight_smile:

But for further clarification, the require() function will return boolean unless you have your module return something, in the case of OOP in the manner you’re talking about, it would be a table.

Or, if your module uses the module() function (not recommended, it’s deprecated, and can lead to memory leaks in Corona), it won’t return a boolean. [import]uid: 52430 topic_id: 16309 reply_id: 61010[/import]

bah, I can’t believe I missed the last line. Thanks, ha. Thanks for the explanation, now I understand why it was a boolean value.

I’m not used to having a class return anything outside of the constructor or public methods.

Also, thanks for the note about module(), I didn’t realize it was deprecated, I’ll be sure to take that out of my code as well. [import]uid: 49447 topic_id: 16309 reply_id: 61011[/import]