Serialization and data transfer from CRunObject to CEditObj

Forum for plugin, effect and behavior programmers. Please use the Help & Support forum for help using Construct.

Post » Thu Dec 25, 2008 4:59 pm

There are several problems I'd like to clarify ^^. Working in C++ is really painful for me so tests would take hours ><'. You Devs probably know answer ^^.

1. Serialization order
Let's say I've fields: myVal1, myVal2 in EditObj, and fields: myFirst, mySecond in RuntimeObj. How should I do serialization stuff in code (serialization.cpp). Is this solution correct (treating 'ar' as stack):
[code:cwqirkhd]/////////////////////////////
// RUNTIME serialization
void ExtObject::Serialize(bin& ar)
{
if (ar.loading)
{
ar >> mySecond;
ar >> myFirst;
}
else
{
ar << myFirst;
ar << mySecond;
}
}[/code:cwqirkhd]
[code:cwqirkhd]/////////////////////////////
// EDITTIME serialization
void EditExt::Serialize(bin& ar)
{
int Version = 1;
SerializeVersion(ar, Version);

if (ar.loading)
{
ar >> myVal2;
ar >> myVal1;
}
else
{
ar << myVal1;
ar << myVal2;
}
}[/code:cwqirkhd]

2. Passing values from EditObj to RuntimeObj
Created RuntimeObj stores handle to EditObj to have access to its data and stuff. Let's say I've two values in EditObj: myVal1 and myVal2. I want to make use of them during Runtime as well (by ACE Methods). Do I need to make fields "myVal1, myVal2" in RuntimeObj as well and duplicate data from EditObj in method "void ExtObject::OnCreate()", or maybe I can access EditObj fields from ACE Methods and not worry about making fields at RunTime Object?

3. Serialization in runtime
This question is somehow related to #2. Surely serializing object in Runtime is used for QuickSave/QuickLoad its state. When making QuickSave in runtime, does corresponding EditObj data serialize as well?
B
6
S
3
G
6
Posts: 219
Reputation: 3,013

Post » Thu Dec 25, 2008 5:16 pm

[quote="BROO":1xb4pd11]
1. Serialization order
Let's say I've fields: myVal1, myVal2 in EditObj, and fields: myFirst, mySecond in RuntimeObj. How should I do serialization stuff in code (serialization.cpp). Is this solution correct (treating 'ar' as stack):
[code:1xb4pd11]/////////////////////////////
// RUNTIME serialization
void ExtObject::Serialize(bin& ar)
{
if (ar.loading)
{
ar >> mySecond;
ar >> myFirst;
}
else
{
ar << myFirst;
ar << mySecond;
}
}[/code:1xb4pd11]
[code:1xb4pd11]/////////////////////////////
// EDITTIME serialization
void EditExt::Serialize(bin& ar)
{
int Version = 1;
SerializeVersion(ar, Version);

if (ar.loading)
{
ar >> myVal2;
ar >> myVal1;
}
else
{
ar << myVal1;
ar << myVal2;
}
}[/code:1xb4pd11][/quote:1xb4pd11]

If you save myVal1 first, then you should load it first, instead of loading myVal2 first. That'd mix up the data.

[quote="BROO":1xb4pd11]2. Passing values from EditObj to RuntimeObj
Created RuntimeObj stores handle to EditObj to have access to its data and stuff. Let's say I've two values in EditObj: myVal1 and myVal2. I want to make use of them during Runtime as well (by ACE Methods). Do I need to make fields "myVal1, myVal2" in RuntimeObj as well and duplicate data from EditObj in method "void ExtObject::OnCreate()", or maybe I can access EditObj fields from ACE Methods and not worry about making fields at RunTime Object?[/b][/quote:1xb4pd11]

Data which is serialized at edittime can then be loaded similarly in OnCreate, like this:

[code:1xb4pd11]
void ExtObject::OnCreate()
{
// Load settings
bin ar;
ar.attach(info.editObject->eData, info.editObject->eSize);

int Version = 0;
ar >> Version;

ar >> tile_width >> tile_height >> speed >> play_if_stopped;
ar.detach();
[/code:1xb4pd11]

Two important things to note: you must load a blank version number (as the edittime saves one), and you must call ar.detach() after you've loaded your data. It's up to you whether you store the loaded variables (as above) or just define them locally in the function for using when creating the object.


[quote="BROO":1xb4pd11]3. Serialization in runtime
This question is somehow related to #2. Surely serializing object in Runtime is used for QuickSave/QuickLoad its state. When making QuickSave in runtime, does corresponding EditObj data serialize as well?[/quote:1xb4pd11]

The runtime serializing is used for quicksave/load, yes. The edittime data is gone by this point, so if you need it, get it in OnCreate and keep it in your run object.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Thu Dec 25, 2008 5:33 pm

Thanks. That explains everything ^^.
B
6
S
3
G
6
Posts: 219
Reputation: 3,013


Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 1 guest