Sign in to follow this  
kaveldun

Class instanes breaking outside scope

Recommended Posts

kaveldun    142
Hey.

Angelscript seems like a great library so far, but despite the documentation, there are some details on how to use classes with angelscript that I haven't managed to figure out.

I have managed to create instances, return them to the c++ side, and then call some member functions upon them. However, as soon as they go out of scope, I cannot use them, and I get a crash at any Execute() call involving them, or even if I do ->Release() on them. Some of my code to illustrate:

[source lang="cpp"]asIScriptObject* WndScript::ScriptKeeper::instantiateEntityClass(std::string type, unsigned int id)
{
asIScriptModule* mod = engine->GetModule("EntityScripts");
asIObjectType* objectType = engine->GetObjectTypeById(mod->GetTypeIdByDecl(type.c_str()));

std::stringstream factoryFunction;
factoryFunction << type << " @" << type << "()";
asIScriptFunction* factory = objectType->GetFactoryByDecl(factoryFunction.str().c_str());

//instantiate
entityContext->Prepare(factory);
entityContext->Execute();
asIScriptObject* returnObj = *(asIScriptObject**) entityContext->GetAddressOfReturnValue();
returnObj->AddRef();

//set id
asIScriptFunction* method = objectType->GetMethodByDecl("void setId(uint id)");
entityContext->Prepare(method);
entityContext->SetObject(returnObj);
entityContext->SetArgDWord(0, id);
entityContext->Execute();

//set type
method = objectType->GetMethodByDecl("void setType(string type)");
entityContext->Prepare(method);
entityContext->SetObject(returnObj);
entityContext->SetArgObject(0, &type);
entityContext->Execute();


method = objectType->GetMethodByDecl("void onCollide()");
if( method == 0 )
{
// The function couldn't be found. Instruct the script writer
// to include the expected function in the script.
printf("The script must have the function '%s'. Please add it and try again.\n", "void onCollide()");
return NULL;
}

entityContext->Prepare(method);
entityContext->SetObject(returnObj);
entityContext->Execute();

return returnObj;
}[/source]
Here is my function that instantiates a script class, and all functions I call upon them in this function works. But when I use that function like so...:
[source lang="cpp"]asIScriptObject* scriptEntity = *(asIScriptObject**) WndScript::ScriptKeeper::getInstance()->instantiateEntityClass(entity->getType(), entity->getUniqueId());[/source]
...then I cannot use "scriptEntity" to call any methods or do anything with it without getting a crash with "access violation". As if it dies whenever going outside the scope of the function that created it. Am I missing something?

Thanks.

Share this post


Link to post
Share on other sites
WitchLord    4677
It seems the code snippet got a messed up in your post. It's not clear how exactly you're creating the script object instance.

The way you're calling the object's methods is correct, however you're not checking any return values. Are you sure everything passes successfully?

You seem to be doing an incorrect cast when calling the instantiateEntityClass() method. The method is already returning asIScriptObject*, so why are you casting it to asIScriptObject**, and then dereferencing it?

Regards,
Andreas

Share this post


Link to post
Share on other sites
kaveldun    142
Hmmm weird with the post formatting. :S

Anyway, it worked when I removed that cast, thanks! :) Not sure what I was thinking, I guess it might be a rest from a copy-paste or something.
One of those mistakes that you end up overlooking all the time, checking everything else.

Anyways, thanks again, nice to finally have it working. :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this