middleclass & extras: middleclass 3.0 is out!

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by kikito »

This is wrong:

Code: Select all

TimedBox = class('TimedBox')
TimedBox:subclass('MessageBox')
The correct way of creating a subclass it is one of this two:

Code: Select all

TimedBox = class('TimedBox', MessageBox)

-- or --

TimedBox = MessageBox:subclass('TimedBox')
Your code doesn't throw any errors because it is doing non-erroneous things:
  • TimedBox = class('TimedBox') Creates a new class (a subclass of Object)
  • TimedBox:subclass('MessageBox') is creating a subclass of TimedBox, called MessageBox. It is also returning it, but no variable receives it.
The other thing I note is the empty constructor:

Code: Select all

function MessageBox:initialize()

end
Never do that.

All constructors should be either implicit (not appear at all, so they get a default implementation) or, if they are explicit, make a call to super.initialize(self)

Code: Select all

function MessageBox:initialize()
  super.initialize(self) -- always, always do this.
end
Finally, if you are doing that, probably you want to set the text directly on the initialize function, as a parameter. Complete sample:

Code: Select all

MessageBox = class('MessageBox')
MessageBox.DEFAULT_TEXT = "Default"
function MessageBox:initialize(text)
  super.initialize(self) -- always, always do this.
  self.text  = text or self.class.DEFAULT_TEXT -- I prefer this, but you can (for now) do self.DEFAULT_TEXT instead of self.class.DEFAULT_TEXT
end

TimedBox = class('TimedBox', MessageBox) -- subclass
function TimedBox:initialize(text)
  super.initialize(self, text)
end

StayBox = class('StayBox', MessageBox) -- subclass
StayBox.DEFAULT_TEXT = "StayBox" -- I changed the default text for StayBox here
function StayBox:initialize(text)
  super.initialize(self, text)
end

ChoiceBox = class('ChoiceBox', MessageBox) -- subclass
function ChoiceBox:initialize(text)
  super.initialize(self, text)
end
And then for testing:

Code: Select all

function love.load()
  msg = MessageBox:new()
  print(msg.text)
  timed = TimedBox:new("Parameter")
  print(timed.text)
  stay = StayBox:new()
  print(stay.text)
end
Output (disclaimer: I didn't actually test it):

Code: Select all

Default
Parameter
StayBox
I hope this helps!
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by TechnoCat »

kikito wrote:This is wrong:

Code: Select all

TimedBox = class('TimedBox')
TimedBox:subclass('MessageBox')
The correct way of creating a subclass it is one of this two:

Code: Select all

TimedBox = class('TimedBox', MessageBox)

-- or --

TimedBox = MessageBox:subclass('TimedBox')
I see, so I was doing inheritance wrong. :oops:
Thanks for the huge help Kikito.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by TechnoCat »

I'm trying to pass in a function pointer. And setting it to default if nil is passed in.

Code: Select all

Object = class('Object')
function Object:initialize(fnct)
  super.initialize(self)
  defaultFnct = function() return "default" end
  self:fnct() = fnct or defaultFnct
end
Output:

Code: Select all

Syntax error: unexpected symbol near '='
I realize this might end up just being a plain Lua question, but I wasn't sure.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by bartbes »

Well, since : is syntactic sugar, you'll need to do self.fnct = fnct or defaultFnct
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by kikito »

TechnoCat wrote:I'm trying to pass in a function pointer. And setting it to default if nil is passed in.
...
I realize this might end up just being a plain Lua question, but I wasn't sure.
Yep, the error is on this line:

Code: Select all

self:fnct() = fnct or defaultFnct
self:fnct() is an invocation. It gets transformed into the value it returns before interpreting the expression that contains it. What you wrote, would be equivalent to doing this:

Code: Select all

"default" = fnct or defaultFnct -- assuming that the function returned "default" when invoked
If you want to reference the function, you have to do self.fnct = ... instead.
When I write def I mean function.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by kikito »

Small update - I detected and corrected a bug on Stateful. It basically made inncludes(module, aClass) fail on certain occasions (subclasses of a Stateful class).

It is an edge case that I didn't think about before; I believe it also happened when Stateful was called MindState.

In order to correct this bug, you will need to update middleclass as well as middleclass-extras.

On other news, I've started integrating middleclass-extras with PÄSSION. Was able to have the avalanche and physics platformer running. I'm now with piano-piano (which is the most complex one, since it has a complex GUI)

Regards!
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by TechnoCat »

What are the rules for including MiddleClass in libraries and games?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by kikito »

You mean, the license?
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by TechnoCat »

Well, I see it is BSD, but what does that mean really?

EDIT: and i've been working on this message box system library with middleclass, you should check it out. http://bitbucket.org/dannyfritz/message-in-a-bottle
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by kikito »

TechnoCat wrote:Well, I see it is BSD, but what does that mean really?
The way I see it, it means 'do whatever you want, but give me credit, and don't use the name to give publicity without my permission'. 'Giving credit' means 'copying the license file somewhere'. You can either have a separate file called middleclass-license.txt or you can just have a single license.txt file with all the licenses you use, one after another. The publicity part is there simply so you can't use the name: "from the creators of MiddleClass, here's a wonderful Snake game!" without permission. That's the way I understand it, at least. I'm not a lawyer.
TechnoCat wrote:EDIT: and i've been working on this message box system library with middleclass, you should check it out. http://bitbucket.org/dannyfritz/message-in-a-bottle
Interesting. Can I has screenshots? It is kind of difficult to imagine how it looks.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot], Majestic-12 [Bot] and 7 guests