[solved] static CCArray (cocos2d-1.0.1-x-0.12.0)

Hello all,
I need to share CCArray information among several classes.
So my aproach is create a global class, with all information I need to share defined as static variables.
I have no problem accessing single object variables, but I’m fighting with static CCArray.

Any example will be willcome.

Thanks in advance,
Jordi

Well… after fighting for a while… I think I found something.

CCArray doesn’t mantain values when readed from different files, nor within same file and different function or method.
I’ve checked that address memory is the same.

Code example:

File.h
static CCArray *foo;
void method1();
void method2();

File.cpp
CCArray* File::foo = NULL 
/* *_Note1_*: any static variable must be initialized, but cannot perform "CCArray* File::foo = CCArray::array();" as usual, because then crashes when accessed from any outter method; Neither can do 'CCArray* File::foo = new CCArray();' as CCArray doesn't provide such function. */

void File::method1() {
    foo = CCArray::array()
    /* *_Note2_*: From my point of view, the problem is doing initialization within method1, but as explained on *_Note1_* it doesn't respond doing it inside File.cpp as it should be
    CCLabelTTF* aLabel = CCLabel::labelWithString("first try", "Arial", 10);
    foo->addObject(aLabel);
    stringstream output;
    output << "foo count: " << File::foo->count();
    CCMessageBox(output.str().c_str(), ""); //Returns 1, ok.
}
void File::method2() {
    stringstream output;
    output << "foo count: " << File::foo->count();
    CCMessageBox(output.str().c_str(), ""); //Returns whatever but 1, wrong.
}

Well… I think that obviously my problem is caused because of where foo initialization is done.
And I found a turn arround using CCMutableArray<CCLabelTTF*>* so I could do a ‘foo = new CCMutableArray<CCLabelTTF*>;’ inside File.cpp

If anybody finds out how to make the same with a CCArray, please, let me know.

Thanks!
Jordi :slight_smile:

P.S. Hope I haven’t mistyped anything, because I haven’t done a copy&paste, but write it from scratch.

Yeap, CCArray in 0.12.0 doesn’t offered constructor.
So i think you can do it like this

  1. implement a static function to initialize CCArray

    CCArray File::sharedArray()
    {
    if (foo == NULL)
    {
    foo = CCArray::array();
    }

     return foo;
    

    }

  2. then you can invoke sharedArray() in method1 and method2.

Thanks Minggo :slight_smile:

Jordi Altimira wrote:

Thanks Minggo :slight_smile:

I’m having a similar (but seems like stranger) problem in 2.0, and a static method doesn’t seem to have fixed the issue. It’s pretty
odd— when I do something like this:

File.h

CCArray *items;

...

File.cpp

bool File::init(){
   items = CCArray::create();
   items->addObject(example1);
   items->addObject(example2);
   CCLOG("count: %i", items->count());//returns 2
   this->method1();
}

void File::method1(){
   CCLOG("count: %i", items->count());//returns 2
}

//then later on, on a button press...
void File::method1(){
   CCLOG("count: %i", items->count());//fails with error or returns something large or incorrect
}

…CCLOG is just for example. Not really sure why I get a correct number from the first two but an unexpected result from the
last one. Anybody have any thoughts? I have a singleton class for storing global values— maybe I’ll just add the CCArray in
question to it and see if there’s any change, but I would really like to understand why this is behaving this way.

EDIT: adding the CCArray to the Singleton class didn’t fix the problem for me, so I tried creating the CCArray itself as the singleton,
and it didn’t work either. (I should point out that I’m new to C++ and cocos2dx, so this may be a familiar issue)

At this point, I’m open to any suggestions. I’m sure I can come up with some ridiculous workaround solution to at least get my game
working, but I’d love to fix this issue.

Hi Chris,
First of all: I haven’t worked with cocos2d-x 2.0, because last time I tried to update it, my game crashed everywhere.
So, without trying I think your trouble is the same I had: object initialization.

So perhaps you should have a try to Minggo advice.
If you do, let us know if it worked for you.

Regards,
Jordi

Sorry man,
I didn’t read your last edit.
I’ve just tried Minggo solution and (obviously) neither worked for me :frowning:
The only work-arround I’ve found is use a class with a ‘new’ constructor, such as CCMutableArray, in order to initialize at ‘bool File::init()’, because the problem with ‘CCArray::array()’ initialization is that crashed when tried to be modified by any method or function outside than that it was created with. :frowning:

I see no other solution…

Great… has CCMutableArray been deprecated in 2.0 version?
Yep… I’ve read it now.

I was looking for the root object of CCMutableArray, to find out another, so that be able to make a ‘new WhateverCollection’
It seems it derives from CCObject, so it isn’t a great help… perhaps CCSet is an alternative… though it isn’t ordered…

Jordi

Jordi Altimira wrote:

Sorry man,
I didn’t read your last edit.
I’ve just tried Minggo solution and (obviously) neither worked for me :frowning:
The only work-arround I’ve found is use a class with a ‘new’ constructor, such as CCMutableArray, in order to initialize at ‘bool File::init()’, because the problem with ‘CCArray::array()’ initialization is that crashed when tried to be modified by any method or function outside than that it was created with. :frowning:
>
>
I see no other solution…
>
>
Great… has CCMutableArray been deprecated in 2.0 version?
Yep… I’ve read it now.
>
>
I was looking for the root object of CCMutableArray, to find out another, so that be able to make a ‘new WhateverCollection’
It seems it derives from CCObject, so it isn’t a great help… perhaps CCSet is an alternative… though it isn’t ordered…
>
>
Jordi

Hi Jordi, sorry to have been delayed in responding to this issue, but it’s been solved in a head-slapping kind of way. I cross posted about this problem
elsewhere on the forum since I hadn’t heard anything on this. Walzer pointed out that CCArray is created as an autorelease object, so by the time I attempt
to reference it again, it’s passed out of scope. I guess I’ve been spoiled by using ARM in Objective-C!

He suggested that I use myCCArray->retain(); after creating it, followed by myCCArray->release(); when I’m done with it (in my case in the destructor). This
worked perfectly and solved my particular problem.

As for the deprecation of CCMutableArray in 2.0, it appears that its functionality has been rolled into CCArray.

Hope that helps!

—Chris