Architectural question - how to wire an ENTER_FRAME flow?

archont's Avatar

archont

30 Aug, 2013 03:39 PM

PROPER POST, mods please delete all previous ones

Hello,

I've never had much success with RL, the framework always got in the way more than it helped and custom, simple design patterns turned out to be more practical in all but the most complex projects - but I figure that's because I used it wrong, and often found myself doing hackish solutions and injections that you could technically do, but defeated the purpose of MVC in the first place.

So I'm giving it another try with RL 2 (and I love some of the changes!) - I'm stuck trying to find a fast, efficient and architecturally sound way to solve this scenario.

Basically, it's a video with overlaying elements on top, their position controlled from XML and updated every frame to match the movie currentframe.

Commands are expensive, Events are sort of expensive, and the event triggering so much code execution is a performance problem. How would I solve this so that it's still elegant but not quite as resource-intensive?

BTW, flowcharts are awesome, so I sketched one up, it's attached here. Is there any plugin or parser that could help me visualize the relationship between various MVC elements? I'm using the global search function more than I'd like and I would love to get a bigger picture of the whole thing without having to manually recreate the whole thing in a flowchart drawing tool?

Also great job on RL2, I'm still getting my feet wet but it just.. feels better than RL1.

Flowchart

  1. 1 Posted by archont on 30 Aug, 2013 03:39 PM

    archont's Avatar

    PROPER POST, mods please delete all previous ones

    Hello,

    I've never had much success with RL, the framework always got in the way more than it helped and custom, simple design patterns turned out to be more practical in all but the most complex projects - but I figure that's because I used it wrong, and often found myself doing hackish solutions and injections that you could technically do, but defeated the purpose of MVC in the first place.

    So I'm giving it another try with RL 2 (and I love some of the changes!) - I'm stuck trying to find a fast, efficient and architecturally sound way to solve this scenario.

    Basically, it's a video with overlaying elements on top, their position controlled from XML and updated every frame to match the movie currentframe.

    Commands are expensive, Events are sort of expensive, and the event triggering so much code execution is a performance problem. How would I solve this so that it's still elegant but not quite as resource-intensive?

    BTW, flowcharts are awesome, so I sketched one up, it's attached here. Is there any plugin or parser that could help me visualize the relationship between various MVC elements? I'm using the global search function more than I'd like and I would love to get a bigger picture of the whole thing without having to manually recreate the whole thing in a flowchart drawing tool?

    Also great job on RL2, I'm still getting my feet wet but it just.. feels better than RL1.

    Flowchart

  2. Support Staff 2 Posted by Shaun Smith on 30 Aug, 2013 07:00 PM

    Shaun Smith's Avatar

    Hmm. there's some weirdness going on with Tender today - this post keeps ending up in the Spam queue.

    Anyhow.. I would definitely recommend against pushing performance critical stuff through the command map (or any similar abstraction).

    Decoupling is a great principle, but use it where it makes sense, and where it provides benefit. Bootstrapping, handing user triggered actions and application state changes, things that occur relatively infrequently etc.

    But a "gameloop", or any sequence of code that is executed on every frame, should be kept as tight as possible. Avoid creating throwaway objects (events for example) as this puts strain on the garbage collector. Avoid loops. Avoid deep call stacks. Heck, avoid method calls if you can!

    So why use RL at all then? Because many things benefit from decoupling; from being given readable, well defined roles within a system. RL and automated DI often make code easier to read and maintain (when used correctly). But things tend to fall apart when developers try to force every single aspect of their system into some imagined MVC structure. An unreasonably decoupled system can be a nightmare to read and understand.

    In your case I might use RL to bootstrap the app, handle the loading and parsing of data, sign users in/out, and wire things together. But the subsystem (module, service, whatever we might call it) that handles frame events and creates, positions and destroys the overlays would be tight and fast.

    Hope that helps a bit!

  3. 3 Posted by archont on 30 Aug, 2013 07:49 PM

    archont's Avatar

    Hey Shaun, thanks for your reply, I never really thought about blending MVC with non-MVC code. I always thought of MVC as more of an all-or-nothing pattern.

    The "gameloop" is a critical, but very tiny part of the application. It's short, simple and linear, with no branching. As it's pretty simple and there's just one of it, an extra function call or two won't really harm performance, but a few dozen calls, few new Events and a Command does.

    While it would be possible to flatten the 2 models and 1 mediator into a single Manager blob, they all do various non-time-critical stuff that greatly benefits from decoupling and MVC. I would want the gameloop handler to be as small and single-purpose as possible.

    What I'm thinking is leaving the non-time-critical functionality in their respective models, then creating an optimized single-purpose singleton Manager(Sprite) that actually stores the data, manipulates itself and listens to ENTER_FRAME on it's own - while being exposed to MVC via a thin Model with tight bindings to the manager.

    This would mean that this new thin Model is a tightly-coupled MVC interface for a singleton manager - so in effect it's kind of like a Mediator and Model rolled into one. Time-critical stuff gets handled inside the manager, while the Manager is fed data via the Model.

    Another option is to have the singleton manager exposed via a Mediator instead - on one hand the overlay has multiple inputs (currentFrame, parsed XML data, responds to stage resize events) and just one output (clicked), so in that respect it ultimately acts like a view. On the other hand Views and their Mediators are supposed to be stateless, disposable elements that rely on a Model for data.

    I'm, still learning to "think in Robotlegs" so I want to know if such an approach would be a sane compromise or a total misinterpretation of MVC principles.

  4. Support Staff 4 Posted by Shaun Smith on 30 Aug, 2013 09:43 PM

    Shaun Smith's Avatar

    Hey, no problemo. So I'm not necessarily suggesting "flattening" a number of classes into a god object, my comments were more to do with message passing, how collaborators are wired together, and how deep the callstack gets inside the gameloop - you should still avoid writing classes that do more than one thing (which is made most obvious when you need to use the word "and" when describing what a class does!).

    It sounds like you have a couple of approaches in mind. Really, you just need to try them out and see what feels the best. Personally, I'd avoid the word "manager" :)

  5. Ondina D.F. closed this discussion on 23 Sep, 2013 02:37 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac