8

I am trying to design an architecture for a SceneKit iOS game. I've shown below two rough high level object diagrams/graphs of my current preferred ideas.

I am only looking at the high level architecture at the moment, i.e. managing transition between Menu/GameLevel/Config/Paused app states and making the app data-driven, so that it can handle multiple levels, etc. Once I get this working, then I'll address lower level architecture, e.g. for the GameLevel state I'll have a GameLevelModel object to represent an actual game level logic.

Would be great to hear which one looks more promising, any obvious pitfalls or things to avoid?

Version A) MVC based, with Custom Container & Nested VCs

I'm trying to stay close to the MVC paradigm with this version.

The behaviour of the app changes significantly between its different states (Menu/GameLevel/Config/etc) so I'll be using a permanent container view controller that will manage/transition between several nested VCs, in a parent-child relationship as per Apple's "View Controller Programming Guide for iOS". These will be effectively entire MVCs.

Because none of Apple's container controllers (UINavigation-, UISplitScreen-, UITabBar-) are suitable (I need the entire screen) I'll have a custom container VC (RootViewController) that will have one nested child VC at all times. The View of each child VC will be fully covering the container's one at all times. The transition between child VCs will be managed by the RootVC and driven by its StateMachine.

Each child VC (effectively entire MVC) is created each time when its corresponding state becomes current and then deallocated when it is replaced with next state's one and no longer needed. Similar to how UINavigationController deals with it's contained VCs.

My only worry with this architecture is that it seems a bit heavy. SceneKit looks like it's designed to work with a single SCNView and to transition between multiple SCNScenes when we need to change the 3D content. This gives more transition options as well.

                         --------------------------
                         |   RootViewController   |
                         --------------------------
                           |  |   |        |    |
     ------------------    |  |   |        |    |      ------------------
     | GameLevelsData |-----  |   |        |    -------|     SCNView    |
     ------------------       |   |        |           ------------------
     ------------------       |   |        |
     |   PlayerData   |--------   |        |
     ------------------           |        |
                                  |        |
                       ----------------    |
                       | StateMachine |    |
                       ----------------    |
                         |                 |
                         |-(*MenuState)    |
                         |-(LevelState)    |
                         |-(ConfigState)   |
                                           |
                                           |
                         ------------------------
                         |  MenuViewController  |
                         ------------------------
      -----------------     |                |      ------------------
      |   MenuData    |------                -------|  MenuSCNView   |
      -----------------                             ------------------

   (child VCs for other States, Level-/Config-/etc are created as needed)
                                           |
                         ------------------------
                         |  LevelViewController |
                         ------------------------
     ------------------     |                |      ------------------
     | GameLevelModel |------                -------|  LevelSCNView  |
     ------------------                             ------------------

Version B) Scene-based (one VC, multiple Scenes)

I'll have only one View Controller (GameViewController) with its View (SCNView) and they will last for the entire app lifecycle. (I will not have container and nested VCs here.)

The change in behaviour and content between app states (Menu/GameLevel/Config/etc) is achieved by the only View transitioning between Scenes and different TouchHandlers (UIResponder subclasses for each state) given First Responder status when relevant.

The (Menu-, GameLevel-, etc) TouchHandler classes will only override touchesBegan/Moved/Ended and canBecomeFirstResponder so that I can intercept touch events without bloating the single controller and view. None of my other objects are UIResponders. I've not tested exactly this part yet.

With each app state the required objects for that state will be created, like corresponding Scene, TouchHandler, (GameLevelModel with game level logic when in GameLevelState), etc and deallocated when next state is entered.

My current issue with this one is, that when using SKTransition to transition between multile SCNScenes on the single SCNView, after 5-20 transitions there is memory leakage. I tried resolving by simplifying the scene, removing all actions, nodes before deallocating but nothing helps. The only way to avoid leakage is avoiding SKTransition and directly assigning scenes to the View's scene property scnView.scene = scnScene. So this will require me to animate my own transitions.

                         --------------------------
                         |   GameViewController   |
                         --------------------------
                           |  |   |             |
     ------------------    |  |   |             |      ------------------
     | GameLevelsData |-----  |   |             -------|     SCNView    |
     ------------------       |   |                    ------------------
     ------------------       |   |                            |
     |   PlayerData   |--------   |                            |
     ------------------           |                            |
                                  |                            |
                       ----------------                        |
                       | StateMachine |                        |
                       ----------------                        |
                         |                            ------------------
                         |-(*MenuState)---------      |  MenuSCNScene  |
                         |-(LevelState)     |  |      ------------------
                         |-(ConfigState)    |  |
                                            |  |
                     --------------------   |  |
                     | MenuTouchHandler |---|  |
                     --------------------      |
                      -----------------        |
                      |   MenuData    |---------
                      -----------------

              (When entering, each state creates objects of corresponding
              dedicated subclasses for its Model, TouchHandler and Scene)
                                            |  |              |
                    --------------------    |  |     -------------------
                    | LevelTouchHandler |---|  |     |  LevelSCNScene  |
                    --------------------       |     -------------------
                     ------------------        |
                     | GameLevelModel |---------
                     ------------------

Thanks in advance.

2
  • I would ask this question in gamedev.stackexchange.com
    – David Lee
    Commented Mar 29, 2017 at 0:06
  • Thanks David, I thought the same and first searched for answers and solutions there, but could find very little, then checked here and there is just much more stuff here on stackoverflow. For example a search "SceneKit" at GameDev results in 27 hits, while here I get 3400 :)
    – Sulevus
    Commented Mar 29, 2017 at 1:10

2 Answers 2

7

I will explain a real game design that I am actually working on and was based on the design of the sample codes provided by Apple for SceneKit.

As @HalMuelller correctly points out, you need to extract the game logic into a platform independent layer, and that is a good idea even if you are writing the game for one platform as that will help you in testing.

enter image description here

As you can see in the class diagram above:

On the right side is the entire game logic starting with a Game class which actually works as the scene and physics delegate. This is just a plain old ObjC or Swift class.

One of the main responsibility of the Game class is to build scenes SCNScene either from files supported by SceneKit/Model IO or programmatically as your case may be.

The Game class also refers to other Model classes such as Animation which loads 3D skeletal animations and Player class which contains the player data such as his statistics, the Player in here refers to some player like a baseball player in the game.

You can also see that there is a PlayerEntity class which is made up of BatterComponent, this is based off the Entity-Component design from GamePlayKit. Now the BatterComponent has a StateMachine which handles the batter's transitions between various states.

The other important component is BatterControlComponent which you add to the entity only when the user is batting. This is also a platform dependent component, so you can write this independently in your macOs/iOS targets. Again the batter control component is backed by a state machine as State machine can also be used for handling controls transitions. This control is implemented as a SpriteKit scene as an overlay on top of the SCNScene.

If you are pitching, then you configure the player entity with the PitcherComponent backed by another state machine and so on.


On the left side of the class diagram, you can see sits a GameViewController which sets up an SCNView and also instantiates a Game object and this game object's scene is what your game view controllers scene view renders. You also ensure that you set the scene view delegate to be the game object.


You must seriously refer to the Apple sample codes for SceneKit and the samples for GameKit. In Apple developer, Sample Code section search for: SceneKit and GameplayKit.


Overall you must design a platform independent game logic layer using plain old Swift/ObjC classes and the appropriate patterns from GamePlayKit based on your game requirements.


A note on production usage. See this memory leak issue and animation issues, so I am not really sure if SceneKit is yet ready for prime time, but Google is your friend. Having said that, SceneKit is really great if you are already working on Apple platform so the turn around time vis a vis learning a new game engine is lesser if you have not used any game engine and are writing games as a hobby or to learn game programming. I believe that SceneKit has been improving every year and in the next 2 releases (WWDC 17, 18) it should be a really polished game engine for Apple platforms. These are my 2$ based on my experience, YMMV.

4
  • Thanks for the reply. I edited (effectively rewritten) my question to make it clearer. The architecture I'm looking at is only the high level part for now. Looking to design it as a data-driven app in a top-down approach. Once I get this working, I'll be looking in detail of the game level model (renamed it to GameLevelModel on the diagrams) and take your tips in account there. I've looked into Apple's samples and entity-component approach, but I'll prob use only simple class hierarchy for game objects, it's only 2-3 classes, player and enemy space ships and collectables.
    – Sulevus
    Commented Mar 30, 2017 at 23:16
  • Are you planning to use SKTransition to transition between SCNScenes? Presumably driven by your GameViewController. I regularly get memory leakage after 5-20 scene transitions. However, I have to mention I use iOS 9 so that I can cover it in my release as well, these problems might be resolved in iOS 10 by now.
    – Sulevus
    Commented Mar 30, 2017 at 23:25
  • I have edited with notes on production usage, covering memory leaks and animation problems. Commented Mar 31, 2017 at 6:19
  • Marked this reply as Accepted as Dmsurti replied to the best of his knowledge, although some questions still remain unsolved, namely: 1) memory leakage when transitioning scenes on a single SCNView, 2) the Game object may become a bit too large, although this could be solved with composition.
    – Sulevus
    Commented Apr 5, 2017 at 17:14
0

I'm not quite following your terminology, especially the notion of "child MVC". The acronym MVC in iOS usually refers to the model-view-controller pattern, and in that context I don't know what a child MVC would be.

There's nothing wrong with multiple SCNViews, and it appears in option B that you're using them for different purposes. Remember that you don't have to do everything in an SCNView. You can still use UIView/NSView, or even an SKView. Using SKView for the menus and controls is a common approach.

Implementing your own touch handlers and transition animations is IMO overkill. Use what's there. That's what SceneKit is for.

I would like to see you break your game logic out of your view controller logic. Consider what will happen when you want to move the game over to tvOS or macOS. If the game logic is in the VCs, you'll have a lot of unwinding to do. Keeping them separate also avoids one of the other meanings of MVC, "Massive View Controller", a bloated mess trying to handle all tasks at once.

Many online tutorials and code samples put everything into one game/view controller class, and while that's fine for simple examples and toy problems, it's not a practice that lends itself to clean code, good maintenance, and unit testability. If my game model logic is in a separate class (perhaps owning the state machine), I can easily write unit tests for that piece.

1
  • Thanks, Hal. I edited (effectively rewritten) the question to make it clearer. By "child MVC" I meant an MVC that is nested in a container VC, as in parent-child or container-nested relationship. Apple often used "child VC" in their View Controller Programming Guide for iOS which I apparently remembered as "child MVC" as we effectively nest the entire MVC that a controller manages. With regard to the game logic, I will have that in GameLevelModel object (renamed from LevelData). I am looking at the top level architecture to make the app data-driven, still not gotten to a game level model.
    – Sulevus
    Commented Mar 30, 2017 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.