🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Cpp based asIScriptAny API

Started by
1 comment, last by WitchLord 18 years, 9 months ago
Im in the process of implementing an event handling system for scripts and I want them to be able to handle as wide a variety of events as possible. Originally I thought it would be as simple as just passing a pointer to an asIScriptAny object, and make the event handler responsible for knowing the proper data type that was stored in the any object, but it appears there is no easy to use interface for this. My idea is that I want the system to be able to handle something like the following: A network event that passes a pointer to a packet to the event handler. A network event that passes a string object to the event handler. A mouse click that passes two integers as parameters. A timer that fires and passes no parameters to the event handler. All event handlers should be script based In my original thoughts I was just going to have the script engine wrapper be responsible for preparing the context and set up the parameters for the script function being called, but I think it would be better to have the object that fires the event to be responsible for everything by passing him a pointer to the script context that is going to be used. My current set up passes a pointer to a "Command" object that has 1 public method and 1 private member. class CMMCommand { public: CMMCommand(CAngelScriptWrapper *Host); virtual ~CMMCommand(); int FireEvent(string &EventHandler, asIScriptAny *EventArg); private: CAngelScriptWrapper *ScriptEngineHost; };</code> Currently when FireEvent is called, it simply adds those parameters to a vector as a pair, and every so often, the main script context is suspended and a context to handle the event is prepared and executed based &#111;n the values in the vector. In writing this post, I think that perhaps should implement an "IFiresEvent" interface and change the FireEvent method to this: int FireEvent(IFireEvent *EventFireer); This method can still place the Event in a vector to be called at a later time, but this time, when the vector is being polled for events, I will do this: EventFireer-&gt;PrepareContext(EventContext); It requires the event fireer to be aware of a script context, but this way I think the event firing object can be responsible for ensuring a properly formed event handler. Any suggestions or further input?
Advertisement
I'm doing something similiar in my mud engine. I avoided passing parameters completly. Instead, there are a few psuedo-global variables set right before script execution, and the script has function bound to get them. I don't need to pass differing parameters to different script functions. They are all void func().
The title suggests that you're not sure how the asIScriptAny interface is used, though you didn't specifically state that in your text. I'll try to answer it anyway, just in case.

If you need to create a script any object you'll have to use the method asIScriptEngine::CreateScriptObject() which works like this:

// Get the type ID for the any objects. This can // be done once for the lifetime of the engineint anyTypeId = engine->GetTypeIdByDecl(0, "any"); // Create an object based on the type ID.asIScriptAny *anyObject = (asIScriptAny*)engine->CreateScriptObject(anyTypeId);if( anyObject == 0 ){  // Something went wrong}// Store the handle to an object in the any object. // This will increase the refcount of the object so // we should release our reference afterwards.int objHandleTypeId = engine->GetTypeIdByDecl(0, "MyObject@");MyObject *obj = new MyObject();anyObject->Store(&obj, objHandleTypeId);obj->Release();// When the any object is no longer used, release itanyObject->Release();


When the script receives the any type it will do the following to work on the contained object:

void EventHandler(any@ e){  MyObject @obj;  e.Retrieve(@obj);  if( obj != null )  {    // Handle the event  }  else  {      // The object stored in the any type    // is incompatible with MyObject.  }}


I hope that helps.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement