Advertisement

Why is this not working?

Started by November 12, 2011 07:37 PM
2 comments, last by WitchLord 13 years ago
I am trying to create an application-side instance of a script-side class.

AngelScript


class Player : ISceneNodeController
{

int health;

Player()
{
health = 100;
print ("created a player");
}
void update()
{
print ("health: " + health);
}
};



C++

asIScriptContext* context = manager->PrepareContext(script->factoryFuncID);
manager->ExecuteCall(context);
object = *(asIScriptObject**)context->GetReturnAddress();
manager->ReturnContext(context);

int propCount = object->GetPropertyCount(); // ERROR for some reason...
for (int i = 0; i < propCount; i++)
{
if (strcmp("health", object->GetPropertyName(i)) == 0)
{
int* f = (int*)object->GetAddressOfProperty(0);
int g = 32;
memcpy(f, &g, sizeof(int));
}
}




I can see "created a player" in the console output, but then I get an 'access violation' error at the line marked.

(asIScriptObject* object was forward declared)

What's going on?


PS: I implemented a asIScriptContext pooling system similar to in the 'game' example.
You forgot to increase the reference count of the script object. When the context's Unprepare() was called (I believe you do this when returning the context to the pool), it released the reference the context held to the object and thus destroyed it.

Add 'object->AddRef();' before 'manager->ReturnContext(context);'


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

Advertisement
I read the page in the manual about instantiating script objects, and it said much the same thing.
So I changed the code


asIScriptContext* context = manager->PrepareContext(script->factoryFuncID);
manager->ExecuteCall(context);
object = *(asIScriptObject**)context->GetReturnAddress();
object->AddRef(); // <- Access violation error now appears here
manager->ReturnContext(context);

int propCount = object->GetPropertyCount();
for (int i = 0; i < propCount; i++)
{
if (strcmp("health", object->GetPropertyName(i)) == 0)
{
int* f = (int*)object->GetAddressOfProperty(0);
int g = 32;
memcpy(f, &g, sizeof(int));
}
}


Now the error appears at object->AddRef();
Now what? The angelscript constructor is clearly working, so surely a valid object should be returned?
Sorry for not spotting this before, but you're dereferencing the address returned from GetReturnAddress(). This method is actually returning the address to the object itself, so you should just reinterpret_cast it to a asIScriptObject pointer.

[source]
object = (asIScriptObject*)context->GetReturnAddress();
[/source]

In the game sample, I'm using the method GetAddressOfReturnValue(), which returns a pointer to the pointer to the object, so here it is correct to dereference the pointer.

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