• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Anglescript + passing objects

## Recommended Posts

Desdemona    158
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?

##### Share on other sites
Desdemona    158
Hmm, I just spent some time switching the engine over to v1.6.0 of Angelscript, and it *seems* like it is solving these problems.

##### Share on other sites
WitchLord    4677
It was most likely a bug in version 1.5.2. I seem to remember fixing a problem like this, but looking through the list of changes I can't find it.

AngelScript is in a state of development, so if you are working with it and are having some problem that you think is not in your code, don't hesitate to ask, either here in the forum or directly to my e-mail. There are most likely other bugs in AngelScript that hasn't been found yet.

It might be a good idea to switch to the beta 1.6.1 already, since there are a couple of bugs in 1.6.0 that may make it difficult to do some things:

- An exception occurs sometimes when compiling object method calls with objects as parameters
- booleans was treated as 4 bytes by AngelScript, when they really should be only 1 byte (actually, still is. I haven't released the fix for this one yet)

I'm currently working on the final changes for version 1.6.1 so that I can release it. Hopefully that will be done already this weekend, or at the most early next week.

__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library

[edited by - WitchLord on March 20, 2004 10:50:35 AM]