Hey guys so for my game im making i have a character selection screen where the user picks three characters and my plan was to have a global array that when they select a character it adds the character to the array and removes it when they deselect it and then when the game starts it loads the sprites and their attributes according to those names. My question is how do i make a global array? everything i have tried so far hasnt worked
Thanks
-Lars
Global Array?
(40 posts) (7 voices)-
Posted 2 years ago #
-
You can have the array be in your appDelegate.h. Then add a method to reference appDelegate:
+(AppDelegate *) get { return (AppDelegate *) [[UIApplication sharedApplication] delegate]; }Then to access in your code:
[AppDelegate get].yourArrayJust remember to change "AppDelegate" to be the name of your AppDelegate or else the code I posted will not work with your code. This is how I do application globals.
Posted 2 years ago # -
Hey jd
Its not working for me.
I put NSMutableArray *Players in the @interface section and added the +(DodgeBallAppDelegat *) to the .m.Then you said to access it in my code i do [SnowFightAppDelegate get].Players;.
do i have to define that in the .h first like NSMutableArray *Players = [SnowFightAppDelegate get].Players;?
Thanks
-LarsPosted 2 years ago # -
Not sure if you did cut and paste error, but you say you added DodgeBallAppDelegat (sp?) and then call SnowFightAppDelegate get method. Names don't match.
You have to declare +(AppDelegate *) get; in your .h file.
You have to alloc init [SnowFightAppDelegate get].Players before using it as well. But after that point, you can always access it with [SnowFightAppDelegate get].Players.
I got the code from somewhere, but I can't find where any more. If you get errors, post them here.
Posted 2 years ago # -
Hey jd ,
so i couldnt get it to work for some reason and i googled it some more and saw that your not supposed to use that method. but insted make globals files.
Heres a Link:
http://tinyurl.com/lpfodvIs this a better method?
Thanks
-LarsPosted 2 years ago # -
I recommend using a Singleton if you need to somewhere to store global data. The App Delegate is not meant to be a place to store global variables.
Check out Apple's guide on Singletons:
Posted 2 years ago # -
Hey I looked at the singleton class but i just dont seem to understand it. If i want a global NSMutableArray how would i do that? and then i want to be able to add, remove and get names from that array? is there a tutorial somewhere?
Thanks
-LarsPosted 2 years ago # -
It may not be the preferred method, but when I first started I looked around and every other method of handling globals was almost the same exact thing. Even the link Korki provided is the same thing I'm doing except the author feels better about himself implementing a separate class. I'm a one man team, so I'm not worried about other people having access to my globals at runtime. Use whatever works for you.
Posted 2 years ago # -
The link Korki gave essentially is like a Singleton class. Basically a singleton is an object where there will only be one instance of in the entire application (AppDelegate is one too).
I did use app delegate before too, but it can get really messy. Data should be contained in its own place, and application delegate code in its own place.
If you have various different categories of global data that needs to be shared across the game/application, creating singleton classes works great. Basically you create the singleton class, let it have the data you need to share across the application, and then you can use it easily. So using Apple's example, let the class have an NSMutableArray instance variable, and then have getters/setters to access it.
If you're unsure, then think about whether your app/game will have a complex or long development lifetime. If it does, singletons will probably make your life easier later. If it doesn't, such as you just need to share one variable across the whole app, then appDelegate is okay...
Usage wise, it's less characters :P
(AppDelegate *) [[[UIApplication sharedApplication] delegate] array] vs.
[SingletonName array]Hope that helps.
Posted 2 years ago # -
Not much less.
[[[AppDelegate get] array]
:)
Posted 2 years ago # -
Hey edisonlabs,
So im trying this Singleton method and i found this:
http://tinyurl.com/6jvqeb
So Im using that and then my GlobalsSingleton.h is:
#import "SynthesizeSingleton.h"@interface Globals : NSObject {
NSMutableArray *_Players;
}+ (Globals *) sharedGlobals;
- (NSString*) getNameAtIndex: (int)x;
- (void) addObjectToPlayers: (NSString*)aname;@end
and my GlobalsSingleton.m is:
#import "GlobalsSingleton.h"@implementation Globals
SYNTHESIZE_SINGLETON_FOR_CLASS(Globals);
- (id) init
{
if (self = [super init])
{
_Players = [[NSMutableArray alloc] init];
}
return self;
}- (NSString*) getNameAtIndex: (int)x;
{
id name = [_Players objectAtIndex:x];return name;
}- (void) addObjectToPlayers: (NSString*)name;
{
[_Players addObject:[NSString stringWithFormat: @"%@",name]];
}@end
I then for testing i have put under touches began 1 tap:
[[Globals _Players] addObjectToPlayers:@"Butch"];and for two taps:
NSLog (@"%@", [[Globals _Players] getNameAtIndex:0]);Im getting the warning:
warning: 'Globals' may not respond to '+_Players'What am i doing wrong?
Thanks
-LarsPosted 2 years ago # -
ouch .. count minus one I added one left bracket more than needed :))
Posted 2 years ago # -
Korki,
Wanna a quick & dirty global?
If you define in the AppDelegate.h before the @interface this will be global. ie:
int levels[MAX_LEVELS]; @interface AppDelegate :In all your code you can call:
levels[idx]
This is not a very academic way you should do singletons, is not well structured, ... but works pretty fine (if ou are a organized person)
Posted 2 years ago # -
[moved to everything else]
Posted 2 years ago # -
I feel like I really need to link some basic Obj-C tutorials to this forum. Not trying to flame anyone, but if you want to use a game engine built on Obj-C, maybe you should get a hang of Obj-C first...
Anyhow, the reason why you get that warning is because there is no class method _Players defined.
Here's a very basic example of Singletons. I'm at work so I can't spend much time verifying if it's right. I'll post up an example later tonight from how I used singletons in my game.
@interface Globals:NSObject {
int val;
}
@property int val;+(Globals*)sharedGlobal;
@endThen in your implementation file:
#import "Globals.h"
static Global *sharedGlobal = nil;@implement
@synthesize val;+(Globals*)sharedGlobal {
if(!sharedGlobal) sharedGlobal = [[sharedGlobal alloc] init];return sharedGlobal;
}@end
Then to use it, you would do something like
[Globals sharedGlobal].val
This is a drastically simplified version. Well what i'm using isn't much complicated either, but it can be. And coolman is right, you can do it the quick and dirty way, but I think you may need the keywords extern in your files.
There's pros and cons to each method, you have to determine by yourself which one is the best. Obj-C singletons have a small overhead from message calling, while the quick and dirty C way doesn't have it.
Gotta get back to work, feel free to comment, there's probably a lot of suggestions that could be made.
Posted 2 years ago # -
As promised, here's a sample code of my global data manager used in Fireworks Fun. It is an extremely simple implementation, and not all variables in there are used in my game.
To use it, just follow the same stuff I listed above. It's all properties so it should be easy to use.
Posted 2 years ago # -
Thanks for sharing it.
Posted 2 years ago # -
Thanks a lot for sharing that edisonslabs I really appreciate it.
-LarsPosted 2 years ago # -
hey so the way i understood this is that when you access one of the variables it will automatically allocate space for it but when i do:
[[GlobalDataManager sharedDataManager].players addObject:@"Test"];
it wont add it the array is always null with 0 objects in it. what am i doing wrong?
Thanks
-LarsPosted 2 years ago # -
No it does not automatically allocate memory for any objects. You still must do it within one of the init/alloc function.
Look at allocWithZone and my comment in it. Doing it there should be safe.
Posted 2 years ago # -
in GlobalDataManager's -init did you remember to initialize players to an empty NSMutableArray?
Posted 2 years ago # -
ok i did forget to do that and this is probably me just being tired and not thingking right but when i do initialize it it comes up with players is unused and even when i run it, it wont work
Thanks
-LarsPosted 2 years ago # -
Can you post your code on pastie? it's most likely a minor error and will be much easier to debug by reading your code than asking you more questions.
Posted 2 years ago # -
hey im not on my computer with the project right now but the code im putting right now is players = [[NSMutableArray alloc] init]; plus ive tried a bunch of other ways. i know im probably just trying to hard or something cuz i dont think this should be this hard
Thanks
-LarsPosted 2 years ago # -
EDIT: I posted something here before, but after a minute, I don't think it was correct. In fact, that code comment I placed probably wasn't right.
What I had previously: Inside allocWithZone, did a:
sharedGlobalDataManager.players = [[NSMutableArray alloc] init];Probably isn't right since sharedGlobalDataManager only had an alloc before that and not an init. So you should probably override the -(id)init method and include the variable initializations there (init does = initialization afterall...)
So maybe have something like
-(id) init { if(self = [super init]){ self.players = [[NSMutableArray alloc] init]; } return self; }Let me know how that works.
Posted 2 years ago # -
hey that worked perfectly!
Thanks a lot
-LarsPosted 2 years ago # -
Just fyi to make sure. How are you defining the property "players"?
@property(readwrite,assign) or @property(readwrite,retain)?
if you used @property(readwrite,retain) then I think:
self.players = [[NSMutableArray alloc] init];
will leak bc players will get retained twice.
Posted 2 years ago # -
Good catch Codemattic. If the property is retain, then it should have been:
self.players = [NSMutableArray arrayWithCapacity:10]; or something of that sort.
If the property is assign, then self.players = [[NSMutableArray alloc] init]; is fine.
Posted 2 years ago # -
Problem defining global sprite in AppDelegate:
1. Declaring a sprite in a header file: global.h
2. I'm importing the header file into AppDelegate.m
3. I want to define the sprite in the app delegate.
4. My app builds with no errors but crashes when I run it.More Information:
It seems like I should be able to set the global sprite in the app delegate and use it in GameScene.m but it crashes every single time. What am I doing wrong? I've attached some come below. Please let me know if you need any more sample code from me. I'd like to fix this problem so that my GameScene loads quicker since the sprite will be defined in the app delegate and it won't have to be created every time GameScene is executed.
Code sample:
/**** * global.h ***/ #import "cocos2d.h" //declares sprite Sprite *game_hud; /**** * GameScene.m ***/ //refers variable to game_hud sprite in global.h extern Sprite *game_hud; //defines sprite game_hud = [Sprite spriteWithFile:@"GAME_HUD.png"]; /**** * AppDelegate.m ***/ #import "global.h" - (void)applicationDidFinishLaunching:(UIApplication *)application { game_hud = [Sprite spriteWithFile:@"GAME_HUD.png"]; }Posted 2 years ago # -
try:
//defines sprite game_hud = [[Sprite spriteWithFile:@"GAME_HUD.png"] retain];Posted 2 years ago #
Reply »
You must log in to post.