Sign in to follow this  
Tanzan

Optimization info about 'world' in MVC design

Recommended Posts

Hello all,

My question is a bit hard to describe but hopefully it will do...

I just wonder what you guys think is the 'best' way of getting info about the model in your view(s)..

To clearify (i hope ;-) )

If the model is updating itself every game-cycle and the  (deep) nested objects all do there jobs how do you get info where only the view is interested in?

So my question is not how to do it but more what do you people think is the best way to do it ?

 

Regards,

 

Alex   

Share this post


Link to post
Share on other sites

Firstly, if you have a complex nested set of models, you should consider whether each of those objects should have its own view, instead of trying to have one view represent a lot of different things.

Secondly, it's usually simple to have a model tell a view when something has changed, and then the view knows to refresh data from the model. Does that not work for you?

Thirdly, the MVC approach is not very common in games, because it's not always a great fit. MVC was originally designed for event-driven user interfaces, and it works well for that system, where the model changes in response to user input from the controllers. It doesn't work so well for games and simulations where the model changes independently of user input and therefore the view needs updating often - typically on a fixed rate (e.g. 60 times a second) regardless of what has changed or what input has occurred.

Share this post


Link to post
Share on other sites
52 minutes ago, Kylotan said:

Hi Kylotan,

First of all thx for your fast and senseful reply ;-)

"Firstly, if you have a complex nested set of models, you should consider whether each of those objects should have its own view, instead of trying to have one view represent a lot of different things. "

That's an interesting approach....

"Secondly, it's usually simple to have a model tell a view when something has changed, and then the view knows to refresh data from the model. Does that not work for you? "

I guess you are reffering to the Observer pattern which i know would probably work, but i just would like to know whats the best way (how do you do it) to do it in design respective OO way...

In my situation it is not a 'solid' MVC design but its for an project on android..where the view (surfaceview) instantiate the world which updates itself every cycle and tells the  'important' stuff through states (i.e. collision,game over etc)  But let's say there is a car amazingly nested all the way to a bolt(s) object(s)  and you want to tell something in your view about that specific bolt (lets say it becomes loose).

Would you 'ask' (method) the world->->->->n (bolt) ?

 

 

 

 

 

Share this post


Link to post
Share on other sites

If the model changes infrequently, then the observer approach is the standard way to tell the view that it needs to redraw. In most games, the view is redrawing constantly, so there's no need for any notification, just do it every frame. I don't know how Android wants to work at the level you're working at, so I can't advise beyond that, unfortunately.

If I had a game where there were visible objects all the way down to a car's bolt, then that bolt would have its own visual representation, i.e. its own view. This might be a 3D model, a 2D sprite, or similar.

Share this post


Link to post
Share on other sites
19 minutes ago, Kylotan said:

If I had a game where there were visible objects all the way down to a car's bolt, then that bolt would have its own visual representation, i.e. its own view. This might be a 3D model, a 2D sprite, or similar.

And if its a non-visual ? (container-n class) 

Share this post


Link to post
Share on other sites
41 minutes ago, Tanzan said:

In my situation it is not a 'solid' MVC design but its for an project on android..where the view (surfaceview) instantiate the world which updates itself every cycle and tells the  'important' stuff through states (i.e. collision,game over etc)  But let's say there is a car amazingly nested all the way to a bolt(s) object(s)  and you want to tell something in your view about that specific bolt (lets say it becomes loose).

This is strange, because the view is passive in all variants of model presentation patterns. It shows what is there, but it does not create something. A game world is created by the level loading system. The view is just the output media of the rendering system, not more. Moreover, the renderer system just renders the view as a presentation of the current state of the world, so the renderer and especially the view need not be notified about world state changes. The only necessary thing (dependent on the OS) may be to set the view to dirty, so causing a redraw on the next opportunity.

23 minutes ago, Tanzan said:

And if its a non-visual ? (container-n class) 

I don't understand. Could you perhaps describe your problem by an example?

Share this post


Link to post
Share on other sites

Hello Haegarr,

 

Thx for your reply as well ;)

On android the 'view' is not so passive i think (like i said its not really mvc but...)

The surfaceview gets the update tick from the OS...so it creates the world (model) which creates all the other stuff then updates itself every cycle ...the surfaceview asks about all visual objects and draw them on the screen.

main  game state is guarded by return enumeration from world.update (levelover,gameover)

But what if the level had waves which have rounds which have parts etc etc...and you want to display the end and beginning of the nested stuff? (level 10->wave8->round14->part1 ) i know its a stupid example but just to make the point ;) would you ask every cycle if they are changed or make a message queue in world or like Kylotan suggests do it with subscribing through observer pattern

 

( i might do it wrong thats why i ask what's the best practice, i just want to learn from you guys)

Share this post


Link to post
Share on other sites

"if the level had waves which have rounds which have parts"... none of this is relevant to the view. Nor is anything about "return enumerations".

The view needs to know what to draw. That's it. In a game, the things to draw tend to fall into 2 clear categories:

  • concrete things with a dedicated visual representation. That visual representation might be a 3D model, a 2D sprite, or similar. You set up the view for those objects when the object is created. That object usually has a way of getting itself onto your screen.
  • abstract things that need to be displayed some other way, such as a current score or some floating text. Usually you work around this by changing the problem into the first problem and either creating a concrete representation (e.g. a TextBox that references the data).

The model needs to expose whatever methods are necessary for the view to get the information it needs. There's no super trick here - you just need to edit your model to make this possible. This is much easier if each entity has its own View object and its own Model object, because the View can reference the Model in each case, without needing to dig into hierarchies or whatever. But if you really want 1 View to be able to display a whole hierarchy of things in a Model, that is fine - you just need to make sure that the Model exposes methods to return that data. What data is that? No idea, it's your game.

Talk of message queues and ticks are also missing the point. Your options are simply these:

  • If the model is changing all or most of the time, the view needs to reflect that, so it should poll for updates frequently.
  • If the model is changing infrequently, it can notify the view of a change via an observer.

That's it. You don't need a message queue. The view just reads the data from the model when it needs it. If you find yourself doing too many updates because a small part of the model changes frequently, you probably need to break down your model into smaller parts.

Share this post


Link to post
Share on other sites
47 minutes ago, Tanzan said:

On android the 'view' is not so passive i think (like i said its not really mvc but...)

As Kylotan already mentioned, a game usually runs in another way than an application does. The surfaceview is a vehicle that the operating system is enforcing you to use. Nevertheless you can just use it as the driver to tick your own game loop. Such a game loop is usually build as a defined sequence of systems / modules / services / how ever you name it. All stuff that belongs to world updating (e.g. player input, animation, physics, collision resolution) is done early in the sequence. The graphical rendering is done at the very end of the sequence. Because the belonging modules are already updated, the world state is fix at the moment of rendering. Due to this, the view (understood as the presentation of what the renderer renders) is passive.

40 minutes ago, Tanzan said:

Thx Kylotan, like i said just tried to find the magic OO trick ;-) to poll constantly feels inefficient (allthough it's how i do it now)

If rendering is done all the time anyway, stuff like the level name, wave number, etc are just data values within the world. The renderer considers them simply by - well - rendering some text of other visual representation in its normal update phase within the game loop. It is not exactly polling. It is more like an immediate mode user interface stuff. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this