State Event design.

Started by
25 comments, last by Stani R 14 years, 1 month ago
I've decided to settle for a basic event(string name, void* object) system (object can be 0, so I can send an object or not). I'll probably add type_info stuff one day, but this fits the bill for big events and just simple onX events.
Advertisement
Okay, I made a basic implementation of it.

		case WM_KEYDOWN:		{			SEventKeyboard data;			data.key = wideParam;			data.pressed = true;						stateLayer->event("keyboard", (void*)&data);			return 0;		}


void CMainMenuState::onEvent(char* eventName, void* object){	IOLayer->log("%s_%i", eventName, eventName == "keyboard");


The output is:

keyboard_0

I don't understand.
in the expression 'eventName == "keyboard"'
"char *eventName" points to memory, where any string or other bytes could be.
"keyboard" boils down to another pointer in this expression, which points to bytes of memory with the character values needed to spell keyboard.

These 2 memory locations are not the same, regardless of whether the actual data in these 2 locations match or not, which evaluates to false, which rings up as "0" in your log statement.

What you want, is to either use stricmp(eventName, "keyboard") or much more preferably, use a real string class, such as std::string, which implements comparison on strings internally.
Haha, thanks.
Last thing, is there a way to avoid a big chain of else ifs for strings?
Depends what you're going to do once you match a string. There's many options really. One example if you were calling a function (note that this is untested - it's just to help you brainstorm):
typedef std::function<bool (void *)> EventCallback;typedef std::map<std::string, EventCallback> EventMap;// -----class CMainMenuState {public:	// Return true if successfully processed	bool onEvent(const std::string &eventName, void *user);private:	// stuff	EventMap callbacks;};// -----bool CMainMenuState::onEvent(const std::string &eventName, void *user) {	IOLayer->log("%s_%i", eventName.c_str(), eventName == "keyboard"); // NOW it works	if (callbacks.find(eventName) != callbacks.end())		return callbacks[eventName](user);	return false;}
Quote:Original post by Jookia
Last thing, is there a way to avoid a big chain of else ifs for strings?


You only need to check for the ones you are interested in. Assuming you have a component-entity system of some kind, any given component will only care about a limited number of events - for instance, CollisionResponseComponent only wants to hear about CollisionEvent.

This topic is closed to new replies.

Advertisement