My object is not working?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Moonkis
Prole
Posts: 14
Joined: Thu Jan 09, 2014 4:37 pm

My object is not working?

Post by Moonkis »

So before anyone flame me! I did read the recommended forum rules, check the wiki and searched the forums. I didn't find any clear answers so I'll just go ahead now.

I'm starting learning Love2D (and Lua alongside), coming from an OOP background with C++ and C# I immediately tried gaining some insight in how to manage objects in lua. Using http://lua-users.org/wiki/ObjectOrientationTutorial, the first example "Simple metatable-based class" is what I used because it looked simple enough, and it works in native Lua. Of course it's not that simple when adapting it to Love2D...

Here is my current code:

Code: Select all

require("Hamster")

function love.load()
   hamster = Hamster.new()
end

function love.update(dt)
   hamster:update(dt)
end

function love.draw()
   hamster:draw()
end

Code: Select all

Hamster = {}
Hamster.__index = Hamster

function Hamster.new()
  local obj = {}
  obj.img = love.graphics.newImage("hamster.png")
  obj.x = 0
  obj.y = 0
  obj.speed = 300
  
  -- setmetatable(obj, { __index = Hamster }) For some reason I need this or it will not work at all!
  return obj
end

function Hamster:update(dt)
   if love.keyboard.isDown("right") then
      self.x = self.x + (self.speed * dt)
   end
   if love.keyboard.isDown("left") then
      self.x = self.x - (self.speed * dt)
   end

   if love.keyboard.isDown("down") then
      self.y = self.y + (self.speed * dt)
   end
   if love.keyboard.isDown("up") then
      self.y = self.y - (self.speed * dt)
   end
end

function Hamster:draw()
 love.graphics.draw(self.img, self.x, self.y)
end
Why exactly don't it work? Shouldn't it work?
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: My object is not working?

Post by Lafolie »

Welcome to the forums.

The __index metatable event takes a function that is executed when triggered. Since you seem to have a grasp of things I'll just point you to the lua-users.org page where you can find all the info you need. You seem to be setting the event to a table reference, which isn't going to do much for you.

Code: Select all

mt = {__index = function(tbl, key) return "unassigned key lookup!" end}
foo = setmetatable({bar = "bar"}, mt)
print(foo.bar) --bar
print(foo.love) --unassigned...
Also, if your goal isn't to write your own OOP implementation you should check out some of the libs made by resident lövers (or at least take a peek to see how they work).

[wiki]Category:Libraries[/wiki] (there's a bunch of other stuff here you might like too!)
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: My object is not working?

Post by kikito »

This should also work:

Code: Select all

 setmetatable(obj, Hamster)
If it doesn't, please include the error message you get. Saying "it doesn't work" makes it very difficult to know exactly what is wrong (or better yet, create a .love file so we can test it quickly)
Lafolie wrote: The __index metatable event takes a function that is executed when triggered. [...]. You seem to be setting the event to a table reference, which isn't going to do much for you.
That is not correct. Using table references on the index metamethod is contemplated. If you do this:

Code: Select all

setmetatable(A, {__index = B})
And A and B are tables, you are doing the equivalent of:

Code: Select all

setmetatable(A, {__index = function(t, key) return B[key] end})
Only the first option makes the lookup a bit faster. (and getmetatable(A).__index returns a table in one case and a function in the other)

In addition to speed, having a table on the __index can have other benefits (i.e. editors with autocomplete features can "deduce" the methods from the table, but not from the function) while the function is usually more flexible (you can perform calculations with the name and do more advanced things, like declaring methods lazily).
When I write def I mean function.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: My object is not working?

Post by Lafolie »

Ah! That's awesome. I haven't played around with metatables for a while. This is why I use a class lib someone else made!
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 52 guests