Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Adding script member function to registered type?

Started by trybaczek Jan 10, 2010 at 7:34 AM 2 replies 1.1k views
Original Post
trybaczek
trybaczek
Hi Everybody! I am playing with AngelScript for a few days and I really like it (especially how easy it it so register things...) Now I try to integrate AS with my game engine. I have C++ class called GameObject, I am going to register it as a reference type eg.:

r = engine->RegisterObjectType("GameObject", 0, asOBJ_REF); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("GameObject", asBEHAVE_ADDREF, "void f()", asMETHOD(GameObject,AddRef), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("GameObject", asBEHAVE_RELEASE, "void f()", asMETHOD(GameObject,Release), asCALL_THISCALL); assert( r >= 0 );

is it possible to extend this new AngelScript type with scripted member methods? I want to script behavior of my class eg. AngelScript code:

class GameObject
{
void Tick(float deltaTime)
{
  this.MoveX(10); // "this" points to c++ registered GameObject type
}
};

so I can call Tick method on my GameObject instance reference (I want to avoid creating separate script object that handles scripting on my GameObject)... a pseudo code for that may look like:

asIScriptFunction *outFunc;
module->CompileFunction("GameObject.as", code, &outFunc);
engine->RegisterObjectMethod("GameObject", outFunc);

In general: is there any way to make "this" in script function (function compiled with script builder) point to registered c++ type instance? Kind Regards Tristan
WitchLord
WitchLord
No, that is not possible.

The way I do it in my own game engine is that I store the GameObject as a member of the script class. Like this:

// AngelScriptclass Controller{  Controller(GameObject @ent)  {    @entity = ent;  }  void Tick()  {    // Do something to the entity    entity.Move();  }  GameObject @entity;}


The GameObject will also have a pointer to the script object (through the asIScriptObject interface), that it will use to call the methods of the script object (if implemented).


In a future version of AngelScript I have plans to investigate possibilities to have script classes inherit from application registered classes, but I have serious doubts as to how well that might work.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
trybaczek
trybaczek
Thank you very much for the explanations,
I have one more question (similar one).
Will it be difficult to implement a way to
call global function supplying object pointer,
so this global function behaves at method
(this variable is set to given object).
I could try to implement it myself if I
have green light and some tips...
Cheers
Tristan
WitchLord
WitchLord
It's already available. Here's some references to the manual that mentions it:

Registering a function
Register object method
asCALL_CDECL_OBJLAST
asCALL_CDECL_OBJFIRST

Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.