OOP trouble in Lua with Love2d

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

OOP trouble in Lua with Love2d

Coved Oswald
I have a program and I was thinking on making it OO so I made a color class to start off with, and it doesn't work the way I want it to. I have a constructor, and I want to make sure that the r, g, and b values (and eventually the a value) will not be able to be over 255. So a simple % operator will suffice, right? I have this so far:

Color = {r = 0, g = 0, b = 0}

function Color:new(o)
     local o = o or {} 
     o.r = o.r % 255
     o.g = o.g % 255
     o.b = o.b % 255
     setmetatable(o, self)
     self.__index = self
     return o;
end
--other constructors, methods, and implementation not shown
It gives me an error of "can't perform arrithmictic on table: o.b"
I wasn't sure if the error is in the love2d runtime, or is it on my bad?
Reply | Threaded
Open this post in threaded view
|

Re: OOP trouble in Lua with Love2d

Choonster TheMage
On 21 May 2013 02:19, Coved Oswald <[hidden email]> wrote:

> I have a program and I was thinking on making it OO so I made a color class
> to start off with, and it doesn't work the way I want it to. I have a
> constructor, and I want to make sure that the r, g, and b values (and
> eventually the a value) will not be able to be over 255. So a simple %
> operator will suffice, right? I have this so far:
>
> Color = {r = 0, g = 0, b = 0}
>
> function Color:new(o)
>      local o = o or {}
>      o.r = o.r % 255
>      o.g = o.g % 255
>      o.b = o.b % 255
>      setmetatable(o, self)
>      self.__index = self
>      return o;
> end
> --other constructors, methods, and implementation not shown
> It gives me an error of "can't perform arrithmictic on table: o.b"
> I wasn't sure if the error is in the love2d runtime, or is it on my bad?

If you call that constructor without an `o` argument, it creates an
empty table and assigns it to a new local variable `o`. This table
doesn't have any values associated with the "r", "g" or "b" keys, so
you're trying to take the modulus of a nil value (which will throw an
error).

You could solve this by defaulting to 0 on each of the three modulo
lines (`o.b % 255` becomes `(o.b or 0) % 255`) or setting the
metatable of the object before performing the modulo operations (so
the table lookups return 0 instead of nil).

Reply | Threaded
Open this post in threaded view
|

Re: OOP trouble in Lua with Love2d

Daniel Schuller
In reply to this post by Coved Oswald

In your constructor, if you don't pass in a o table, it makes an empty table.
You're then doing an operation o.r = o.r % 255 which is equivalent to o.r = nil % 255

The default values of r, g, and b are set on the first line in the Color table which means to access them you'd need to this


function Color:new(o)
     local o = o or {}
     setmetatable(o, self)
     self.__index = self
     o.r = o.r % 255
     o.g = o.g % 255
     o.b = o.b % 255
     return o
end

or

function Color:new(o)
     local o = o or {}
     o.r = self.r % 255
     o.g = self.g % 255
     o.b = self.b % 255
     setmetatable(o, self)
     self.__index = self
     return o
end


Reply | Threaded
Open this post in threaded view
|

Re: OOP trouble in Lua with Love2d

Ashwin Hirschi
In reply to this post by Coved Oswald

Perhaps you should also consider changing 255 into 256 for the modulo  
operation...

Ashwin.

Reply | Threaded
Open this post in threaded view
|

Re: OOP trouble in Lua with Love2d

Kaj Eijlers
Also, in general colors are are clamped and not modulo'd, as adding a bit to a bright color could result in a dark color with modulo.


On Mon, May 20, 2013 at 1:39 PM, Ashwin Hirschi <[hidden email]> wrote:

Perhaps you should also consider changing 255 into 256 for the modulo operation...

Ashwin.


Reply | Threaded
Open this post in threaded view
|

Re: OOP trouble in Lua with Love2d

Coda Highland
I would also have initialized the default to { r=0, g=0, b=0 } rather
than {}, to avoid doing a modulo on nil.

/s/ Adam

On Mon, May 20, 2013 at 10:52 AM, Kaj Eijlers <[hidden email]> wrote:

> Also, in general colors are are clamped and not modulo'd, as adding a bit to
> a bright color could result in a dark color with modulo.
>
>
> On Mon, May 20, 2013 at 1:39 PM, Ashwin Hirschi <[hidden email]> wrote:
>>
>>
>> Perhaps you should also consider changing 255 into 256 for the modulo
>> operation...
>>
>> Ashwin.
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: OOP trouble in Lua with Love2d

Michal Kolodziejczyk-3
On 20.05.2013 19:58, Coda Highland wrote:
> I would also have initialized the default to { r=0, g=0, b=0 } rather
> than {}, to avoid doing a modulo on nil.

I think OP tried to do that (with "Color = {r = 0, g = 0, b = 0} in
first line"), but failed to use this on object creation. Modified code
should be written as:

Color = {r = 0, g = 0, b = 0}

function Color:new(o)
     local o = o or {}
     o.r = (o.r or Color.r) % 255
     o.g = (o.g or Color.g) % 255
     o.b = (o.b or Color.b) % 255
     setmetatable(o, self)
     self.__index = self
     return o;
end

Regards,
miko


Reply | Threaded
Open this post in threaded view
|

Re: OOP trouble in Lua with Love2d

Michal Kolodziejczyk-3
On 21.05.2013 08:12, Michal Kolodziejczyk wrote:


>      o.r = (o.r or Color.r) % 255

...should be % 256 of course.

Regards,
miko