0

I'm going to be super simple.

how do I set these variables in one class map.m

static const uint32_t buildingStructureCategory = 0x1 << 0;
static const uint32_t buildingWindowCategory = 0x1 << 1;
static const uint32_t buildingSignCategory = 0x1 << 2;
static const uint32_t buiildingSignCategory = 0x1 << 3;
static const uint32_t buildingDoorCategory = 0x1 << 4;
static const uint32_t buildingRoofCategory = 0x1 << 5;

but also view the set values in game.m

so say i wanted buildingStructureCategory value in an NSLog in game.m

How do i do this?

Mutch95
  • 271
  • 3
  • 14
  • 2
    Maybe http://stackoverflow.com/questions/11942458/referencing-a-static-nsstring-const-from-another-class will help you. – Popeye Apr 23 '14 at 08:12
  • Please note if they are in `map.m` they are private move them to `map.h` and they are public so everywhere you import `map.h` you will be able to access them. – Popeye Apr 23 '14 at 10:03

5 Answers5

1

Your question is confusing. If you create these constants in map.m they will be visible locally only (im map.m).

If you want to use the constants in another class (not map), then you should define them in a header file (for example map.h), and import that into game.h. However, that exposes everything else that is defined in map.h to game.h and game.m.

You might want to consider creating a separate class for your constants, and import that wherever it is needed (for example map.h and game.h). That way your constants (and only your constants) will be visible to any object that needs to use them.

Oh, and you should get rid of the static keyword, it is really not necessary.

katzenhut
  • 1,742
  • 18
  • 26
1

All that you need to do is move the from your implementation file (.m) to your header file (.h) whilst in the implementation file they are only accessible within your implementation file and nowhere else but when you have moved them to your header file you will be able to access them in class that import the map.h so change to

#import ..... // Whatever imports you need in your header file 

const uint32_t buildingStructureCategory = 0x1 << 0;
const uint32_t buildingWindowCategory = 0x1 << 1;
const uint32_t buildingSignCategory = 0x1 << 2;
const uint32_t buiildingSignCategory = 0x1 << 3;
const uint32_t buildingDoorCategory = 0x1 << 4;
const uint32_t buildingRoofCategory = 0x1 << 5;

@interface Map : NSObject // Or whatever your Map class is a subclass of
// Whatever else you require in your header file.
@end

Then whenever you do #import "Map.h" you will be able to easily access your constants.

The reason why you would have them in your Map class and not in another class are because these are clearly associated with your Map class and only really need to be used when using the Map class and no other class so there is no logical reason to add these to a separate header file that you would also have to import separately, but if you wish to take that path which there doesn't seem to be any point in doing so you can have a look at this answer Referencing a static NSString * const from another class

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
0

Create a NSObject Class and delete .m file. Just need .h file only, then

static const uint32_t buildingStructureCategory = 0x1 << 0;
static const uint32_t buildingWindowCategory = 0x1 << 1;
static const uint32_t buildingSignCategory = 0x1 << 2;
static const uint32_t buiildingSignCategory = 0x1 << 3;
static const uint32_t buildingDoorCategory = 0x1 << 4;
static const uint32_t buildingRoofCategory = 0x1 << 5;

then where you want import the class then ou can access any where from the project

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • What difference does this make over what they have already got? You have just moved them from `map.h` to some random class that does the same thing. And it would be a subclass of `NSObject` it wouldn't be an `NSObject` class – Popeye Apr 23 '14 at 10:02
  • @Popeye - the "random class that does the same thing" can be imported wherever it is needed without exposing other parts of the map.h - file. see my answer for details. – katzenhut Apr 23 '14 at 12:59
  • @katzenhut not the way it should be done really, if these are associated with the map class and should only be used with the map class then they should be included in the map class not in a random class of it's own that could be imported and not make any sense why it's in a class without the thing it should be working with, so I completely disagree with your answer. – Popeye Apr 23 '14 at 13:16
  • @Popeye - Sure. Its just that these constants are not always associated with only one class, and a utility header class can do wonders for you in these cases. also please note that i also suggested the approach of keeping them in the map-class, albeit in the header. Just do what works for your usecase... – katzenhut Apr 23 '14 at 13:42
  • @katzenhut Where do they say that they are also associated in another class? He says about setting values in another class that is all nothing about them being completely separate from the `Map` class, and to be on honest based on his own answer we are all wrong as these shouldn't be `static constants` at all they should be properties so they definitely should be in the `Map` class. – Popeye Apr 23 '14 at 13:46
  • @Popeye - The OP sure didnt mention that they want to use them independently. I just added an extra usecase to my answer after mentioning the possibility of moving them to the header file. So maybe theres more information in my answer than is needed. What is your point? – katzenhut Apr 23 '14 at 13:49
  • @katzenhut To be honest based on the OPs answer we are all wrong, it looks like they wanted to know how to set a constant from another class, which obviously you can't assign a constant after it has been set. I'm going for OP not making it clear enough for us to give a good enough answer. So going to give +1 to your answer as it was the OP that was unclear in what they want not us. – Popeye Apr 23 '14 at 13:51
-1

If you want to access an object from multiple classes, turn it into an Singleton shared object. So you can access it from any whwere.

Rukshan
  • 7,902
  • 6
  • 43
  • 61
-1

I created a method in map.m called createCategories: all it did was assign the value to each instance variable which was created from the @property's I declared in map.h.

map.h

@interface map : SKNode

@property uint32_t buildingStructureCategory;
@property uint32_t buildingWindowCategory;
@property uint32_t buildingSignCategory;
@property uint32_t buiildingSignCategory;
@property uint32_t buildingDoorCategory;
@property uint32_t buildingRoofCategory;

map.m

-(void)createCategories{

    _buildingStructureCategory = 0x1 << 0;
    _buildingWindowCategory = 0x1 << 1;
    _buildingSignCategory = 0x1 << 2;
    _buiildingSignCategory = 0x1 << 3;
    _buildingDoorCategory = 0x1 << 4;
    _buildingRoofCategory = 0x1 << 5;

}

I then alloc and init an instance object of the map class in game.m after allocating and initialising the instance object I then called the method createCategories: and used the variables from map.m as I pleased with the dot syntax.

-(SKSpriteNode*)createCharacter{

    categoryBitManager = [[ProceduralMapGeneration alloc]init]; //alloc and init instance object
    [categoryBitManager createCategories]; //calling method to set property values

    NSString *characterString = [NSString name:@"Player" andRetinaBool:_retina];

    SKSpriteNode *character = [SKSpriteNode spriteNodeWithImageNamed:characterString];

    character.name = @"character";
    character.size = CGSizeMake(10, 10);
    character.position = CGPointMake(self.scene.size.width/2, self.scene.size.height/2);
    character.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:character.size];
    character.physicsBody.categoryBitMask = playerCategory;
    character.physicsBody.collisionBitMask = categoryBitManager.buildingStructureCategory; // accessing property

    NSLog(@"the value is %u",character.physicsBody.collisionBitMask); //testing is value is same as in map.m


    return character;
}
Mutch95
  • 271
  • 3
  • 14
  • To be honest your answer has confused the hell out of me. Your answer doesn't seem to match what you where asking. If you where asking about how to set constants which your answer makes it seem like you were, you can't note the word `const` it means the value assigned to that variable is constant and can't be changed. You might want to make your questions clearer in the future. – Popeye Apr 23 '14 at 13:49