0 Favourites

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

This forum is currently in read-only mode.
  • 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?

  • 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:

    // 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.
  • Also, this should be posted in Construct Engineering.

    and welcome to scirra.

  • Also, this should be posted in Construct Engineering.

    Heh, I didn't even check which forum this was in. Moved.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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?

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)