Can I use events in C++ like in C#?

Started by
13 comments, last by wqking 12 years, 1 month ago
I have a game object manager class that manages a list of all the game objects. I also have a physics manager class that stores the collision geometry for each object. When the physics manager does collision detection, I'd like for it to raise an event to the main application class with the ID's of the two objects colliding, so I can get them from the game object manager and call their CollisionResponse functions. In C# I know how I would do this, but I'm using C++ and having some trouble. Is this even possible, or am I taking the wrong approach?
Advertisement
There's no built-in construct for that in C++, you'll have to implement it yourself using a list of callbacks.
"A list of callbacks" is also called "a signal", and there are several signal libraries for C++ around. I've used Boost.Signals before and it's nice and easy, although I've heard criticisms about its performance (which didn't matter much in the situations where I've used them).
you should check out the Observer pattern: http://www.google.co.in/search?sourceid=chrome&ie=UTF-8&q=observer+pattern

a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)


"A list of callbacks" is also called "a signal", and there are several signal libraries for C++ around. I've used Boost.Signals before and it's nice and easy, although I've heard criticisms about its performance (which didn't matter much in the situations where I've used them).


Thanks, I downloaded boost and got some wonky code working with signals. I'm sure I did it wrong because it won't let me put break points in the method it calls, but apparently it is running the code! So good enough for now at least.
You are likely trying to debug in release mode. Which will often inline functions where possible.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


You are likely trying to debug in release mode. Which will often inline functions where possible.


Whoops, you're right I am in release mode. I switched to debug and I get an access violation in one of the boost header files when it tries to connect the signal though.

Whoops, you're right I am in release mode. I switched to debug and I get an access violation in one of the boost header files when it tries to connect the signal though.


Check the call stack and inspect the variables in your program -- make sure you're hooking everything up correctly.

[quote name='noodlyappendage' timestamp='1331615266' post='4921576']
Whoops, you're right I am in release mode. I switched to debug and I get an access violation in one of the boost header files when it tries to connect the signal though.


Check the call stack and inspect the variables in your program -- make sure you're hooking everything up correctly.
[/quote]

Hmm. It even happens in the basic Hello World example from the boost tutorial. I wonder if maybe it's some setting in the IDE? I'll keep investigating...

Edit: Looks like it is this http://stackoverflow.com/questions/137089/boostsignal-memory-access-error
If you're using Visual Studio 2010 or newer (or a compiler that supports C++0x);


bool CGame::Create( /* */ )
{
tr1::function<void (CGame*)> CGameOnLoading = &CGame::OnLoading;
m_nState = EGS_WORLD;//EGS_LOADING;
CGameOnLoading( this );

return true;
}


Note that the tr1 namespace is inside the std namespace.

This topic is closed to new replies.

Advertisement