Saving OOP Object as Table, what to consider?

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

Saving OOP Object as Table, what to consider?

Post by Bugala »

While others can answer too, this question is mainly targeted at Andreas.

I realised that by using WriteTable and ReadTable, I could save Object Oriented Programming Objects in Hollywood.

However, I also realised it probably is not that simple and came to conclusion that instead of trying out myself, there might be more than I figure out on myself, so better ask from you directly what things you think should be taken into consideration when saving OOP objects as tables to a file.

as an example, heres the basic code for my OOP object:

Code: Select all

function OOP:New()
	o = {}
	SetMetaTable(o, self)
	self.__index = self
	o.var = 1
	Return(o)
endfunction

myoopobject = OOP:New()
Now to demonstrate problems that came to my mind, lets say I now save this myoopobject using WriteTable.

Then I run another program and decide to use ReadTable to get this myoopobject to other program.

I Guess for example debugprint(myoopobject.var) would work just fine, printing out "1".

But suppose I have some other function, like myoopobject:MyFunc, I suppose that wouldnt work since thanks to SetMetaTable and self.__index commands, it would try to find the MyFunc from OOP:MyFunc, and since I loaded this myoopobject to a completely new program, OOP wouldnt exist, or am I right?

These kind of things came to my mind, that while I suppose I basically can save OOP Object using WriteTable, there might be some things like these that I need to take into consideration if planning on doing that.

So what all things should I take into consideration if I am using WriteTable/ReadTable to save/load OOP Object?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Saving OOP Object as Table, what to consider?

Post by Bugala »

And are there any way around these problems?

Like could I somehow WriteTable/ReadTable both OOP as well as myoopobject tables and that way get it to work?

As in if I normally save these both and then load them separately using ReadTable to another program as OOP and myoopobject, I suppose myoopobject wouldnt be able to refer to the OOP anymore despite them both being loaded using ReadTable.

But is there some way I could load them both and myoopobject to be able to refer to OOP table again?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Saving OOP Object as Table, what to consider?

Post by Bugala »

To demonstrate this better, here is example code.

Program 1:

Code: Select all

TESTOOP = {}

Function TESTOOP:Create()
	o = {}
	SetMetaTable(o, self)
	self.__index = self
	o.var = 2
	Return(o)
EndFunction

Function TESTOOP.myfunc()
DebugPrint("test")
EndFunction

a = TESTOOP:create()
b = a:create()

OpenFile(1, "testoop.tbl", #MODE_WRITE)
WriteTable(1, b)
CloseFile(1)

Program 2:

Code: Select all

OpenFile(1, "testoop.tbl", #MODE_READ)
b = ReadTable(1)
CloseFile(1)

DebugPrint(b.var)
b.myfunc()
As I expected, b.var prints out 2 like it should with no problem at all.
And also as I expected. b.myfunc() doesnt work, since I suppose it tries to look for that function from original TESTOOP table, which doesnt exist in program 2.


What other things do I need to take into consideration if using WriteTable/ReadTable for OOP Object table, and are there ways around these problems, like asked in previous message too, can I someway ReadTable both TESTOOP table as well as b table for b table to be able to get the myfunc() from TESTOOP again?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Saving OOP Object as Table, what to consider?

Post by Bugala »

I figured out a way to get around the previous comments problem, change program 2 into this:

Code: Select all

TESTOOP = {}

Function TESTOOP:Create()
	o = {}
	SetMetaTable(o, self)
	self.__index = self
	o.var = 2
	Return(o)
EndFunction

Function TESTOOP.myfunc()
DebugPrint("test")
EndFunction



OpenFile(1, "testoop.tbl", #MODE_READ)
b = ReadTable(1)
CloseFile(1)

DebugPrint(b.var)

a = TESTOOP:Create()

SetMetaTable(b, TESTOOP)
b.__index = TESTOOP
b.myfunc()
and it works.

by other words I am reassigning the metatable to TESTOOP manually after I first ReadTable table b.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Saving OOP Object as Table, what to consider?

Post by airsoftsoftwair »

Yes, metatables currently aren't handled by ReadTable() or WriteTable() so that's the way to go.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Saving OOP Object as Table, what to consider?

Post by Bugala »

And I guess nothing else I need to take into consideration when Read/Writing OOP Table? At least I cant figure out anything else.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Saving OOP Object as Table, what to consider?

Post by airsoftsoftwair »

Bugala wrote: Mon Nov 23, 2020 5:58 pm And I guess nothing else I need to take into consideration when Read/Writing OOP Table? At least I cant figure out anything else.
No, AFAICS you only need to restore the metatable manually after ReadTable() because it isn't exported by that function.
Post Reply