Jump to content
  • Advertisement
Sign in to follow this  
hupsilardee

Why is this not working?

This topic is 2588 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

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.

Share this post


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

Share this post


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

Share this post


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

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!