• Advertisement
Sign in to follow this  

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

This topic is 2167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
"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).

Share this post


Link to post
Share on other sites

"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.

Share this post


Link to post
Share on other sites
You are likely trying to debug in release mode. Which will often inline functions where possible.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

[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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.


TR1 is not necessary. VS2010 has std::function support as part of its C++11 support. Its also important to note that std::function is not the same as boost signals and slots, which allows for registration and deregistration of multiple listeners.

Share this post


Link to post
Share on other sites

TR1 is not necessary. VS2010 has std::function support as part of its C++11 support. Its also important to note that std::function is not the same as boost signals and slots, which allows for registration and deregistration of multiple listeners.


I could of sworn MSDN said it was apart of TR1, but I may be mistaken since I read it up around 2/3ish AM. +1 :)
As much as I dislike using libraries, I may try giving Boost a shot, your last sentence really has got me interested.

Share this post


Link to post
Share on other sites

I could of sworn MSDN said it was apart of TR1

TR1, or "Technical Review 1", is part of the work leading up to (and predating) C++11. What Washu is getting at is that the technical reviews are not part of the C++ standard itself, although what they contained was effectively rolled into the core C++ standard in C++11 (with various errata to e.g. take advantage of new language-level features, and little things like putting them in the root std namespace, etc).

Share this post


Link to post
Share on other sites
Beside Boost::Signal and Boost::Signal2, there is also libsigc++ you may want to try.
Just a reminder if you use callbacks heavily You'd better benchmark, or investigate, between Boost and libsigc++ to see the performance.
As far as I know and some google result, libsigc++ has better performance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement