Register assignment operator for glm::vec3 based value type in Angelscript

1k views Asked by At

I have registered a Vec3 value type in Angelscript using:

RegisterObjectType("Vec3", sizeof(glm::vec3), asOBJ_VALUE | asGetTypeTraits<glm::vec3>());

I have also registered a constructor and destructor function for that type.

I have registered another type, "Transform", and given it a method "Vec3 GetTranslation()", which corresponds to the C++ class method "const glm::vec3& GetTranslation()"

RegisterObjectMethod("Transform", "Vec3 GetTranslation()", asMETHOD(Transform, GetTranslation), asCALL_THISCALL);

This is how I am using the two in Angelscript:

Vec3 v = transform.GetTranslation();

And I am getting this error:

No appropriate opAssign method found in 'Vec3' for value assignment

What is the correct way of registering an assignment operator, or any operator for that matter?

I tried this:

RegisterObjectMethod("Vec3", "Vec3 opAssign(const Vec3 &in)", asMETHOD(glm::vec3, operator=), asCALL_THISCALL);

And got this compile error:

error: no matches converting function 'operator=' to type 'void (struct glm::detail::tvec3<float, (glm::precision)0u>::*)()'|
note: in expansion of macro 'asMETHOD'|
note: candidates are: template<class U> glm::detail::tvec3<T, P>& glm::detail::tvec3<T, P>::operator=(const glm::detail::tvec3<U, P>&) [with U = U; T = float; glm::precision P = (glm::precision)0u]
note:                 glm::detail::tvec3<T, P>& glm::detail::tvec3<T, P>::operator=(const glm::detail::tvec3<T, P>&) [with T = float; glm::precision P = (glm::precision)0u]|
1

There are 1 answers

0
Hayden Whitehead On BEST ANSWER

I fixed the compile error by using asMETHODPR, rather than asMETHOD:

RegisterObjectMethod("Vec3", "Vec3& opAssign(const Vec3 &in)", asMETHODPR(glm::vec3, operator=, (const glm::vec3&), glm::vec3&), asCALL_THISCALL);

I also needed to change the opAssign method to return a Vec3&, instead of Vec3.

And I changed the GetTranslation() method to return a Vec3& as well:

RegisterObjectMethod("Transform", "Vec3& GetTranslation()", asMETHOD(Transform, GetTranslation), asCALL_THISCALL);

The values are now copying over in the script.