I'm trying to get two scripts (on different machines) to communicate, by sending each other structs that are defined by the scripts themselves. To send a struct is easy enough: I declare ‘message’ to be an interface (without any methods, just an interface). A script-class can then just inherit from this interface, and passed to function send(message@) which serializes it and sends it to the other script.
I was hoping to get the other script to register a callback for handling the message, but here I run into a minor problem: if I register a generic handler for ‘message’ I need to cast the message type, which is something I would prefer to avoid, and I have not found a way to allow registration of callbacks that take a script-defined class as an argument. That is, at the time when I try to register the handler, the type being handled must already be known.
In code, the setup:
engine->RegisterInterface ("message");
engine->RegisterGlobalFunction ("void send(message @)", asFUNCTION (HandleSend), asCALL_CDECL);
engine->RegisterFuncdef ("void message_callback(message @)");
engine->RegisterGlobalFunction("void set_message_callback(message_callback @)", asFUNCTION (MessageCallback), asCALL_CDECL);
Shared between scripts:
class my_message: message {
int int_val = 42;
};
Sending script:
my_message m;
send (m);
This works, because my_message is a message and thus meets the interface requirements.
The receiving script:
void handle_my_message (message @msg) {
auto @my_msg = cast<my_message> (msg);
...do stuff with my_msg...
}
set_message_callback (handle_my_message);
This also works, but requires the cast. Can I somehow do this?
void handle_my_message (my_message @msg) {
...do stuff with msg...
}
set_message_callback (handle_my_message);
I prefer the type safety this provides, plus I can then register a message handler for each supported message type, instead of having to figure out the right type at runtime (presumably using some sort of additional identifier). But is it possible?