Hey there.
I'm just beginning to use AngelScript for my own project, and I must say it is brilliant. This is the first thing I've needed to get help on, as the documentation is extremely helpful.
Okay, so basically, here's my question: is it possible via AngelScript API to register a type that utilizes the well-known member function chaining method? An example of such a class is below:
class ChainMe
{
public:
int X;
ChainMe() :
X(0)
{
}
ChainMe &Increase(const int &v)
{
X += v;
return *this;
}
};
How would one register such a type in AngelScript so that you may use it from AngelScript as you would from C++, like:
ChainMe().Increase(5).Increase(15).Increase(25)
My first (failed) attempt at doing this involved this very basic implementation:
engine->RegisterObjectType("ChainMe", sizeof(ChainMe), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_C);
engine->RegisterObjectMethod("ChainMe", "ChainMe &Increase(const int &in)", asMETHOD(ChainMe, Increase), asCALL_THISCALL);
(This also included a constructor but they aren't important for this sample)
The engine runs the constructor, as expected, then runs Increase, as expected.. however, following that, it calls the constructor again, then errors with "GC cannot free an object of type '_builtin_objecttype_', it is kept alive by the application".
EDIT: I managed to get the error to go away by adding asOBJ_NOHANDLE to the type flags. I think that this was the problem. I'm still getting a two-constructor call when I try passing the return value of the functions to another function via a reference (ie, "const ChainMe &in")
EDIT2: Via adding one extra function (which I love about AngelScript), I have figured out that the "extra constructor" being called is the copy constructor. Fair enough, but, why is it copying when it only needs to pass a reference? Is it because it is an r-value reference?
Thanks in advance,
-P