🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

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

Started by
1 comment, last by WitchLord 4 years 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

Advertisement

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