Advertisement

Single-reference type question

Started by December 09, 2011 11:56 AM
2 comments, last by WitchLord 13 years ago
In your game sample you are registering CGameMgr as single-reference type:

// Register the game manager as a singleton. The script will access it through the global property
r = engine->RegisterObjectType("CGameMgr", 0, asOBJ_REF | asOBJ_NOHANDLE); assert( r >= 0 );

Is it possible, to call methods of this object this way (like calling static methods):

CGameMgr.SomeMethod();
or
CGameMgr::SomeMethod();?
I have a global math object that I use. Since you really don't care about the type in this case and only about what it's name is and what it does, you could register your global object as something like

r=e->RegisterObjectType( "G_CGameManager",0,asOBJ_REF );
// Behaviors omitted.
r=e->RegisterObjectMethod( "G_CGameManager","void BeginGame() const",asMETHOD(CGameManager,BeginGame),asCALL_THISCALL );


Then you can register an already allocated instance of your game manager like so.


r=e->RegisterGlobalProperty( "const G_CGameManager@ CGameManager",(void*)gGameManager );


Now any time you want to reference it in your script:

CGameManager.BeginGame();


You just need to make sure that your factory function and your AddRef and Release functions don't do anything.
Rantings, ravings, and occasional insight from your typical code-monkey: http://angryprogrammingprimate.blogspot.com/
Advertisement


You just need to make sure that your factory function and your AddRef and Release functions don't do anything.


According to: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_reg_basicref.html "You can do this by registering the type as a normal reference type, but omit the registration of the factory behaviour. ".
That's correct. If you don't register the factory behaviour the script cannot instanciate the type. An by registering the type as NOHANDLE you tell the script engine that it shouldn't allow any extra handles to it either, thus only the references you supply to the object will be accessible.

It's not yet possible to call global functions (or static methods) as if they are in the name space of the class. Once I get to implement support for namespaces, this should be possible to support too.

You can trick AngelScript though, by registering the static method with CDECL_OBJLAST. AngelScript will pass the object pointer to the static method as the last argument, but as the calling convention is CDECL the function will simply ignore it. This would allow you to do something like this:


engine->RegisterObjectType("CGameMgr_t", 0, asOBJ_REF | asOBJ_HANDLE);
engine->RegisterObjectMethod("CGameMgr_t", "void StaticMethod()", asFUNCTION(CGameMgr::StaticMethod), asCALL_CDECL_OBJLAST);
engine->RegisterGlobalProperty("CGameMgr_t CGameMgr", (void*)gameMgr);


With this the static method can be called, almost as if it was a normal static method:


CGameMgr.StaticMethod();

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