Zilarrezko wrote:
Code: Select all
instance.x, instance.y = x, y
instance.w, instance.h = w or 32, h or 32
Didn't know you could do that
Well, this is a handy way to create new objects with default values.
Zilarrezko wrote:
I did try
But it didn't work so I thought it wouldn't work in any case.
There is definitely a problem with this. You have to know how Lua deals with multiple assignments.
In this case, you have two variables on the left side of the assignment operator ("="), and only a single value on the right side.
Lua will assign this value to the first variable, and will assign nil to all the variables remaining. So recW will be 10, while recH will be nil.
Zilarrezko wrote:Whatever happened to the karma system by the way?
Broke. As far as I know.
Zilarrezko wrote:
edit: forgot to mention, that one of the tile:create function was suppost to create a new type of tile, should I just create a method "type" in the class? or just create a different class?
It depends. What do you mean by " a different type of tile" ? In what way is this supposed to be different ?
You can have it as an optional paramater, and let your methods handle it differently... But if the "new tile" behaves like the others, but implements new methods,
maybe inheritance is what you are looking for.
Zilarrezko wrote:
edit: Yup, now getting an error. Says that the second argument... or self.x (probably self.y too) is nil. I tried this...
Code: Select all
function tile:draw()
if instance then
love.graphics.setColor(255,0,0,255)
love.graphics.rectangle("fill", self.x, self.y, self.w, self.h)
end
end
The "if-clause" is wrong.
instance is
nil, here. Remember that, when calling a method with a colon, Lua spawns a value as a first argument to the method itself. That value can be caught with "self", not anything else.
Example:
Code: Select all
local t = {}
t.func = function(...) print('arguments', ...) end
t.func(1) -- will print 1
t:func(1) -- will print a table, then 1. The keyword "self" will reference this table within the function body. And this table is actually "t" itself.
Replace
instance with
self, it should work fine.
But IMHO, it won't make any sense, because since you are calling the method with a colon, you
will always have a
self argument.