🎉 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!

Upgrading legacy code

Started by
1 comment, last by Miss 5 years, 4 months ago

I am currently working on a legacy codebase that was using AngelScript 2.4.1e for its scripting engine and I'm trying to upgrade it.

After having settled with the compilation errors, one problem that I encountered is that all the scripts have a common function which is called from the application to set an instance of a Game class and that Game class is an interface/wrapper for interacting with the game engine (each script instance is sandboxed).

Scripts:


Game@ instance;

void SetInstance(Game &in i)
{
    @instance = @i;
}

Application:


class Game
{
  private :
  	Game();
  public :
  	Game(IScript *);
  	virtual ~Game();
  	int AddRef();
  	int Release();
  	void ScriptLoaded();
  // ...
};

// Legacy code:
r=engine->RegisterObjectType("Game", sizeof(Game), asOBJ_CLASS);
r=engine->RegisterObjectBehaviour("Game", asBEHAVE_ADDREF, "void f()", asMETHOD(Game, AddRef), asCALL_THISCALL);
r=engine->RegisterObjectBehaviour("Game", asBEHAVE_RELEASE, "void f()", asMETHOD(Game, Release), asCALL_THISCALL);
// ...

// After upgrade:
r=engine->RegisterObjectType("Game", sizeof(Game), asOBJ_REF); assert(r >= 0);
r=engine->RegisterObjectBehaviour("Game", asBEHAVE_ADDREF, "void f()", asMETHOD(Game, AddRef), asCALL_THISCALL); assert(r >= 0);
r=engine->RegisterObjectBehaviour("Game", asBEHAVE_RELEASE, "void f()", asMETHOD(Game, Release), asCALL_THISCALL); assert(r >= 0);
// ...

// In Game's ScriptLoaded method
context->Prepare(module->GetFunctionByName("SetInstance"));
context->SetArgObject(0, (void*)this);
context->Execute();

The error that I am getting and being thrown during runtime is "Parameter type can't be 'Game&in', because the type cannot be instantiated." when SetInstance is being compiled/called.

How can I fix it? I tried registering a copy constructor with asBEHAVE_FACTORY but no luck. Keeping in mind I would like to avoid editing manually over 300 scripts if possible.

Thanks in advance.

 

Advertisement

You're registering Game only as asOBJ_REF, which means you can only pass it as Game@ and &inout. You're passing it with &in, which I believe is only possible if it's an asOBJ_VALUE.

Your choices are to change the script to simply pass the Game param as "Game &i" (or "Game@ i") rather than "Game &in i", or to allow the class to be a value as well, which might be more involved. You can take a look at the scripthandle addon for an example, which is a combination of asOBJ_VALUE, asOBJ_ASHANDLE, and asOBJ_GC.

Changing the scripts is the best solution, in my opinion. (Search & replace all files maybe?)

This topic is closed to new replies.

Advertisement