lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Using 'self' over 'Display' when defining the functions for the Display class should fix your issue.

On Sun, Nov 22, 2015 at 12:30 PM, Andreas Matthias <andreas.matthias@gmail.com> wrote:
In the code below I tried to define a static (class) variable `Display.count'.
It seems to work at first. But doesn't work any more once you rename the class.
What's the correct way to get static variables?

Andreas



Display = {}
Display.count = 0

function Display:new ()
   local me = {}
   setmetatable(me, self)
   self.__index = self
   Display.count = Display.count + 1
   return me
end

function Display:getCount ()
   return Display.count
end

dis1 = Display:new()
dis2 = Display:new()
print(dis1:getCount(), dis2:getCount())

-- now let's rename Display
DDD, Display = Display, nil

dis = DDD:new()
print(dis:getCount())