I''m using Angelscript 1.5.2 in a game engine that I''m writing.
For the most part everything has gone smoothly; I use ExecuteStep() to handle the execution of multiple scripts concurrently and have exposed interfaces to the engine like this:
Scripting.GetEngine()->RegisterObjectType("CCamera", 0);
Scripting.GetEngine()->RegisterGlobalProperty("CCamera Camera", this, 0);
Scripting.GetEngine()->RegisterObjectMethod("CCamera", "void RotateView(float angle, float x, float y, float z)", (asMETHOD)CCamera::RotateView, asCALL_THISCALL);
// etc...
All the other modules for the engine expose their interfaces the same way and everything works great. My problems start when I try and work with objects and passing them around. For example, suppose I create a simple vector class:
struct MYVECTOR3
{
float x, y, z;
};
And then register it with Angelscript:
m_pEngine->RegisterObjectType("MYVECTOR3", sizeof(MYVECTOR3));
m_pEngine->RegisterObjectProperty("MYVECTOR3", "float x", offsetof(MYVECTOR3, x), 0);
m_pEngine->RegisterObjectProperty("MYVECTOR3", "float y", offsetof(MYVECTOR3, y), 0);
m_pEngine->RegisterObjectProperty("MYVECTOR3", "float z", offsetof(MYVECTOR3, z), 0);
Everything is 32bit and should be aligned okay for Angelscript to work with. This seems to work when I declare a variable of type MYVECTOR3 inside a script. I can change the x, y, and z values (inside the script), and pass those as floats into the engine.
If I register a function/method that accepts a parameter of type MYVECTOR3, however, it does not work. For example, if I create a MYVECTOR3 object and assign x=1.2, y=3.4, and z=5.6, and pass that by value, then I see x=0.0, y=1.2, z=3.4 on the other end.
I''ve been pulling my hair trying to figure whats going wrong here. Because I get (0.0, 1.2, 3.4) out on the engine side, it seems obvious that Angelscript is giving the correct data, but shifted 32bits. I did the following hack and that seemed to fix things:
// Adding 4 to the sizeof()
m_pEngine->RegisterObjectType("MYVECTOR3", sizeof(MYVECTOR3) + 4);
m_pEngine->RegisterObjectProperty("MYVECTOR3", "float x", offsetof(MYVECTOR3, x), 0);
m_pEngine->RegisterObjectProperty("MYVECTOR3", "float y", offsetof(MYVECTOR3, y), 0);
m_pEngine->RegisterObjectProperty("MYVECTOR3", "float z", offsetof(MYVECTOR3, z), 0);
// This is a function inside the engine.
void test(MYVECTOR3 v)
{
MYVECTOR3* p = (MYVECTOR3*)((char*)&v + 4);
Console.Write("Vector=(%f,%f,%f)\n", p->x, p->y, p->z);
}
// The function is regsitered like this:
m_pEngine->RegisterGlobalFunction("void test(MYVECTOR3 v)", (asFUNCTION)test, asCALL_CDECL);
But this is unacceptable... Has anyone experienced similar problems?