Advertisement

I'm having trouble passing a pointer to AngelScript

Started by July 12, 2020 09:51 AM
3 comments, last by WitchLord 4 years, 4 months ago

I'm working on a game engine, using AngelScript. I have registered my entity class to AngleScript (there is no constructor on purpose, I don't want users to be able to declare new entities):

r = engine->RegisterObjectType("Entity", 0, asOBJ_REF); assert(r >= 0);
r = engine->RegisterObjectBehaviour("Entity", asBEHAVE_ADDREF, "void f()", asMETHOD(Entity, AddRef), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectBehaviour("Entity", asBEHAVE_RELEASE, "void f()", asMETHOD(Entity, ReleaseRef), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("Entity", "Transform& getTransform()", asMETHOD(Entity, get<Transform>), asCALL_THISCALL); assert(r >= 0);

I've also registered my Transform component:

r = engine->RegisterObjectType("Transform", sizeof(Transform), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK | asOBJ_APP_CLASS_ALLFLOATS); assert(r >= 0);
r = engine->RegisterObjectProperty("Transform", "Vec2 position", asOFFSET(Transform, position)); assert(r >= 0);
r = engine->RegisterObjectProperty("Transform", "Vec2 scale", asOFFSET(Transform, scale)); assert(r >= 0);
r = engine->RegisterObjectProperty("Transform", "float rotation", asOFFSET(Transform, rotation)); assert(r >= 0);

(My Vec2 is also registered, and I can confirm that it works).

I've added a global object to each script for the entity that it is sitting on:

r = engine->RegisterGlobalProperty("Entity entity", entity); assert(r >= 0);

entity (the one that I am passing into RegisterGlobalProperty()) is a pointer to the entity that the script is sitting on. So, if entity is a pointer, and getTransform() is returning a reference, then why doesn't the following set the entities position from a script?

entity.getTransform().position.x = 25.0f;

What am I missing? getTransform() works from native C++, so why isn't it working here?

None

What does the method Entity::get<Transform>() look like in C++?

Have you checked the values of the returned Transform in the script? Is the script really receiving a proper reference to the Transform object?

Have you checked if the script is really getting the correct reference to the entity via the global property?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

@WitchLord Here's the get function in C++. The values of getTransform() are correct to the entity that it is sitting on.

template <typename T> T& get() const
{
	assert(has<T>())
	auto ptr(componentArray[getComponentTypeID<T>()]);
	return *static_cast<T*>(ptr);
}

None

Thanks. The implementation looks fine.

What of the other questions?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement