How to Pass DATA/values from oneClass.cpp to AnotherClass.cpp

Hi guyz. Suppose I have some data that I want to pass from Scene1.cpp to Scene2.cpp, how do I do that?

for Example let’s say…

in Scene1.cpp :
int myArray[]= {1,2,3,4,5,6,7,8,9,10};
int globalNumber = 777;

How do I pass those data to Scene2.cpp?

Show me the steps please :smile: thanks!!

Give Scene2 a function that receives the data? Have a global data store?

In one app I made, I had a loading screen between scenes and I would pass data from the first scene to the loading screen scene (passing it to its constructor) which would remember the data and then pass it onto the second scene (when calling its constructor). But if you are going directly from Scene1 to Scene2, then you could just pass the data to Scene2’s constructor.

I also had a globally accessible data store for data I might want to keep until later (or save to file and load again when the app is next run.)

did you have to use a Singleton class? Coz Im not familiar yet how to make my own…hmmmm… let me figure out what you’re talking about :smiley: some snippets of code, if you have more time, would surely help

Yeah, exactly as what @grimfate said…

Global variable or pass it through constructor…
You may also save the data using UsersDefault in the scen1 and then retrieving it before the next scene(scene2) loads…
But this could be more costly thing. But there is always trade of between keeping things in RAM(by using global variables) and keeping data in phone’s memory or sdcard

For global variables… you just can make a header file and include it wherever you need to use those variables…

For passing through constructor you can pass some parameters while creating the new scene…
And then instead of leaving your contructor’s body empty(which you did in a post… hope you rem. we had discussion on some topic yesterday…), there you can initialize some variables that are defined inside the header file of this new scene.

For UserDefaults, you need to use UserDefault API :smiley:
For this you need to save the data in scene1 and then you can create a new scene.
And then in the constructor or whichever function just retrieve this data.

Just a concept more: It might happen that if you’re not encrypting the data while storing in scene1. It is because it would be costly to encrpyt and descrypt in the next scene. I read recently read somewhere that you can check what all apps are running simultaneously with your own app. And if that app is something that can change user’s data externally then you can crash/close your app intentionally giving some Message to the user… Like 'Don’t do Fishy Things :smiley:

@catch_up of course I remember our conversation. Good thing we managed to solve it! hahaha. It was in the cocos2dx Wiki to begin with lol glad @StainzE pointed that out

ok will review your pointers guyz. Let me just see how this works. Remember Im trying to pass a whole bunch of data , like an array so It’s not just one, it’s gonna be many :smile:

Oh yes, @catch_up is onto it. Use UserDefault for persistent data or data that will be shared by lots of classes. I’m not sure if it was in Cocos2d-x 2.x, but whether or not it was, I was new and didn’t know so I just made my own equivalent. But no use making your own version when you have that available.

Use the constructor or passing the data to a function method if you just need Scene2 to get the data and then throw it away.

yes this is important…

And ya
@grimfate

yes… v2.x was having it to save the data… this kind of things is always available in good frameworks and cocos2d-x is way better than those good ones :smiley:

Yeah… I rem. I went through all the wiki when I begun with cocos2d-x…
This is the most imp. thing, I feel, every beginner should rem. even if he is not understanding parts of it… Atleast he should be aware what all is inside it :smiley: Anyways… more resources one knows more is chances of getting something resovled :smiley:

@grimfate if you have spare time, can you write a snippet code here using the ‘pass a data to a function method’ ? I think that’s what I want to learn also :smile:

@catch_up yeh totally agree man :smiley:

Just something like

class Scene2 : public Scene {
  ...
  public:
    // Scene2 gets the data here
    void receiveData(void* data) {
      // do whatever you want with the data
    }
    ...
};

class Scene1 : public Scene {
  ...
  // this function is called when you want to move to the next scene
  public changeScene() {
    // create the next scene
    Scene2* scene = Scene2::create();

    // give your data to the next scene
    scene->receiveData(data);

    // change scene
    Director::getInstance()->replaceScene(scene);  
  }
  ...
};

So, just give your second scene a function for receiving data (such as receiveData) and call it with the data after creating it but before changing scene to it.

You could pass the data to the constructor, e.g.

Scene2::Scene2(void* data) {
  Scene();
  // handle data here
}

or create an init method, e.g.

void Scene2::initWithData(void* data) { 
  init();
  // handle data here
}

But I’m not sure off the top of my head how these would interact with the create method for the scene. (FYI There may be mistakes in the code, as I’m typing from memory. I usually use JS instead.)

Hey, I believe the cleanest way I’ve found so far is to use the Scene constructors. Most other examples abuse Singleton patterns or static variables which are generally bad to use without a very specific need.

The recieveData() approach isn’t bad but I prefer a less general approach one that doesn’t initialize after construction, and would much rather use the constructors to pass data.

Check out my response on another thread:

We have tutorials on exactly this http://www.sonarlearning.co.uk/coursepage.php?topic=game&course=cocos2d-x-pass-data