Class and Methods

I am using the Corona Editor in Sublime, which I am liking very much. Being prone to migraines, I especially like the black background (wish there was one on this page). I am using the Class stub to build out a Class called Card. I have no problem creating new cards, but I am having problems with using methods defined in the same file as the class. 

function Card:setNumber (num)

    self.cardNumber = num

    print ("Card number is " … self.cardNumber)

end

is in Card.lua

and the call

cards[1]:setNumber(89) is in main.lua

cards[1] is a Card and I can get cards[1].number with no problem. So the object and attributes exists, but the function (method) is not getting inherited by the instance.

If I rewrite the function in main, then I can get it to work, but I would prefer the methods and instances be defined in the Class lua file. 

Has anyone dealt with this?

Can you share a snipet of the code in which the issue happens?

Example: how are you creating new classes?

Sure: here is the HeritageCard class file. Notice in this case I am using the snippet included in the editor.

module(…, package.seeall)

– local myDeck = require (“Deck”)

– snippet for class below. 

– local Class = {}

– function Class:new( o )

–     o = o or {}

–     setmetadata( o, self )

–     self.__index = self

–     return o

– end

– return Class

local HeritageCard = {}

function HeritageCard:new(o)

    o = o or {}

    setmetatable (o, self.mt)

    self._index = self

    return o

end

function HeritageCard:showImage ()      – to the background group

    local image = display.newImageRect (self.imageFile, 150, 200)

    image.alpha = 1

    image.xScale = 0.34

    image.yScale = 0.38        

    image.x = self.x

    image.y = self.y

end

function HeritageCard:setNumber (num)

    self.cardNumber = num

    print ("Card number is " … self.cardNumber)

end

function HeritageCard:createCards ()

    local cards = {}

    cards[1] = HeritageCard:new ()

    cards[1].backFile = “CardCoverInBronzeColor.jpg”

    cards[1].cardNumber = 96

    cards[1].title = “Community”

    cards[1].timeFrame = “Parallel”

    

    cards[1].imageFile = “89-Test.jpg”

    cards[2] = HeritageCard:new ()

    cards[2].cardNumber = 1

    cards[2].imageFile = “98-Test.jpg”

    

    cards[3] = HeritageCard:new()

    cards[3].cardNumber = 0

    cards[3].backFile = “CardCoverInBronzeColor.jpg”

    return cards

end

return HeritageCard


This is the main.lua code

local deckClass = require (“Deck”) 

local Card = require(“HeritageCard”)

local cards  = Card:createCards()

local myDeck = deckClass:createDeck()

local  splashGroup = display.newGroup()

   

print (" " … cards[1].cardNumber)

cards[1]:setNumber(89) – this call fails as it sees cards[1] as nil

cards[1].cardNumber = 89 – this call succeeds however 

print (" " … cards[1].cardNumber)

try changing your new function to this:

function HeritageCard:new()

    local o = {}

    setmetatable (o, self)

    self.__index = self

    return o

end

Note that Im using “self” instead of “self.mt”, and “__index” instead of “_index”, much like your commented version of the function “Class:new”

Thanks, I had modified it based upon the snippet. I started using the version from a book on lua. 

But I still get this error when I do

cards[1]:setNumber (89) – this is error/main.lua:19: attempt to call method ‘setNumber’ (a nil value)

It seems I can use the object but not a method. 

Thats strange… because it works in my simulator.

Heres the exact code Im running (I removed the parts that were unecessary for the test). Try running this and see how it goes. The error might be somewhere else.

The module:

local HeritageCard = {} function HeritageCard:new() local o = {} setmetatable (o, self) self.\_\_index = self return o end function HeritageCard:setNumber (num) self.cardNumber = num print ("Card number is " .. self.cardNumber) end function HeritageCard:createCards () local cards = {} cards[1] = HeritageCard:new () cards[1].backFile = "CardCoverInBronzeColor.jpg" cards[1].cardNumber = 96 cards[1].title = "Community" cards[1].timeFrame = "Parallel" cards[1].imageFile = "89-Test.jpg" cards[2] = HeritageCard:new () cards[2].cardNumber = 1 cards[2].imageFile = "98-Test.jpg" cards[3] = HeritageCard:new() cards[3].cardNumber = 0 cards[3].backFile = "CardCoverInBronzeColor.jpg" return cards end return HeritageCard

And the main

 local Card = require("HeritageCard") local cards = Card:createCards() print (" " .. cards[1].cardNumber) cards[1]:setNumber(89) -- this call fails as it sees cards[1] as nil print (" " .. cards[1].cardNumber)

And here’s output from the console I get when I run:

 96

Card number is 89

 89

In a way that is good for me. Could the problem be in a module statement?

I use module(…, package.seeall)  at the top of the HeritageCard class,

but no module statement on the main class. 

Interesting - When I just completely replaced my file with what you have. It works. I have no idea what is different, but I will let what I had go, and move on.

Thanks very much marcior.

Can you share a snipet of the code in which the issue happens?

Example: how are you creating new classes?

Sure: here is the HeritageCard class file. Notice in this case I am using the snippet included in the editor.

module(…, package.seeall)

– local myDeck = require (“Deck”)

– snippet for class below. 

– local Class = {}

– function Class:new( o )

–     o = o or {}

–     setmetadata( o, self )

–     self.__index = self

–     return o

– end

– return Class

local HeritageCard = {}

function HeritageCard:new(o)

    o = o or {}

    setmetatable (o, self.mt)

    self._index = self

    return o

end

function HeritageCard:showImage ()      – to the background group

    local image = display.newImageRect (self.imageFile, 150, 200)

    image.alpha = 1

    image.xScale = 0.34

    image.yScale = 0.38        

    image.x = self.x

    image.y = self.y

end

function HeritageCard:setNumber (num)

    self.cardNumber = num

    print ("Card number is " … self.cardNumber)

end

function HeritageCard:createCards ()

    local cards = {}

    cards[1] = HeritageCard:new ()

    cards[1].backFile = “CardCoverInBronzeColor.jpg”

    cards[1].cardNumber = 96

    cards[1].title = “Community”

    cards[1].timeFrame = “Parallel”

    

    cards[1].imageFile = “89-Test.jpg”

    cards[2] = HeritageCard:new ()

    cards[2].cardNumber = 1

    cards[2].imageFile = “98-Test.jpg”

    

    cards[3] = HeritageCard:new()

    cards[3].cardNumber = 0

    cards[3].backFile = “CardCoverInBronzeColor.jpg”

    return cards

end

return HeritageCard


This is the main.lua code

local deckClass = require (“Deck”) 

local Card = require(“HeritageCard”)

local cards  = Card:createCards()

local myDeck = deckClass:createDeck()

local  splashGroup = display.newGroup()

   

print (" " … cards[1].cardNumber)

cards[1]:setNumber(89) – this call fails as it sees cards[1] as nil

cards[1].cardNumber = 89 – this call succeeds however 

print (" " … cards[1].cardNumber)

try changing your new function to this:

function HeritageCard:new()

    local o = {}

    setmetatable (o, self)

    self.__index = self

    return o

end

Note that Im using “self” instead of “self.mt”, and “__index” instead of “_index”, much like your commented version of the function “Class:new”

Thanks, I had modified it based upon the snippet. I started using the version from a book on lua. 

But I still get this error when I do

cards[1]:setNumber (89) – this is error/main.lua:19: attempt to call method ‘setNumber’ (a nil value)

It seems I can use the object but not a method. 

Thats strange… because it works in my simulator.

Heres the exact code Im running (I removed the parts that were unecessary for the test). Try running this and see how it goes. The error might be somewhere else.

The module:

local HeritageCard = {} function HeritageCard:new() local o = {} setmetatable (o, self) self.\_\_index = self return o end function HeritageCard:setNumber (num) self.cardNumber = num print ("Card number is " .. self.cardNumber) end function HeritageCard:createCards () local cards = {} cards[1] = HeritageCard:new () cards[1].backFile = "CardCoverInBronzeColor.jpg" cards[1].cardNumber = 96 cards[1].title = "Community" cards[1].timeFrame = "Parallel" cards[1].imageFile = "89-Test.jpg" cards[2] = HeritageCard:new () cards[2].cardNumber = 1 cards[2].imageFile = "98-Test.jpg" cards[3] = HeritageCard:new() cards[3].cardNumber = 0 cards[3].backFile = "CardCoverInBronzeColor.jpg" return cards end return HeritageCard

And the main

 local Card = require("HeritageCard") local cards = Card:createCards() print (" " .. cards[1].cardNumber) cards[1]:setNumber(89) -- this call fails as it sees cards[1] as nil print (" " .. cards[1].cardNumber)

And here’s output from the console I get when I run:

 96

Card number is 89

 89

In a way that is good for me. Could the problem be in a module statement?

I use module(…, package.seeall)  at the top of the HeritageCard class,

but no module statement on the main class. 

Interesting - When I just completely replaced my file with what you have. It works. I have no idea what is different, but I will let what I had go, and move on.

Thanks very much marcior.