University of Southern CaliforniaUSC
USC ICT TwitterUSC ICT FacebookUSC ICT YouTube

EventHandlerExample | General SmartBody Discussion | Forum

Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
EventHandlerExample
October 6, 2014
3:12 pm
Avatar
Member
Members
Forum Posts: 20
Member Since:
September 11, 2014
sp_UserOfflineSmall Offline

Hello,

Where can i find a solid example of how to write an EventHandler class with C++ ?

thanks!
metalos

October 6, 2014
11:20 pm
Avatar
Admin
Forum Posts: 983
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

You can create the event like this:

 

class MyEventHandler(SBEventHandler):
   def executeAction(self, event):
      str = event.getParameters()
      print "Now executing event with parameters " + str

You can do anything you want with the parameter string.

You then load the event handler by name into the system:

 

myHandler = MyEventHandler()
eventManager = scene.getEventManager()
eventManager .addEventHandler("myevents", myHandler)

 

then you can trigger an event like this:

eventManager = scene.getEventManager()
event = eventManager.createEvent("myevents", "here are the parameters")

eventManager.handleEvent(event, 0)

October 7, 2014
4:23 pm
Avatar
Member
Members
Forum Posts: 20
Member Since:
September 11, 2014
sp_UserOfflineSmall Offline

Thanks Ari.
Works

December 27, 2014
6:26 am
Avatar
New Member
Members
Forum Posts: 1
Member Since:
December 26, 2014
sp_UserOfflineSmall Offline

Instead of having static methods and passing around a pointer to the class instance, you could use functionality in the new C++11 standard: std::function and std::bind
#include <functional>
class EventHandler
{
    public:
        void addHandler(std::function<void(int)> callback)
        {
            cout << "Handler added..." << endl;
            // Let's pretend an event just occured
            callback(1);
        }
};

The addHandler method now accepts a std::function argument, and this "function object" have no return value and takes an integer as argument.

December 27, 2014
10:37 pm
Avatar
Admin
Forum Posts: 983
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

Thanks, I wasn't familiar with that functionality. I'll keep it in mind when I commit to that standard.

 

Ari

Forum Timezone: America/Los_Angeles

Most Users Ever Online: 733

Currently Online: mcclurelloyd91, Andreytielo, kvistaagesen6, shorebagger6, campbennedsen38, carrollpratt5, fosskrog0, magnussonmagnusson3, zhuwarner26, dreyer26guerrero, miscreantunderc69, BernardFaulkner25
72 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

jwwalker: 80

jyambao: 52

rbaral: 47

adiaz: 30

WargnierP: 29

lucky7456969: 28

mbarros: 28

avida.matt: 26

JonathanW: 24

laguerre: 23

Member Stats:

Guest Posters: 69

Members: 122101

Moderators: 3

Admins: 4

Forum Stats:

Groups: 1

Forums: 5

Topics: 531

Posts: 2495