Cocos2d-x 3.1 Vector Value and Strings

Hello,

I have a quick question about how to use Vectors in Cocos2D-X 3.1.0.

I am currently porting a game to Cocos2D-X 3.1.0 and am trying to replace arrays of strings.

I used to have

CCMutableArray<CCString*> * myArray;
myArray = new CCMutableArray <CCString*>();
CCString* dataString = new CCString("myString");
myArray->addObject(dataString);

And have replaced them by

Vector<CCString*>* myArray;
myArray = new Vector <CCString*>();
CCString* dataString = new CCString("myString");
myArray->pushBack(dataString);

But I would like to get replace the CCString as they are deprecated.

From what I understand I should be able to use Value type.

Vector<cocos2d::Value*> * myArray;
myArray = new Vector<Value*>();
Value dataString("myString");
myArray->pushBack(&dataString);

But this does not work.

Could anyone tell me the proper method of storing strings in a vector in Cocos2D-X V3.1.0 ?

For immediate use you can use __String, but I think they want you to use a cocos2d::Value like you said. I haven’t used it, but I found its wiki page.

The memory of cocos2d::Value is handled automatically by its own destructor. So please stick to the best practice of c++ memory management rules when handling the memory of cocos2d::Value.

Sounds like you should use standard STL containers, like std::vector. And without pointers.

If you only want strings in a vector, you don’t need to use Value, which can take different types. Just use std::vector and std::string:

std::vector<std::string> myvec;
myvec.push_back("myString");

Doh, that was going to be my suggestion!

Thanks everyone for your help, I am now using a std::vector but this means I am mixing std::vector and cocos2d::Vector.
I was hoping for a pure cocos2D way of doing things but at least this remove the numerous warnings I have.

You might as well use just cocos2d::Vector in that case. It’s using std::vector internally anyway.

Cocos2d-x typedefs ValueVector (and ValueMap)… maybe that’s what you want? It’s simply:

typedef std::vector< Value > ValueVector;
typedef std::unordered_map< std::string, Value > ValueMap;
typedef std::unordered_map< int, Value > ValueMapIntKey;

But you don’t have to mix up vector types.

I have just used std::vector everywhere possible and only cocos2d::Vector if absolutly required.
Thank you