two local object get the same reference... why?

I used the following class to make an object call Deck which contains cards for a game. However, in a separate game I use the same class to make another deck with different cards. For some reason the second game to load gets the set of cards of the first game that loads as if they have the reference to the same Deck.

Why is this?

PS: (I suppose is something to do with the require, when I made two different “cards” file name differently the deck where ok. How can I have the “cards” file to work for every game without creating a different file per game?)

Thanks all

Local variable in each game:

 local Card = require( "cards" ) -- temp cards local Deck = require( "cards" ) -- all cards

When I load the deck:

Deck.addCard( Card.newCard( "googai", "ก", "g", 1 ) )

The class:

-- Cards --Init card local this = {} --cards table local cards = {} --ContainsName check local function containsName( name ) for i=1, #cards do local cardName = cards[i].name if ( cardName == name ) then return true end end return false end --Add card this.addCard = function( card ) --key local name = card.getName() if ( containsName( name ) ) then return false else table.insert( cards, card ) return true end end --Remove card this.removeCard = function( name ) for i=1, #cards do local cardName = cards[i].name if ( cardName == name ) then table.remove( cards, i ) return true end end return false end --Total number of cards this.getTotalNumberOfCards = function() return #cards end --Get No of Mastered this.noMastered = function() local noMastered = 7 for i=1, #cards do local card = cards[i] if ( card.mastered == true ) then noMastered = noMastered + 1 end end if noMastered \> 44 then noMastered = 44 end return noMastered end --Get card this.getCard = function( name ) for i=1, #cards do local card = cards[i] if ( card.name == name ) then return card end end return false end --Get card by index this.getCardByIndex = function( index ) for i=1, #cards do local card = cards[i] if ( card.index == index ) then return card end end return false end --Get all cards this.getAllCards = function() return cards end --card object constructor this.newCard = function( name, char, value, index ) local card = {} -- card name (ex: gai) card.name = name -- thai character card.char = char -- transliteration (ex: g) card.value = value card.index = index card.hold = 0 card.mastered = false card.guesses = 10 card.getName = function() return card.name end return card end --card object constructor this.loadCard = function( name, char, value, index, hold, mastered, guesses ) local card = {} -- card name (ex: gai) card.name = name -- thai character card.char = char -- transliteration (ex: g) card.value = value card.index = index card.hold = hold card.mastered = mastered card.guesses = guesses card.getName = function() return card.name end return card end --Return card return this

Lua likes to only load modules once.  You should have a card class with a “new” method to produce a unique table/object that is initialed with your card set of the deck you’re creating.

Thanks for answering.

So should it be like the following?

Local variable in each game:

local card = Card:new() -- temp cards local deck = Card:new() -- all cards

 

When I load the deck:

deck.addCard( card.newCard( "googai", "ก", "g", 1 ) )

The module

Card = {} function Card:new() -- the module here as it was written before end return Card

Yes, when you require some file for the first time, all it’s content is executed. When you include it next time, it remembered what was returned during execution and just returns this (without executing)

So you should have everything in new() function or use Lua’s metatables to get Object Oriented Programming’s “class”

Lua likes to only load modules once.  You should have a card class with a “new” method to produce a unique table/object that is initialed with your card set of the deck you’re creating.

Thanks for answering.

So should it be like the following?

Local variable in each game:

local card = Card:new() -- temp cards local deck = Card:new() -- all cards

 

When I load the deck:

deck.addCard( card.newCard( "googai", "ก", "g", 1 ) )

The module

Card = {} function Card:new() -- the module here as it was written before end return Card

Yes, when you require some file for the first time, all it’s content is executed. When you include it next time, it remembered what was returned during execution and just returns this (without executing)

So you should have everything in new() function or use Lua’s metatables to get Object Oriented Programming’s “class”