Weird continual crash, maybe a problem with multiple objects having same script

Started by
2 comments, last by WitchLord 10 years, 4 months ago

I have the task of incorporating AngelScript into our game engine and I'm having some problems. The engine uses the concept of a GameObject, which can have a script attached to it. When I have the same script running on different GameObjects, things seem to get strange with seemingly random crashes.

I keep getting the following crash in the function asCContext::ExecuteNext():

case asBC_RDR4:

*(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)&m_regs.valueRegister; <---EXC_BAD_ACCESS( code=1, address=0xfffffff1 )

l_bc++;

break;

The following crash always happens when the same script is compiled for multiple GameObjects of the same type, and I examined the m_currentFunction variable in the context and it was calling a function that was defined in the script file and not a globally registered function.

case asBC_WRTV4:

**(asDWORD**)&m_regs.valueRegister = *(l_fp - asBC_SWORDARG0(l_bc));<---EXC_BAD_ACCESS( code=2, address=0xfffffff1 )

l_bc++;

break;

What are some general things to check for with theses particular crashes happening?

Advertisement

My first thought is that you're doing something wrong when reusing the context or script modules. Can you show us how you've integrated the script engine?

A second thought is that perhaps you've encountered some obscure bug in the script compiler that makes it generate invalid bytecode. You've already found m_currentFunction. Can you use it to identify the script code that is being executed and show the script code here, so I can have a look at it?

Does the crash always occur on the same place in the script? You say the crash happens when running the same script on different GameObjects. Does this mean the script works as expected when you only have one GameObject? That would pretty much rule out a problem with the script it self, or a bug in the script compiler.

You also say you compile the same script for multiple GameObjects. Are you reusing the same script module when you do this? Did you cache the asIScriptFunctions before you reused the module (it would invalidate the script function unless you call AddRef on it).

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Hi! Thanks for getting back to me and sorry for my delay in responding.

Script engine integration:

I use the scriptbuilder add on to compile the script code:

CScriptBuilder builder;

Currently, each script compiled gets it's own module, but I thought I saw that you have a module only for each different type of object, so there's a difference.

Then, I cache the function's to be called like so:

UpdateFunction = Type->GetMethodByDecl("void Update( float )");

After this I get the factory function that is used to create the script object like so:

std::string s = std::string( Type->GetName() ) + "@ " + std::string( Type->GetName() ) + "( GAMEOBJECT@ )";

FactoryFunc = Type->GetFactoryByDecl( s.c_str() );

and from the c++ side I call this to create the class along with all of it's variables:

Context->Prepare( FactoryFunc );

Context->SetArgObject( 0, GameObject ); //the c++ gameobject.

Context->Execute();

next, I cache the newly created script object in the GameObject:

GameObject->ScriptFactory = *( (asIScriptObject**)Context->GetAddressOfReturnValue() );

GameObject->ScriptFactory->AddRef();

Context->Unprepare();

Here is what my script's look like:

class MainPlayer : IGameObjectScript { //IGameObjectScript is an interface with no methods defined.

#include "SharedCode.as"

GAMEOBJECT@ GameObject;

//the App will pass in the newly created GameObject.

MainPlayer( GAMEOBJECT@ Object ) {

@GameObject = @Object;

}

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

void Update( float DeltaTime ) {

Thing1 = ComputeSomethingReallyFast( @GameObject );

}

//variable list.

float ScaleFactor = 1.13f;

float Thing1 = 0;

float Thing2 = ( 20.f / 60.f );

vector4 ComputedPosition;

}

the SharedCode.as file has various functions defined that are common to a set of GameObject's that share the same script file.

float ComputeSomethingReallyFast( GAMEOBJECT@ GameObject ) {

return 9000.f;

}

I register the GAMEOBJECT like so:

RegisterObjectType( "GAMEOBJECT", 0, asOBJ_REF | asOBJ_NOCOUNT );

RegisterGlobalFunction( "void GameObject_GetPosition( GAMEOBJECT@ GameObject, vector4& Position )", asFUNCTIONPR( GameObject_GetPosition, ( GAMEOBJECT* GameObject, vector4* Position ), void ), asCALL_CDECL );

RegisterGlobalFunction( "void GameObject_SetPosition( GAMEOBJECT@ GameObject, vector4& Position )", asFUNCTIONPR( GameObject_SetPosition, ( GAMEOBJECT* GameObject, vector4* Position ), void ), asCALL_CDECL );

I register the vector4 class like so:

RegisterObjectType( "vector4", sizeof( vector4 ), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA ); assert( r >= 0 );

RegisterObjectBehaviour( "vector4", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR( ConstructVector4x, (vector4*), void), asCALL_CDECL_OBJLAST );

RegisterObjectBehaviour( "vector4", asBEHAVE_CONSTRUCT, "void f( float, float, float, float )", asFUNCTIONPR( ConstructVector4, ( float, float, float, float, vector4* ), void ), asCALL_CDECL_OBJLAST );

RegisterObjectMethod( "vector4", "vector4 &opAssign(const vector4 &in)", asFUNCTIONPR( Vector4Assignment,(const vector4&, vector4*), vector4& ), asCALL_CDECL_OBJLAST);

RegisterObjectProperty( "vector4", "float x", offsetof( vector4, x ) );

RegisterObjectProperty( "vector4", "float y", offsetof( vector4, y ) );

RegisterObjectProperty( "vector4", "float z", offsetof( vector4, z ) );

RegisterObjectProperty( "vector4", "float w", offsetof( vector4, w ) );

RegisterGlobalFunction( "const vector4& vector4_scale( vector4& result , const vector4& v , float scale )", asFUNCTIONPR( vector4_scale, ( vector4* result , const vector4* v , float scale ), const vector4* ), asCALL_CDECL );

Can functions that are registered like this maybe cause problems in the script compiler if called like this:

vector4 Temp;

vector4_scale( Temp, ComputedPosition, ScaleFactor );

instead of like this where I actually use the return value?

ComputedPosition = vector4_scale( Temp, ComputedPosition, ScaleFactor );

Now, each game object is updated like so:

Context->Prepare( FactoryFunc );

Context->SetObject( GameObject->ScriptFactory );

Context->SetArgFloat( 0, FloatValue ); //FloatValue is just some number that get's passed to the script function every frame from the App or c++ side.

Context->Execute();

Context->Unprepare();

Doing it this way keeps all script data tied to the one owning gameobject and there are no internal conflicts with the variables correct?

This seems like the right way to instantiate all script objects but when I have several objects all sharing the same script file things seem to go awry or maybe I'm just doing something stupid.

There are times when a crash occurs and I inspect the m_currentFunction and it's pointing to a function defined in the script file SharedCode.as and I was wondering why this could possibly be a problem because the code appears to be correct.

If I'm not being clear just let me know and I will try to be more specific.

Thanks for your help.

I'll need to do a more detailed review of the info you posted, but after a quick readthrough I see nothing wrong with what you're doing.

The script you're using is also quite trivial so I find it hard to believe the you've stumbled on a bug in the compiler. Though, it can't be ruled out.

Yet, I can't help but think that you might be mixing the function pointers and object instances. Even though the modules are compiled from the same source code each object type will be unique and you must use the correct function pointer with the matching object instance.

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