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.