*object* undeclared (first use in function) problem

Discussion in 'iOS Programming' started by RPGamerL99, Oct 1, 2008.

  1. RPGamerL99 macrumors newbie

    Joined:
    Mar 10, 2008
    #1
    In my main.m I have the following code in my init method,

    Code:
    id po;
    po = [playerobj alloc];
    po = [[playerobj alloc]CreateGame];
    [po StartGame];
    then in main.m I have this in my idle method,

    Code:
    if (dead == YES) // error: 'dead' undeclared (first use in function)
    {
        [po StartGame]; // error: 'po' undeclared (first use in function)
    }
        [po gameidle]; // error: 'po' undeclared (first use in function)
    
    return;
    StartGame and gameidle are methods in my game.m file and the class is declared in game.h and "dead" is a variable for the "po" object (player object). Can someone help explain why that variable and object is giving errors and how I can solve it? Any help would be appreciated.
     
  2. grimjim macrumors member

    Joined:
    May 24, 2003
    #2
    I would assume that it's a scope issue. If you are declaring these variables inside a method, they won't be available to other methods. Try making them global by adding them to the .h file for that class. That might make the difference. Also, I may be missing something, by why are you allocating po twice? And if you know it's going to be an instance of playerobject, why do you declare it as an id?
     
  3. imods macrumors newbie

    Joined:
    Aug 18, 2007
    #3
    I think it should be (at least):

    Code:
    id po;
    po = [[playerobj alloc] init];
    [po StartGame];
    But of course the scope of this "po" will be limited to your init method. So you will have to create a property to hold this. Also make sure to release your object in dealloc.
    Code:
    [po release];
    Pacon!
     
  4. grimjim macrumors member

    Joined:
    May 24, 2003
    #4
    Hang on a moment...

    I've just re-read your original message, and things just aren't right on many levels.

    First of all, please tell me that you aren't adding this stuff to main.m. It doesn't go in main.m. You will probably never need to even look at main.m, and you certainly shouldn't be just sticking code like this into it. This code belongs elsewhere, such as the application delegate class or the relevant view controller. Look at some of Apple's examples to see where your code belongs.

    And with that out of the way, I think that you should add a line like:
    Code:
    PlayerObj *po;
    to the header (.h) file of the class that you have chosen to implement your code in. Note that I have given the class name a capital initial letter. This isn't really necessary, but it is a convention in Objective C, and will make your code easier to read.

    Next, you can instantiate your po object in the relevant class's init method:
    Code:
    po = [[PlayerObj alloc] init]; 
    Note that you don't need to declare an id first and then tell it that it's an object of type PlayerObj. Just initialise a PlayerObj and work with it.
    Then you can work with it:
    Code:
    [po createGame];
    [po startGame];
    Note that, by convention, method names in Objective C start with a lower-case letter. Again, it's not essential, but it's good practice and will make it easier for others to see what's going on.

    Now, you can add your idle method to the same class as your init method, and you should find that the po object is recognised.

    Sorry if that's a bit hurried - I'm not in front of a Mac with Xcode at the moment.
     
  5. hchung macrumors 6502a

    Joined:
    Oct 2, 2008
    #5
    Additionally, consider putting all this code for object creation in the AppDelegate file for your application. More likely under the applicationDidFinishLaunching method.
     
  6. RPGamerL99 thread starter macrumors newbie

    Joined:
    Mar 10, 2008
    #6
    Thanks for the replies. I'm still confused on something tho. If I have objects and game logic in the AppDelegate and the OpenGL stuff in EAGLview, how do I get the flow of the program back to the AppDelegate? I am using the template provided when creating a new project under "create OpenGL game". I am really new to this and i am confused on how to properly implement my code into the iPhone framework.
     
  7. hchung macrumors 6502a

    Joined:
    Oct 2, 2008
    #7
    Simplest way to get to the app delegate is typically
    [[UIApplication sharedApplication] delegate] I think.

     

Share This Page