Question about state machine

Started by
13 comments, last by ultramailman 11 years, 5 months ago

Hi ultramailman,

Sorry for the delayed response. Had a bit of a... "complication in life" last week. But if you still want my help the first code sample more resembles to your requirement than the second one. I'm not sure if your second option is fairly common in c programming but it seems it would look ugly scattered everywhere in your code. The second one more resembles an inheritance than a composition of MVC. Let me know if you need further clarification.


Hello Brent,

The second code segment is just a dirty way to access the other parts, no inheritence was intended.
Better example to replace the mess that was the previous example:

struct model{
struct controller * c
struct view * v;
};
struct controller{
struct model * m;
struct view * v;
};
struct view{
struct model * m;
struct controller * c;
};


Here, I can easily access the model from the view, or the controller from the model, etc. Should easy communication like this be allowed in MVC, or is it better design to not allow them know about each other at all? Thanks
Advertisement
Strictly speaking, no. You would ask yourself how do these systems communicate if none of them know of each other. The (hidden) solution for this to make this work is to actually build another layer that manages the communication between these systems. This is where the event system/manager comes in. The event system is pretty much just a messenger that sends messages to the particular addressee. Or sends the message to those who are only interested in that particular type of message. This way, your model, view and controller doesn't have to know about any of them. They just care about sending all their information to the event system and the event system takes care of sending these info to the other systems interested.

Searching "event system" in google would return three or four links about this subject.

You can check out my simple implementation of an event system here:
http://pulsetec.code...ew/15801#463060
http://pulsetec.code...ew/15801#463062

btw, a more appropriate name for this system is called a listener/dispatcher design pattern.

This is just my suggestion but it might be better for you to use components instead of trying to follow MVC for your game objects. I don't know about the others but from my experience MVC is more used/implemented in defining their high-level applications. Unity for example might use something similar to MVC for the editor and main game systems but their GameObject uses a component object model.

Strictly speaking, no. You would ask yourself how do these systems communicate if none of them know of each other. The (hidden) solution for this to make this work is to actually build another layer that manages the communication between these systems. This is where the event system/manager comes in. The event system is pretty much just a messenger that sends messages to the particular addressee. Or sends the message to those who are only interested in that particular type of message. This way, your model, view and controller doesn't have to know about any of them. They just care about sending all their information to the event system and the event system takes care of sending these info to the other systems interested.

Searching "event system" in google would return three or four links about this subject.

You can check out my simple implementation of an event system here:
http://pulsetec.code...ew/15801#463060
http://pulsetec.code...ew/15801#463062

btw, a more appropriate name for this system is called a listener/dispatcher design pattern.

This is just my suggestion but it might be better for you to use components instead of trying to follow MVC for your game objects. I don't know about the others but from my experience MVC is more used/implemented in defining their high-level applications. Unity for example might use something similar to MVC for the editor and main game systems but their GameObject uses a component object model.


Whew, that what a big project you have there! The names event and listener reminds me of Java, where ActionListener's (something like that) were installed into swing buttons. I assume thats the type of event system you mean?

Anyways, I am using SDL, do you think the SDL events are similar enough that I can cook up some wrapper functions around it to emulate the event system you are talking about?

As for whether to use MVC or component, I still haven't decided yet. I think It will be become clear to me which one to use later on, as I make the game. Maybe I will even end up not using any patterns or paradigms.
Right. A general rule of thumb is that only apply a particular design pattern where you see fit. Don't try or force a particular pattern to a problem that wasn't designed to fix that problem. Design patterns normally emerge after implementing the same behavior a hundred times. Then you realize and say: "hey... these part of code more or less do the same thing.I can generalize this part of code and make it like this to make it flexible." And a design pattern is born.

I'm not very much familiar with Java and SDL but i would assume it is so. Only the system is limited to specific actions defined by the system/application. I could be totally wrong though, try to check the documentation if SDL has a way of registering new custom event types.

If you have your own event system, it would be easier to create, attach, and dispatch built-in and game specific events. Though the learning curve may be a bit too steep for you at this moment, i encourage you to learn and try to build one your own nonetheless. You'll be amazed to learn and discover new techniques and tricks after that. Just always remember there's no such thing as a perfect solution/implementation. The first system you'll make may not be as efficient, powerful, and flexible as a commercial grade one, but the experience you'll gain is invaluable and will be used the next time you re-create the system or design and implement a relatively similar one. I rewrote my manager totally from scratch about 7 times i think. I rewrote my Virtual File system at least three times. And each version was about almost two thousand lines of code.
So far I've only used it for keyboard input handling, but I remember it also provides the ability to define custom events. Thanks for the advice! Very motivating.

This topic is closed to new replies.

Advertisement