Jump to content
  • Advertisement
Sign in to follow this  
virious

Single-reference type question

This topic is 2560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!