Help with arrays/vectors when writing a C++ plugin

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

Post » Fri May 14, 2010 11:32 pm

Hello.

I am working on a C++-plugin that is a string container. It needs to store any amount of strings that the user inputs. Hence I thought that using a vector<CString> would be a good idea. Unfortunately, visual studio disagreed.

The serialization that is needed don't seem to work on vectors nor arrays. :/ What should I do?
B
1
G
2
Posts: 4
Reputation: 633

Post » Sat May 15, 2010 1:09 am

When serializing the data in a vector you need to serialize the size of the vector and then loop through and serialize separately each element. Here's an example:

[code:13klpbmo]// RUNTIME serialization
void ExtObject::Serialize(bin& ar)
{
if (ar.loading) {

// Load vector size
int vectorSize;
ar >> vectorSize;

// Temporary string to hold the loaded element
CString tempElement;

// Load each vector element
for (int i = 0; i < vectorSize; i++)
{
ar >> tempElement;
myVector.push_back(tempElement);
}

}
else {

// Save vector size
ar << (int)myVector.size();

// Loop through the vector and save each element
vector<CString>::iterator i = myVector.begin();
for ( ; i != myVector.end(); i++ )
{
ar << *i;
}

}
}[/code:13klpbmo]
I hope this helps. :)
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sat May 15, 2010 2:11 am

Also, this should be posted in Construct Engineering.
and welcome to scirra.
Spriter Dev
B
88
S
21
G
12
Posts: 3,240
Reputation: 16,486

Post » Sat May 15, 2010 2:34 am

[quote="lucid":km2f8sd2]Also, this should be posted in Construct Engineering.[/quote:km2f8sd2]
Heh, I didn't even check which forum this was in. :P Moved.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sat May 15, 2010 4:32 pm

Yes, it worked! : D

Semi-worked at least.

The serialization in serialize.cpp worked fine with that you posted. However, in runtime when it is supposed to load the serialization, it fails even if I just copy paste the loading part from the serialize.cpp file.

This is not needed by me though since the game we are writing never loads in runtime so it works for me. However if I am supposed to post the plugin onthe forum, it would be nice to fix this. Any ideas on how to?
B
1
G
2
Posts: 4
Reputation: 633

Post » Sat May 15, 2010 5:05 pm

Yeah, I just remembered that you need to clear the vector with clear() before doing anything else when loading. That should fix your problem, although it's hard to say without seeing the code.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817


Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests