middleclass & extras: middleclass 3.0 is out!

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by TechnoCat »

I'm getting some sort of __call metamethod error using a tween callback and middleclass.

Code: Select all

Error: ./lib/middleclass/middleclass.lua:70: class Passenger doesn't implement metamethod '__call'

stack traceback:
	[C]: in function 'assert'
	./lib/middleclass/middleclass.lua:70: in function 'callback'
	./lib/tween/tween.lua:109: in function 'finishTween'
	./lib/tween/tween.lua:369: in function 'update'
	[string "main.lua"]:17: in function 'update'
	[string "boot.lua"]:320: in function <[string "boot.lua"]:308>
	[C]: in function 'xpcall'
[Finished]
The program is a visual IO scheduler. Press space-bar to start and numbers or 'r' to request blocks to be read. It crashes when it attempts to call table.remove from tween.
Attachments
lovelator.love
(670.75 KiB) Downloaded 84 times
Last edited by TechnoCat on Wed May 11, 2011 12:23 pm, edited 1 time in total.
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 »

Hi Technocat,

that is weird. I'll try to give it a look this evening.
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 »

Hi technocat,

I've found the problem. It was this tween in Elevator.lua:

Code: Select all

		tween(
				Passenger.TWEEN_TIME,
				passenger,
				{x=self.x, y=passenger.y, alpha=0},
				'inOutCubic',
				table.remove(self.passengers, #self.passengers))
The issue was on the last line; the callback must be a function (in your case, it was the result of executing table.remove, which as a person instance. Tween was trying to apply the __call metamethod to that person instance.

You can either wrap that last line within an anonymous function, like this:

Code: Select all

		tween(
				Passenger.TWEEN_TIME,
				passenger,
				{x=self.x, y=passenger.y, alpha=0},
				'inOutCubic',
				function() table.remove(self.passengers, #self.passengers) end)
Or put the function followed by its parameters:

Code: Select all

		tween(
				Passenger.TWEEN_TIME,
				passenger,
				{x=self.x, y=passenger.y, alpha=0},
				'inOutCubic',
				table.remove, self.passengers, #self.passengers)
Beware, though: on the first case #self.passengers will be evaluated when the tween finishes, and on the second it will be evaluated when the tween starts.ç

I've used the first option and it seems to work just fine. Attaching the result.

EDIT: oh and by the way, sorry for the time it took me to answer to this.
Attachments
lovelator.love
fixed!
(670.75 KiB) Downloaded 94 times
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by TechnoCat »

Yes, this pleases me very much. Thank you Kikito.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: middleclass & middleclass-extras: OOP for LUA

Post by Kadoba »

It's a small issue but in the comments in Apply.lua I think there's a typo on line 15. It says to call the method "includes" instead of "include". I'm just learning middleclass so this threw me for a little bit.

Code: Select all

MyClass:includes(Apply)
Edit: and another on line 49:

Code: Select all

- subclasses should also have the _istances list
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 »

Hi Kadoba,

I'm sorry the comment threw you off.

I've changed the comment so it doesn't happen to anyone else.

Thanks for reporting this!
When I write def I mean function.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: middleclass & middleclass-extras: OOP for LUA

Post by LuaWeaver »

I'm trying to work with middleclass, and it's just not working. Does it have to be in it's folder? I tried in and out of the folder and it did not work. I used your code here in and out of love.load().

Code: Select all

require 'middleclass.lua'
Person = class('Person') --this is the same as class('Person', Object) or Object:subclass('Person')
function Person:initialize(name)
  self.name = name
end
function Person:speak()
  print('Hi, I am ' .. self.name ..'.')
end

AgedPerson = class('AgedPerson', Person) -- or Person:subclass('AgedPerson')
AgedPerson.ADULT_AGE = 18 --this is a class variable
function AgedPerson:initialize(name, age)
  Person.initialize(self, name) -- this calls the parent's constructor (Person.initialize) on self
  self.age = age
end
function AgedPerson:speak()
  Person.speak(self) -- prints "Hi, I am xx."
  if(self.age < AgedPerson.ADULT_AGE) then --accessing a class variable from an instance method
    print('I am underaged.')
  else
    print('I am an adult.')
  end
end

local p1 = AgedPerson:new('Billy the Kid', 13) -- this is equivalent to AgedPerson('Billy the Kid', 13) - the :new part is implicit
local p2 = AgedPerson:new('Luke Skywalker', 21)
p1:speak()
p2:speak()
This is in love.
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by Robin »

How is it not working? Do you get an error? If so, what does it say? etc.

(Also, to use the file called "middleclass.lua" once usually calls require 'middleclass'.)
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by BlackBulletIV »

You can't require files with the '.lua' extensions; it expands all dots to slashes. If you have middleclass in its folder then just go require 'middleclass.init', and in LOVE, require 'middleclass' should work too. If you have it in your main folder as a single file, just use require 'middleclass'.

EDIT: Just noticed Robin already mentioned this to a certain degree.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: middleclass & middleclass-extras: OOP for LUA

Post by Kadoba »

I use the .lua extensions on all my require paths and I don't have any trouble. I believe it only expands the dots if it can't find the literal file first. (Although this may be different outside windows)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 58 guests