on 01-04-2012 03:38 PM
Hey guys, I thought I'd post a question here since there seems to be some knowledgeable and experienced programmers here.
I'm trying to use the MVC(Model-View-Controller) design pattern for an engine. I had a quick question for the model portion of the code. Can my model class include an update function or is that strictly handled by the controller?
Thanks!
01-04-2012 04:21 PM - edited 01-04-2012 04:31 PM
I have a little experience with the MVVM pattern which is very similar to MVC and I would think people would prefer update code in the C or VM. The model wouldn't have a sense of time or interactions with the room, other actors, etc so updating would be limited.
I've been toying with this idea too but my impression is that implementing it would entail forgoing use of GameEngine2d completely. I'm waiting to see if later versions of PSS included collision detection as part of GE2d before I make up my mind.
Edit: Here's a series of articles about Coco2d and MVC.
on 01-04-2012 05:03 PM
Thanks for the quick reply! So this what I have so far. I've defined my model, view, and controller classes. My model class contains all the raw data the game needs, my views reads from the model in order to get whatever information it needs to display, and my controller controls the updates and render calls to all the views as well as send events to the model to change states.
However, I also have an update method inside my model class which the controller also calls within its own update method. So according to what you said, it should be best if the model didn't have to worry about updating the game world and is just a container for world data correct?
on 01-04-2012 07:25 PM
As I see it, the model would not have an update method. The controller or viewmodel would have the update method.
Keep in mind that my experience is with MVVM and not MVC. The components in MVVM have slightly different resposibilities than in MVC.
But here is what I think PSS would look like in psuedocode as MVVM. As I imagine it, the view would actually be a class that derives from some Node class, such as SpriteUV. If it worked like this then I could understand using the Director more.
public class BadGuyView : SpriteUV
{
public BadGuyView(BadGuyViewModel vm)
{
OnCollision += HandleCollision;
BindingSource = vm;
Position = BindTo(BindingSource.Position);
}
public override void HandleCollision(Object sender, CollisionEventArgs e)
{
Message.Send(this, MsgToken.CollisionDetected, sender, e);
}
}
public class BadGuyViewModel : ViewModelBase
{
public Game _Game;
public BadGuyModel _Model;
public BadGuyView _View;
public BadGuyViewModel(Game game)
{
_Game = game;
_Model = new BadGuyModel();
_View = new BadGuyModel(this);
_Game.Director.AddToScene(_View);
//Message.Listen(_View, MsgToken.GamePadInput, OnGamePadInputOccured);
Message.Listen(_View, MsgToken.CollisionDetected, OnCollisionOccured);
}
//Called every frame by Game.Director
public override void Update(Int32 ticksSinceLastUpdate)
{
Position += 20 * ticksSinceLastUpdate;
}
public void OnCollisionOccured(MessageArgs args)
{
Position = CalcNewPosition(args.ObjectHit, args.Angle);
}
public Vector2 Position
{
{ get { return _Model.Position; }
{ set { _Model.Position = value; }
}
}
public class BadGuyModel : ModelBase
{
public Vector2 Position;
}
on 01-04-2012 08:31 PM
I'm curious how you plan to use MVC in PSS. Are you doing a GameEngine2d game or low level 2d or 3d?
on 01-04-2012 08:38 PM
PS- I was reading up on MVC and it seems like your idea of an Update method in the model is sound. MVVM moves all of that type of code from the model into the viewmodel.
01-04-2012 10:52 PM - edited 01-04-2012 10:54 PM
Hey there..sorry for the late reply! I just got back home. But to answer your question, I'm not doing MVC with PSS but rather taking a take home test that I need to do in order to move forward in the job interview process.
I think I have everything set up correctly where my views only display game data, my controller has handles to all the active views and calls the various update and render calls for each view. The controller also handles all events from the views and passes it onto the model. My model contains all the raw data that the game would need to function. I was just curious if it would be okay to have an update method within the model and just make sure the controller was the one calling it rather than the model handling its own update itself.
I just wanted to make sure this was okay before I submitted my code samples to the company I'm applying for. Right now, I'm doing everything in Actionscript 3. But looking at it some more, the MVC pattern addresses many dependency issues I had in the past. I haven't looked into how I can utilize this with PSS but now that I know a little more, it could be worth investigating.
Thanks again for the help and the time you spent replying! ![]()
on 01-04-2012 11:02 PM
Thanks again for that link! I read the second part of his article and he did state that the model has its own update method. So with that, I aim to wrap things up! Many thanks again for the help and the info! Good stuff. ![]()
PSM Developer Registration (for free) on PSM DevPortal is required to post on the forum.
Please sign out then sign in again to the forum and PSM DevPortal after you have completed the registration.
フォーラムへ投稿をするにはPSM DevPortalへの登録(無料)が必要です。
登録後はフォーラムと
PSM DevPortalを一度ログアウトし、再度ログインしてください。


Website ©2013 Sony Computer Entertainment Europe
All content, game titles, trade names and/or trade dress, trademarks, artwork and associated imagery are trademarks and/or copyright material of their respective owners. All rights reserved. [more info]
%%http://community.eu.playstation.com/t5/Announcements/Beta-Trial-Information/td-p/11386362
best_shooter.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_driver.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_performer.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_footballer.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_fighter.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_creator.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_action_player.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
dev2.png%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/Producer-and-Developer-Ranks/td-p/18407352
trophy.gif%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/The-Community-Awards-FAQ/td-p/18407096
PSlogoSM.png%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/Online-Support-Coordinator-rank/td-p/18414870