Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 noodlyappendage   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 March 2012 - 05:30 PM

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?

Sponsor:

#2 DvDmanDT   GDNet+   -  Reputation: 934

Like
0Likes
Like

Posted 12 March 2012 - 07:56 PM

There's no built-in construct for that in C++, you'll have to implement it yourself using a list of callbacks.

#3 Álvaro   Crossbones+   -  Reputation: 13649

Like
2Likes
Like

Posted 12 March 2012 - 08:47 PM

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

#4 bollµ   Members   -  Reputation: 354

Like
0Likes
Like

Posted 12 March 2012 - 10:34 PM

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


#5 noodlyappendage   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 March 2012 - 10:53 PM

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

#6 Washu   Senior Moderators   -  Reputation: 5359

Like
2Likes
Like

Posted 12 March 2012 - 10:55 PM

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.
ScapeCode - Blog | SlimDX


#7 noodlyappendage   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 March 2012 - 11:07 PM

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.

#8 fastcall22   Crossbones+   -  Reputation: 4375

Like
0Likes
Like

Posted 12 March 2012 - 11:11 PM

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.
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#9 noodlyappendage   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 March 2012 - 11:20 PM


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.


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

#10 MarlboroKing   Members   -  Reputation: 192

Like
0Likes
Like

Posted 13 March 2012 - 01:34 PM

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.

#11 Washu   Senior Moderators   -  Reputation: 5359

Like
3Likes
Like

Posted 13 March 2012 - 03:27 PM

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.

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.
ScapeCode - Blog | SlimDX


#12 MarlboroKing   Members   -  Reputation: 192

Like
0Likes
Like

Posted 14 March 2012 - 01:44 AM

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.

#13 MaulingMonkey   Members   -  Reputation: 1556

Like
0Likes
Like

Posted 15 March 2012 - 02:02 AM

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

#14 Endurion   Crossbones+   -  Reputation: 3646

Like
0Likes
Like

Posted 15 March 2012 - 11:55 PM

If you want to use something that works without a hassle, try fastdelegate http://www.codeproje...astest-Possible It's only a header to include.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#15 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 16 March 2012 - 12:20 AM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS