Jump to content
  • Advertisement
Sign in to follow this  
noodlyappendage

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

This topic is 2350 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
There's no built-in construct for that in C++, you'll have to implement it yourself using a list of callbacks.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!