Appdelegate vs. UIViewcontroller

Member
Posts: 27
Joined: 2011.04
Post: #1
I was wondering whether it would be better to use the appdelegate or a viewcontroller as the state manager for my game. I'm having trouble deciding because both have their advantages. The appdelegate would be much more easily accessible as you can get a reference to it anywhere, and that would be useful inside the game state classes when you wanted to tell the state manager to change states. However, using a viewcontroller would also have some advantages. I am most likely going to also use viewcontrollers as my game states, so if I used another viewcontroller as my state manager I may be able to present game states in a modal transition which would be useful. I also noticed that the flipside project template in xcode uses a rootviewcontroller as its state manager, so is there any reason why that is used instead of the appdelegate? However, one problem with a viewcontroller state manager is that the game states would need a reference to the state manager in order to tell it to change states (or is this when I should use delegation?). I am leaning towards using the appdelegate as the state manager as that would be a lot easier to change states with from inside a state itself, and if I created some transitions by using Core Animation then modal transitions wouldn't be needed that much. However, I would very much like some input from you guys on which would offer advantages, or any other ideas.

BTW, I hope the post was easy to follow, it's kinda messy. Wacko

A 14 year old app developer...god help me...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
didn't we just go through this? neither, a new class.
Quote this message in a reply
⌘-R in Chief
Posts: 1,265
Joined: 2002.05
Post: #3
There really are a zillion different ways you can do this. The advantages between several approaches can be completely subjective.

If you made the topmost view controller the "state manager", there's no reason you can't access it from anywhere in the project either. At the simplest, the app delegate would have a reference to it and object in the class can get a reference through it that way. Use a macro to make access prettier if you want.

You can also use a third object, a singleton whose only job is to manage the states, their modality etc. You could have the state manager know about the topmost view controller, or not. In one approach, the state manager would push the states/view controllers into the top one as part of switching states, or you could use one of the delegate/notification/KVO approaches and have the topmost vc do the push/popping based on those messages.

Lots of different ways. If what you're doing feels really wrong, then it probably is. If it seems slightly awkward but so would every other approach, you're fine. Wink
Quote this message in a reply
Member
Posts: 27
Joined: 2011.04
Post: #4
OneSadCookie: I suppose, it's just that I thought you were more talking about the game logic than the state management. Sorry though.

SethWillits: Okay, thanks. What I have in mind right now seems like a good way to go about it, so I'm going to stick to it. Like you said, it's probably a lot more subjective than I thought, and there are too many ways to really have a best method. And really, whatever works, works.

Thanks for your input, that brings me one step closer. Rasp

A 14 year old app developer...god help me...
Quote this message in a reply
Member
Posts: 60
Joined: 2009.06
Post: #5
I would totally endorse using Cocos2D, it takes care of a lot of things for you.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
State management is not really one of those things however.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 60
Joined: 2009.06
Post: #7
I guess my game is just so simple that I haven't run into any issues...I just used one if statement for game running/not running. Everything else is handled by scenes and data is passed between scenes via NS User defaults.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How do I add a UIViewController to GLSprite? Rasterman 0 2,658 Sep 20, 2010 11:49 AM
Last Post: Rasterman