dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Tying game MVC together with AS3

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Posts
    17

    Tying game MVC together with AS3

    Hi

    I'm building the framework of a game at the moment and am using the MVC approach. The State DP is integrated into this structure to define gamestates. Also, there are a numerous singletons in the class structure including 2 discrete data models, the logger and the console.

    While broadcasting events out from these objects enables loose coupling, the array of controllers (states) need direct access to the data models. For MVC, this is typical.

    So, my problem is getting a data model reference out to the various states that perform the game logic. My problem is essentially twofold:

    1. I want to maintain loose coupling to keep my codebase extensible.
    2. I want to use the singleton pattern for singleton objects, so that there is a single, accessible point of access to the model for all objects (such as states) to reference each singleton. (I am not worried about enforcing single instantiation, only about referencing a single instance.)
    3. I want to give my controller objects an implicit way of referencing the data models (preferably in keeping with point 1), eg. I want to avoid passing arguments, using globals, etc.


    When the model is created, I can do one of the following to get it's reference to the individual states:

    --Create the singleton objects and have any states call a static getInstance method of the Singleton superclass -- except static functions aren't inherited. This is my preferred route but I can't get it working, see here:
    http://www.kirupa.com/forum/showthre...223798&page=35
    (In spite of my response there, I later realised I was no better off with the given "solution".)

    --Create semi-singleton objects (no instantiation enforcement, but still have non-static getInstance() methods referencing a static instance variable). I like this option, but can anyone clarify why Singleton's need a static getInstance() method? Am I safe in dropping it?

    --Every time a new state is called, pass the data model ref into the new state, from either the old state or the StateManager (which holds a permanent reference to it), as a parameter. I don't like this: it just seems ridiculous to be passing params when in reality every state should, by it's very nature, be able to access the model. In fact, the model(s) should be able to be accessed by pretty much any object in my program that wishes to access it.

    --Don't enforce singleton behaviour, instead store the ref in global variable. I smell rubbish...!


    Long post, I know. Any ideas?

    -Nick

  2. #2
    Junior Member
    Join Date
    Jan 2009
    Posts
    2
    Heya,

    To address point #2, singletons have a static getInstance method because instantiating the singleton object would already give you an instance - so instead of:

    var x : Singleton = Singleton.getInstance();

    you would be doing:

    var x : Singleton = new Singleton();
    so x.getInstance() is a bit pointless as you already have an instance. Using the static method means that you can ensure only one instance provided only the static method is used to instantiate. I think in Java you could modify the constructor(s) to throw an InstantiationException to prevent the singleton from being directly instantiated, in as3 you can have a class variable for instance and check if it's not null in the constructor.

    I would recommend you start by having a look here:

    http://browse.puremvc.org/wsvn/wsvn/...emvc_as3_core_

    PureMVC will probably provide you with the basic framework anyway, although if you want to roll your own then the above repository contains all the AS3 code. It's well documented. They use singletons for the controller and model. Hope that's a bit helpful!

    Cheers,
    Tavis

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Posts
    17
    Tavis,

    Thanks for the reply... though that post was pretty old and I've since resolved my understanding of the overall pattern (specifics are always a different story).

    I'm sorry now that I didn't mention in this thread, as I have in MVC threads I've started on other boards: "Please don't bother to bring up PureMVC".

    I don't need MVC done for me, I need to learn how to build MVC applications from the ground up. I don't know about others but honestly, staring at someone else's enormous codebase for hours on end (eg. PureMVC's source) is not going to help me to understand the architectural aspects at hand. Reasons why I need to learn it for myself:

    1. I write games in my free time with a view to moving into that industry full time.

    2. I'm building a portfolio of game code, meaning that except for a few incidental classes, most of the structure / skeleton of the game needs to be code written by my own hand.

    3. One can't optimise a framework that one didn't write without understanding it as well as (or better than) those who wrote it.

    For reasons of optimisation, game structures very often do need to be (re)written from the ground up. This is becoming less true in compiled languages like C++ and Java where gleaning speed is at less of a premium due to the nature of the language (so as a result middleware is being used more and more frequently), however since AS is a slow, interpreted language we need to code games in the way that C/C++ programmers had to back in the 80's and 90's.

    Games usually do require heavy optimisation. This game contains an impressive amount of logic for a Flash application, and that's only the beginning. For this reason, I've considered splitting it (at a later date) into client-server and porting the core game logic to Java, while retaining the UI view-controller and a synchronised "mini-model" on the Flash client side.

    *Stomps another "Heard of PureMVC"? thread*
    Last edited by NickZA; 01-04-2009 at 01:51 PM.

  4. #4
    Junior Member
    Join Date
    Jan 2009
    Posts
    2
    Ah, my bad, I came across your post on google and am not actually a regular frequenter of these forums

    I've only just gotten into flash based game development as well, I do java/javame for a living and it's not game related. For my first flash game project I'm not using MVC at all actually, although it's going to be quite a simple game so I may consider it for later projects.

    In terms of putting game logic on the server, what are you intending to separate out? I believe eve online used stackless python for the server component (which I've had a play with, it's likely to be good if you have a similar scenario with potentially lots of players and autonomous AI entities). I suppose the only drawback with that model is how to synchronize it all, a cursory google found some links in
    this answer at stackoverflow.

    I also agree with the doing-things-yourself approach, in my case because otherwise I won't understand properly. Sorry it wasn't that helpful!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center