I’m currently making a C++ game.
I have my main loop during which the logic is computed then the sprites etc are drawn
I would like to implement “dialog windows” : when you press a touch in front of a NPC, a dialog window pops up in the bottom of the screen and you are frozen until you press a key, BUT the game continues to run (the other characters are moving, etc) so the main loop is still runing.
I managed to do it just fine: when an object is activated, it sends a message to a dialog manager that display the text window while the game keeps running, so it looks like :
object::OnActivated() {
GameManager::doWindow("some text");
//the game is not blocked here, the game continues to run normally and will display a window on the next frame
}
Now the problem comes when I want some association action to happen after the dialog is over, or for instance if you selected “yes” to a question. I’d like to have an implementation that’d work like this:
object::OnActivated() {
GameManager::doWindow("some text");
if(GameManager::Accepted()) addGold(100);
}
The problem is that this check & actions will be performed as soon as the window event is created, NOT when the window is closed / acepted. Is there any way to do this, while keeping the associated action in the OnActivated() function? I have no idea how to do this properly without using function pointers, which would force me to have a certain signature for every method I could use as a result.
thanks
edit: I posted a bounty because I’d like to know what is the most “canonical” answer to this problem. I guess this is a very common issue (for a lot of applications and every modern games),
and I’d like to have a solution that is as flexible as possible, since I cannot list as of today all of the possible “consequences” that a dialog could trigger.
More info:
– each dialog would be triggered by objects deriving from a common “Entity” class
– different objects from the same class would almost always have different dialog/actions associated to them (for instance, all of the NPCs objects would not have the same dialogs)
– I don’t care about moving the “dialog logic” out of the OnActivated method or even outside of the Entity class. This would happen anyway because I’d like to have the ability to add “random” dialogs scenarios for each NPC, so the dialogs etc would be stored elsewhere
– BUT I’d like to keep the dialog logic itself as close as possible for a single dialog. Ideally I’d like to be able to do something like : ” result = dialogWindow(“question?”); if (result) { … }”. I’m not sure it’s possible though
The Command Pattern is used to encapsulate code to be executed at a later time.
In your situation, the
object::OnActivated()function would create an appropriate command object and store it to be found later. When Yes/No is selected by the user, the command may be run without the code needing to know what particular command object happens to be there.Here’s an example of an “add gold” command object:
Now, given some storage for an upcoming command:
You could have your
OnActivated()create the command:And when the user finally makes a choice: