Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
Foxkit

MVC Model questions

Recommended Posts

This is for the more advanced coders here (Mark, you even may be able to answer this). I've been looking at creating a framework for my game because sorting through 500+ lines of code is very quickly going to be a problem. Looking at the available frameworks and saw that the Model View Controller one was the most popular and might be a good idea. I need a couple of questions answered before I do so. I'll be creating a few pretty pictures to help explain my questions.

 

So first; What should a model be? This part doesn't seem very clear. Is it just the data? Is it an object that grabs the data? Does it also handle events? Does it pull commands from the controller and send them to the view?

 

If it's just supposed to be an object, then is the controller mostly the one telling the view what to do? Picture time.

 

Is this a good picture to see what's going on? I just seem to be either really close or missing this by a mile. Thanks in advance. -FoxkitMVCHELP_zps353a84df.png

Share this post


Link to post
Share on other sites

Ya, the site is written in this architecture. Though sometimes I chuck model stuff in my controllers, lolll

 

Give this a read: http://webdevrefinery.com/forums/topic/600-an-introduction-to-mvc-architecture/

 

If you don't understand it maybe i can try elaborate in a game context. What language are using for the game?

Share this post


Link to post
Share on other sites

I'm using python. I think I'm getting it. I was trying to get my model to pass stuff onto the view which is bad? Say the keyboard controls the sprite "main character." That controller should only send information to the "main character" class and maybe the "map" class. Where the model crunches the data and then sends it back to the controller. The controller then passes that along to the view to show changes on the screen. Am I getting it within the ball park?

Share this post


Link to post
Share on other sites

Nice, that's pretty good. I think you've got it. It's not an absolute prohibition, its really just separating out the different functionality. So your controller is supposed to pass stuff off to the model, but you may get lazy and just skip the controller and do it in the controller.

 

One main benefit is the ability to reuse your model functions. Nearly all of my classes are singletons and the functions are static so if I want to expand the web app that is RMU, I can chuck in a few lines and have a like system up, comments, deal with permissions, all super quickly across any different component I want to go and write.

 

Web is a wee bit different, definitely simpler. I'll explain how a page like a script works eg

http://www.gdunlimited.net/scripts/rpg-maker-vx-ace/misc-system/splash-screen-map

This is specific to this site but quite common. The "scripts" part of the URL triggers the scripts controller to run. My URLs are pretty cool, and I've got a special router system that allows me to exclude ID's, so its not so clear from this URL, but from my controller I'm calling a specific function. "rpg-maker-vx-ace", "misc-system" and "splash-screen-map" are variables that go into the function and are sent to the model.

 

In the model I convert these to numbers:

rpg-maker-vx-ace = engine_id

misc-system = category_id

splash-screen-map = string

 

And using these 3 variables, I use the model to query the database to bring back the information relating to the script.

 

So in the controller you'd be all

$script_data = ScriptsModel::getScript($engine_id,$cat_id,$seo_title);

And you'd come back with an array of data.

 

Then you simple pass this onto your view, where all the HTML is. The view ONLY contains html (even though you gotta plug your vars in, I use a templating system so technically...)

 

So in the view you have something like this

<h1>{$script_data["title"]}</h1>

And that's the end result. So you can see how each part plays its own role, and the controller is only like a traffic operator, receiving and sending variables.

Share this post


Link to post
Share on other sites

Awesome! That helps a TON. Now... to go weed out all that tera-bad stuff I was putting into my model.

>.>

 

Next question. I should be able to create a controller that takes what the model spits back, and direct that to the view couldn't I? Kind of like creating and event manager?

Edited by Foxkit

Share this post


Link to post
Share on other sites

Yep, that's essentially what's happening in my example and what I meant by traffic operator. You don't want want to be passing stuff from Model to View because obviously you can't reuse those model functions for something that doesn't involve that view. So use the controller to get stuff from the model, then pass that to the view from the controller.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...