Copytable loses Function, Bug or Behavior?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Copytable loses Function, Bug or Behavior?

Post by Bugala »

Here are two basically similar situations, one works, other doesn't:

Code: Select all

TableModel = { text="text to print"}

Function TableModel:New()
	Local o = TableModel
	SetMetaTable(o, {__index = TableModel})
	Return(o)
EndFunction

Function TableModel:MyPrintFunction()
	DebugPrint(self.text)
EndFunction

NewTable = {}
NewTable[1] = {}
NewTable[1].ThisTable = TableModel:New()

CopyOfNewTable = CopyTable(NewTable)

CopyOfNewTable[1].ThisTable:MyPrintFunction()
This first one works fine and debugprints the "text to print"

I am not going to copy my whole program, but just point out these three lines which show what happens:

Code: Select all

T_Teams[1].Deck:DrawTopCard()
T_TempTeams = CopyTable(T_Teams)
T_TempTeams[1].Deck:DrawTopCard()
Practically line 1 and 3 are identical, both are calling this DrawTopCard() function, however, the line 3 fails saying "Function drawtopcard() not found!"

Why did this first example not lose the function, while the second one did, even they seem identical? Also, is this a bug or a feature?
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: Copytable loses Function, Bug or Behavior?

Post by Bugala »

I was now able to make another version of the first one that fails:

Code: Select all

TableModel = { text="text to print"}

Function TableModel:New()
	Local o = {}
	SetMetaTable(o, {__index = TableModel})
	Return(o)
EndFunction

Function TableModel:MyPrintFunction()
	DebugPrint(self.text)
EndFunction

Function CreateTable()
	Local TempTable = {}
	TempTable[1] = {}
	TempTable[1].ThisTable = TableModel:New()
	Return(TempTable)
EndFunction

NewTable = CreateTable()
NewTable[1].ThisTable:MyPrintFunction()
CopyOfNewTable = CopyTable(NewTable)
CopyOfNewTable[1].ThisTable:MyPrintFunction()	
Difference seems to be that I had (actually accidentally) picked Local O = TableModel, when I meant it to be Local O = {}.

So I guess the point is that when I copy the Table, it doesn't copy the SetMetaTable, which is why it can't find the function, as it doesn't know where to look for it, right?

But is this supposed behavior, or not, and if it is, is there any other way around this than to manually SetMetaTable again, which in this case is bit annoying as there are several teams each with their own deck, meaning I have to SetMetaTable for each one of them separately.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: Copytable loses Function, Bug or Behavior?

Post by Bugala »

Well, I actually realised a solution to my current problem, just declare:

Code: Select all

Function TableModel:New()
	Local o = CopyTable(TableModel)
	Return(o)
EndFunction
And I don't need to deal with SetMetaTable.

Of course there can be situations where Metatable is actually useful, but in this specific case this will solve my problem.
Post Reply