Hi! I'm adding AngelScript to my simple engine, which has a class like so:
class Transform3D
{
glm::vec3 get_pos() const { clear_dirty(); return pos; }
private:
glm::vec3 pos;
}
class Node3D : Node
{
private:
Transform3D transform;
public:
Transform3D& get_transform();
}
auto transform_to_str(const Transform3D* trans) -> std::string
{
// just show the pos for now
return glm::to_string(trans->get_pos());
}
I registered it like so:
int r = pEngine->RegisterObjectType("Transform3D", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0);
r = pEngine->RegisterObjectMethod("Transform3D", "vec3 get_pos() const property", asMETHOD(Transform3D, get_pos), asCALL_THISCALL);
assert(r >= 0);
r = pEngine->RegisterObjectMethod("Transform3D", "string opImplConv() const", asFUNCTION(transform_to_str), asCALL_CDECL_OBJLAST);
assert(r >= 0);
// now register Node3D
r = pEngine->RegisterObjectType("Node3D", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0);
r = pEngine->RegisterObjectMethod("Node3D", "Transform3D@ get_xform() property", asMETHOD(Node3D, transform), asCALL_THISCALL);
assert(r >= 0);
In my script, I have this:
auto @xform = @self.xform;
log("xform: " + xform);
log("xform.pos: " + xform.pos);
And it prints:
[SCRIPT] xform: vec3(0.000000, 0.000000, 5.000000)
[SCRIPT] xform.pos: vec3(0.250000, -22.000000, 18.000000)
When I check with a debugger, the call to Transform3D::get_pos
that happens when accessing it via .pos
is made on an invalid/garbage this
pointer, which ends up printing garbage values.
What am I doing wrong? Why is it different?