Using metatables with middleclass

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
User avatar
silver_hawk
Prole
Posts: 36
Joined: Mon Feb 27, 2012 2:19 pm

Using metatables with middleclass

Post by silver_hawk »

I'm using middleclass for my classes in my game, and i set a lot of variables in

Code: Select all

function player:initialize()
   variables ...
   ...
   ...
end
Some of the variables are single variables but others are tables.

I was wondering if someone could point me out how to do proper getters/setters so that fx. if I want to get the variable hp, which is a table with more values,
I would get the first value in the table ie: hp[0] from player.
example

Code: Select all

hp = {[1] = 2, [2] = 4, [3] = 6}
a simple function on the player would be

Code: Select all

player:get(variable)
if type(self[variable]) == "table" then
   return self[variable][1]
else
   return self[variable]
end
but is it possible via. metatables to make this function execute using just player.hp instead of player:get("hp") ?

I've played around with the tables, but couldn't get it to work :)
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Using metatables with middleclass

Post by MarekkPie »

Seems like you are coming from a Java mindset, which is fine. But if you have no need to hide the variable other than it being "good Java practice" then don't hide it.

It looks like you are going through an unnecessary loop just to make Lua mold to what you might be more familiar with.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Using metatables with middleclass

Post by ejmr »

I second MarekkPie's advice. That doesn't look like a situation where you get any benefit from getters or setters. But having said that, you can use the metatable function '__index' to execute a function, or whatever else, when accessing an element in a table.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Using metatables with middleclass

Post by kikito »

middleclass needs to use the metatables for class stuff. Simply modifying a class' metatable will not work as expected. You could try to extend the existing change the metamethods, but the setup would be complex. Also, you would be paying a performance tax every time you called a method in any instance.
I was wondering if someone could point me out how to do proper getters/setters so that fx. if I want to get the variable hp, which is a table with more values,
I would get the first value in the table ie: hp[0] from player.
example

Code: Select all

hp = {[1] = 2, [2] = 4, [3] = 6}
Forgive me for saying this, but that's a bad design. Instead, I recommend you have the information of each instance inside the instance, and then have one table holding references to the instances themselves:

Code: Select all

function Player:initialize()
  self.hp = 10
end
...
entities = {}
entities[1] = Player:new()
You will achieve the same thing, and will have the data better organized (the internal parts of each instance will be contained inside it, instead of being "spread around" into several tables).

By the way, I strongly recommend naming your classes in UpperCase and your variables and methods in lowerCase. See the naming conventions.
When I write def I mean function.
User avatar
silver_hawk
Prole
Posts: 36
Joined: Mon Feb 27, 2012 2:19 pm

Re: Using metatables with middleclass

Post by silver_hawk »

Okay thank you all :) I just thought that it would be nicer to have to do the player.hp instead of player.hp[1] because I always needs the first element for things outside the player class environment, the other indexes are for internal operations :)

And ty for the naming conventions :) better bring my code to proper conventions right away :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest