Advertisement

Message Processing

Started by July 15, 2004 06:13 AM
3 comments, last by rKallmeyer 20 years, 7 months ago
I am working on the game plan now.And I'd like the message to be processed by function array. for example: void f1(); void f2(); void f3(); .... the Message: MSG_A=0 MSG_B=1 MSG_C=2 .... what I want is fun[] and when message MSG_A comes,the program will call fun[MSG_A]. So what I want to know is how to define the function array,and how to call it? Thanks in advance.
It would go something like:

typedef void (*FunctionPointer)(void);

FunctionPointer pointer_array[3] = { &f1, &f2, &f3 };

(*pointer_array[MSG_A])();
Advertisement
Or you can use SiCrane's method without star (*) :

typedef void FunctionPointer(void);

FunctionPointer pointer_array[3] = { f1, f2, f3 };

pointer_array[MSG_A]();

[Edited by - cuBie on July 15, 2004 9:32:20 AM]
I just want to add that there is almost NO performance
gain using a function pointer array over a switch statment.

The reson for this is that every desent compiler out there
generates a function pointer array for you if its worth it.

Just figured that I should mention it...
edit: theres a good thread about this here on gamedev somewhere, so if you want more info do a search for it :D
- Me
This isn't what you originally asked to do, but you get the same end result with the added benifet of not having to update the array every time you create a new message

// base classclass MESSAGE {    virtual void Foo() = 0;};// different message typesclass MESSAGEPRINT : public MESSAGE {private:    char* m_text;public:    MESSAGEPRINT (char* text);    void Foo(){       // do print stuff    }};


then add messages to your message handler like this

MessageHandler.AddMessage(new MESSAGEPRINT("print text"));


and when you need to process the message do this

// ... in message process functionm_message_que.front_message->Foo();


The idea is a little bit more involved but personaly I like it better.

This topic is closed to new replies.

Advertisement