Page 11 of 25

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Sun Nov 07, 2010 9:35 pm
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!

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Sun Nov 07, 2010 10:26 pm
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.

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Tue Nov 09, 2010 4:40 am
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.

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Tue Nov 09, 2010 7:03 am
by bartbes
Well, since : is syntactic sugar, you'll need to do self.fnct = fnct or defaultFnct

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Tue Nov 09, 2010 12:47 pm
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.

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Thu Nov 11, 2010 12:01 am
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!

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Thu Nov 11, 2010 12:41 am
by TechnoCat
What are the rules for including MiddleClass in libraries and games?

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Thu Nov 11, 2010 1:04 am
by kikito
You mean, the license?

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Thu Nov 11, 2010 3:10 am
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

Re: middleclass & middleclass-extras: OOP for LUA

Posted: Thu Nov 11, 2010 8:43 am
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.