Discussion for Tutorial 10 - Game Structure

Discussions for all the iPhone Screencast Tutorials.

Re: Discussion for Tutorial 10 - Game Structure

Postby mike » Thu Aug 27, 2009 8:54 am

I hadn't spotted that :D

Thanks for the code, that's going to go into my code and if all is well, which I assume it will be, it will be in the code in the book as well :)

Mike
mike
 
Posts: 670
Joined: Fri Aug 21, 2009 2:10 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Elphaba » Thu Aug 27, 2009 12:15 pm

Woo Hoo! I'll finally be a published person! ;)
Elphaba
 
Posts: 6
Joined: Sun Aug 23, 2009 3:15 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby mike1o1 » Wed Sep 02, 2009 1:59 pm

Hi Mike,

I just wanted to thank you for all your hard work and generosity on this site. I haven't done serious coding in quite some time, so a lot of these concepts (singletons, texture factories, etc.) are new to me, and your way of explaining them is quite helpful.

I'm trying to do something which I think is fairly basic. I want a certain event to trigger every second in the game. To do this, I've added a double gameTicks to the game controller, and each time updateScene;aDelta is called, aDelta is added to gameTicks. If gameTicks >= 1, then one second has passed and the action should trigger.

Here is the code, but with an NSLog statement as a test for timing accuracy.

Code: Select all
- (void)updateScene:(GLfloat)aDelta {
   
   gameTicks += aDelta;
   
   if (gameTicks >= 1)
   {
      // One second has passed
      NSLog(@"One second");
      gameTicks = 0;
   }
   
   // Update the games logic based for the current scene
   [[_director currentScene] updateWithDelta:aDelta];

}


Pretty basic, but unfortunately I seem to be running into some precision errors of some kind or something. Here is what I get on the console when I run this code:

Code: Select all
[Session started at 2009-09-02 22:35:51 +1000.]
2009-09-02 22:35:56.326 OGLGame[6401:20b] One second
2009-09-02 22:35:57.339 OGLGame[6401:20b] One second
2009-09-02 22:35:58.353 OGLGame[6401:20b] One second
2009-09-02 22:35:59.364 OGLGame[6401:20b] One second
2009-09-02 22:36:00.378 OGLGame[6401:20b] One second
2009-09-02 22:36:01.391 OGLGame[6401:20b] One second
2009-09-02 22:36:02.405 OGLGame[6401:20b] One second


etc etc. As you can see, it's not firing exactly every second, and I can't for the life of me figure out why. Even if I remove the rendering update, and the currentScene update, it still happens.

Any thoughts, or suggestions on alternatives to keeping good track of time in a game loop?

(Interestingly, if I code something similar using Objective-C++ with the mainGameLoop running inside a C++ class, I can get it to run fine, with items updated exactly every second, but I lose touch events.)

Thanks,
Mike
mike1o1
 
Posts: 3
Joined: Wed Sep 02, 2009 1:37 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Tony » Thu Sep 03, 2009 3:18 am

Hi Mike1o1,

Doing some late-night coding? :D

From your log output, it seems to be firing every second, give or take a few milliseconds. The iPhone/iPod Touch operating system (OS) and the iPhone simulator do not function in real-time. Housekeeping functions of the OS throw off the timer. Any timer you set up is serviced on a best-effort basis and is given lower priority than most, if not all, OS functions. Does your app really need millisecond accuracy or can it get by with being off by milliseconds? Have you seen any ill-effects on your game, such as jerkiness?

Cheers!
Tony
 
Posts: 16
Joined: Sun Aug 23, 2009 10:32 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Sumaleth » Thu Sep 03, 2009 9:57 am

Firstly; congratulations on a great tutorial series.

Reading about OO programming had given me an intellectual understanding of how it all worked, but it took this video series to give me a feel for it. By the forth or fifth video I was predicting additions and changes, and by this tenth video the whole system felt second nature. You just don't get that from a book, and even the corrections and changes from video to video helped reinforce the way OO works and the mindset of a programmer to make good use of it.

I see you've begun using an underscore to identify private properties within a class, but aren't using that same naming convention on private methods. I've read that you should use an underscore for private methods and I've also read that you shouldn't, so I guess it's a personal preference sort of thing but I wondered why you used them on properties but not methods?

Another thing I've read about, from a coding conventions standpoint, is that you want to have as much of the code at the top level (not-tabbed) as possible. So for example, instead of:

Code: Select all
if (TRUE)
    something;
    return TRUE;
else
    return FALSE;


You might go for something like this instead:

Code: Select all
if (FALSE)
    return FALSE;

something;
return TRUE;


It doesn't help the functionality at all, but I thought it was an interesting tip for readability and flow and I saw a lot of places during the videos where it could be used.

I also wanted to ask about subclassing. Several of the classes in these tutorials, such as spritesheets, animation, fonts, and tilemaps, look like they could benefit from being subclasses of the image class, and animation/fonts/tilemaps could perhaps be a subclass of spritesheets. If I'm understanding this correctly, I think subclassing would simplify the code. Is that something you've considered?

I've been particularly keen on the tutorials that talk about the game code structure; where the different bits go, the game loop, frames per second, the singletons, and so on. I'm hoping to get a chance to write a game in a couple of months, a game where I will need to control the frames per second as accurately as possible, so I've followed that side of things with interest.

By the way, "angel" (from AngelCode) is pronounced ain-gel, not ang-gl. :)

Again, thanks for the great tutorial series. The only down side is that I watched five months of tutorials in less than a week, and I'm dying to see it ends. :)
Sumaleth
 
Posts: 197
Joined: Thu Sep 03, 2009 9:55 am

Re: Discussion for Tutorial 10 - Game Structure

Postby mike » Sat Sep 05, 2009 5:41 pm

Hi samuleth and thanks for your feedback.

Your right about the _'s. This was something I saw Apple doing, but then read later that Apple don't like having _'s, so I stopped :)

I was interested in your comment about keeping everything at the top lever. That is something I'll have to have a look at.

Your right about the subclassing. There is no real reason why I could not subclass from the Image class. The only real reason is that I'm trying to make the code as readable as possible and if people are new to OO then subclassing too much could be confusing, even though it does make sense.

Thanks again for the comments and there will be more videos once I get some time back from doing the book :)

Mike
mike
 
Posts: 670
Joined: Fri Aug 21, 2009 2:10 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Gregorski » Mon Sep 07, 2009 11:09 pm

Could someone help me out here. I really don't understand how the following code works:

Code: Select all
if(_cachedTexture = [_cachedTextures objectForKey:aTextureName]) {
    return _cachedTexture;
}


I know what it does, but I don't understand how the assignment evaluates to either TRUE or FALSE.... Any help would be appreciated.
Gregorski
 
Posts: 10
Joined: Mon Sep 07, 2009 11:06 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Tony » Mon Sep 07, 2009 11:31 pm

if [_cachedTextures objectForKey:aTextureName] doesn't find a texture with the name you pass it, it will return zero, or FALSE.

Cheers!
Tony
 
Posts: 16
Joined: Sun Aug 23, 2009 10:32 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Gregorski » Tue Sep 08, 2009 12:00 am

So in the if statement:

Code: Select all
if(_cachedTexture = [_cachedTextures objectForKey:aTextureName])


only the left hand side of the assignment is evaluated? i.e this shorthand for:

Code: Select all

_cachedTexture = [_cachedTextures objectForKey:aTextureName];

if(_cachedTexture) {



If so then this is something new to me. I've never seen this in any of the languages i've worked in before. Thanks !
Gregorski
 
Posts: 10
Joined: Mon Sep 07, 2009 11:06 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Tony » Tue Sep 08, 2009 5:29 pm

Your expansion of the code is right on target! The original code is, as you say, shorthand to save a few keystrokes. I should have expanded it to clarify it for you.
Tony
 
Posts: 16
Joined: Sun Aug 23, 2009 10:32 pm

PreviousNext

Return to iPhone Game Tutorials

Who is online

Users browsing this forum: No registered users and 0 guests