Is this possible, if so, how? Thanks
Switch scene, keep data?
(10 posts) (4 voices)-
Posted 1 year ago #
-
There are many ways to pass data between scene nodes and I'm not sure which one to show since it depends on your code setup.
Hey! Maybe if you can explain more of the problem and show some steps you took trying to solve it and back that up some code implementation attempts (psuedo or otherwise) then I could show you one of the solutions that would work for your problem.
Posted 1 year ago # -
Ok, basically at the end of my game I call UIAlertView for the player to choose where to submit his/her score.
Eg: Facebook - changes scene to the Facebook submission page. Now I can't access the score variable from the game scene because it is 'gone'.
Hope this makes sense :)
Posted 1 year ago # -
The usual way is to have an external GameController object which runs outside of your scenes and stores data between and out of scenes.
A very simple way is to add a property to your app delegate and then access it via something like [[UIApplication sharedApplication] delegate].thePropertyName
Posted 1 year ago # -
How do you add it to your app delegate?
Posted 1 year ago # -
I know some people might frown upon this, but I use extern.
Create a file called "globals.c"
In it say something like:
int score;
extern int score;Include the file in all the files that access the variable.
#include "globals.c"Only thing is it does not work with UIImage, CCSprite, etc. Works with float, int, etc.
Posted 1 year ago # -
Just:
int score;
extern int score;Nothing else needed.
Posted 1 year ago # -
@JavaWizKid: Properties are a basic element of objective-c. If do not know how to add a property to your delegate then you need to stop programming and start reading documentation first.
Information on properties can be found here:
Maybe you should start from the beginning though: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html
Posted 1 year ago # -
Sorry for the delayed reply. I just noticed I got into the beta for a a certain pc game so I was all excited to try it out. Forgot that I was going to help you out.
From what I can gather, everyone's suggestion are on the mark on how to keep the data that you need.
But maybe I'll try to explain why we do this. Let's start simple with a stream of thought approach.
The problem you probably noticed is that your data is 'gone' because your scene is 'gone'. The scene is gone because you switched to another scene right?"At least that's how I sees it arghh!"
So then I ask myself,
"Lam... If the scene is gone then my data is gone, wouldn't it be better to not have the data in the scene?"Introspective me is right, let's put the data somewhere else then. Except for that now the data isn't in my scene so I can't access it!
Introspective me: Couldn't we just "reference" it in our game scene?
Hold a reference in the game scene, modify it as needed, and when the scene does disappear, we'd still have the data.Oh that makes some sense! Let's take this idea with a java example (I'm assuming you understand a bit of java logic)
The game data we want to not go away.
public class GameData { public int score; }The game scene but pretending it works with cocos2d in java
public class GameScene extends CCLayer { public GameData referenceData; }The instantiation part of my classes
public static void main(String[] args) { //Create the game data GameData data = new GameData(); GameScene scene = new GameScene(); // hold the reference data. scene.referenceData = data; scene.doStuffWithThatData(); //Now if the scene is gone then the data is still intact. scene = nil; }Ok that's all fine but isn't there any easy way to access the GameData. Of course! and that's through a singleton or global variable like hactar and mz explained.
An external GameController is a great idea if we want less references passed to each class. CCDirector and quite a few other classes do this in cocos2d and that is why you can access the exact same object in any scene or node.
So why not follow that idea for your game data. Let's assume you've written up the external GameController class just like CCDirector. Now the access to store your score is easy.
//somewhere where you need to save your score //In GameScene [[MyGameData sharedData] setHighScore: theNewUserScore]; //Destroy the scene and go to the facebook submission page //In facebook page you can now get the score you just saved. int score = [[MyGameData sharedData] getHighScore];Or you can do that simply by storing it in the app delegate and then access the app delelgate in a similar way.
[[UIApplication sharedApplication] delegate]I hope this long post kinda clears things up now.
Posted 1 year ago #
Reply
You must log in to post.