Advertisement

Can declare a reference to a value type in a method body?

Started by May 26, 2020 06:11 AM
1 comment, last by WitchLord 4 years, 6 months ago

Basically what the title says. I know AS support pass value types by reference on parameters, but it is possible to define a reference to a value type in a method body? For example I've registered a C++ Vector2 as a value type, each operations returns a reference to itself for method chaining.

engine->RegisterObjectMethod("Vector2", "Vector2& add(const Vector2 &in)", asMETHODPR(Vector2, add, (Vector2&), Vector2&), asCALL_THISCALL);
engine->RegisterObjectMethod("Vector2", "Vector2& sub(const Vector2 &in)", asMETHODPR(Vector2, sub, (Vector2&), Vector2&), asCALL_THISCALL);

Then it can be possible to do in AngelScript the following? or should I register it as a reference type?

void main() {
	Vector2 A(1,5);
	Vector2 B(7,4);
	Vector2& directionAB = B.sub(A);
}

Thanks in advance

No, this is unfortunately not yet possible.

You can register vector2 as a reference type, but is it really worth it? Having it as a reference type, thus require a dynamic memory allocation on the heap for each instance, will create an overhead, not only in performance but also when interfacing with your application that expects normal value vector2 types.

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