how to store different data types in a ccdictionary

HI, I want to store different data types, like a float, int, ccpoint and string in a ccdictionary and then store this dictionary into an array. thanks.

For strings, if you use CCString then you can store that in a CCDictionary and CCDictionary can be stored into a CCArray. However, you can’t store arbitary data into CCDictionary since it expects the data to inherit from CCObject so that it can use retain/release. To get around this you’ll either need to wrap whatever data types into your own class that extends CCObject (which sounds like a lot of bloat) or use the std libraries and maintain the memory yourself (or C-style arrays).

I just noticed that some of these wrapper classes already exist! See CCFloat, CCBool, CCDouble, and CCInteger!

Unfortunately CCPoint does not inherit from CCObject so you can’t store those, but you can do the rest :).

C.J. Kimberlin wrote:

I just noticed that some of these wrapper classes already exist! See CCFloat, CCBool, CCDouble, and CCInteger!
>
Unfortunately CCPoint does not inherit from CCObject so you can’t store those, but you can do the rest :).

hi thanks for the reply… tried doing the same for floats and it says unknow type name ccfloat??? but the same works for integer. I dont know what I am doing wrong…

CCDictionary dict = CCDictionary::create;
CCInteger
v1 = CCInteger::create(tgid);
CCFloat *f1 = CCFloat::create(tileRect.origin.x); <— unknown type error

dict->setObject(v1, “gid”);

I’m not sure either, I tried it out and it works for me. What’s the exact compile error?

C.J. Kimberlin wrote:

I’m not sure either, I tried it out and it works for me. What’s the exact compile error?

Ya, i think they removed it for the current version of cocos2dx…. if there anyway around it???

Hrmm, could use CCDouble instead?

C.J. Kimberlin wrote:

Hrmm, could use CCDouble instead?

sorry no luck :frowning:

I just checked the latest released version (2.1.4) and it has both CCFloat and CCDouble.

Something else is wrong with either your setup or how you are using the classes.

C.J. Kimberlin wrote:

I just checked the latest released version (2.1.4) and it has both CCFloat and CCDouble.
>
Something else is wrong with either your setup or how you are using the classes.

HI, I have included my code in the above post…. could you paste yours? And I am using version cocos2d-2.1beta3-x-2.1.0…. rgds.

Using the latest stable download (2.1.4) I inserted the following into the HelloCpp sample:

    //
    // Code added to HellowWorld::init() of HelloCpp
    //

    CCArray* array = CCArray::create();
    float myFloat = 0.62f;

    // I want to put the float in the array!
    CCFloat* floatObject = CCFloat::create(myFloat);
    array->addObject(floatObject);

    // Yay!

It complied and ran no problem.

Yeah, I just checked, CCFloat and CCDouble aren’t in the older version of cocos2d-x that you are using. If you want this functionality, I would suggest updating.

C.J. Kimberlin wrote:

Yeah, I just checked, CCFloat and CCDouble aren’t in the older version of cocos2d-x that you are using. If you want this functionality, I would suggest updating.

if I upgrade … will the current game that I am working now still work? … or can I keep both versions??

It will probably work? They’ve added features and most likely fixed bugs. I’m not sure how often, if ever, they remove deprecated APIs. Only answer that can be given is to upgrade and find out :).

C.J. Kimberlin wrote:

It will probably work? They’ve added features and most likely fixed bugs. I’m not sure how often, if ever, they remove deprecated APIs. Only answer that can be given is to upgrade and find out :).

hey thanks… guess I have to work around it as otherwise I would have to start the from scratch with 2.1.4… :slight_smile: … will work with vectors for now.

still it would have been nice if we could store different data types in array.