Skip to main content
GameDev.net gamedev.net
🔒 Locked

About MVC pattern and data sensors??

Started by Farraj Mar 8, 2008 at 11:55 PM 1 replies 1.7k views
Original Post
Farraj
Farraj
Hi, First Question: I’ve been recently reading up on some design patterns that have successfully used with game programming. One of these is the Model-View-Control design. Although I haven’t seen any code example for this pattern yet, I think I got the full idea and can put this pattern in my code. There was only one point that I couldn’t understand how it’s done. Everyone who explained said that, although the Controller has all the functionality to control a ‘Model’, It’s doesn’t do any controlling (doesn’t control the ‘Model’ directly). Instead, the ‘Model’ ‘queries’ (God I hate that word -_-) the controller for the changes. How does this work?? I can’t seem to visualize it. Second Question: What is the best way to check if a value has changed without using conditions or loops? For example, let say I have a Player Character class with a ‘m_health’ member. How can I design this so if the value of m_health changes, other parts of the game get informed of that (like the GUI health bar)? I know that sending an ‘Event’ with the changed member and value is enough to inform the rest of the game. But who or what sends that ‘Event’ in the first place? Thanks in advance
Pox
Pox
As to question number two: I'm guessing you're using a language like C++ and you're writing most of the game engine (not graphics, but the logic) yourself, otherwise this probably isn't applicable.

The simplest solution is to keep a copy of m_health (eg. m_health_old) that is assigned to at the start of each frame, then checked for a change at the end of each frame - however, this could potentially double your memory use.

You could make whatever modifies the m_health variable call the event right then and there, or set a flag that causes it to be called later.

However, is it really necessary? Unless you're really pressed for performance and only blitting certain parts of the screen, redrawing a health bar each frame whether it's changed or not should have virtually 0 effect overall.
Telastyn
Telastyn
Quote:

But who or what sends that ‘Event’ in the first place?


Whatever class owns the variable in question. That way it can properly ignore who sees the event, or what happens after it's triggered and the listener can ably ignore how the update occurs or from where.


//Psuedo-ish code.// Viewclass HealthBar{    void Update(int Life){         // stuff    }}// Modelclass SomethingWithHealth{    private int health;    public event OnHealthChange(SomethingWithHealth source, int from, int to);    public void SetHealth(int Life){        OnHealthChange(this,health,Life);        health=Life;    }}// GlueSomethingWithHealth Player;HealthBar           UIhealthBar;Player.OnHealthChange = { UIhealthBar.Update(to); }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.