Passing large POD as reference
Is there any method to pass POD arguments by reference without copying them around? (with safe references turned on)
This POD values have guaranteed life time, but limited to called script function only. Something similar to opaque pointer described in this topic: http://www.gamedev.n...to-angelscript/
For now I just registered it as reference type with dummy AddRef/Release, but this allows to store handle for use beyond scope.
Btw. why does AS reject "(const Type &in name)" declaration, with error: " Parameter type can't be 'const Type&' ", but accepts "(const Type &inout name)"? (my ver is 2.22.0 r1024)
The only way this can be prevented is to disallow the script to store the pointer at all. The easiest way of doing it is to use the NOHANDLE flag when registering the type, then the script will only be able to access a reference given by the app but not pass it to around.
Another option could be to register the type as ordinary REF, but then add extra validation on the compilation to deny the scripts from declaring global variables or class members of that type.
I could add a flag to have the compiler verify this automatically, but I'm not sure how useful it is. Even without the flag it can quite easily be validated from the application after the compilation by enumerating class members and global variables.
Does that help?
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
Btw. why does AS reject "(const Type &in name)" declaration, with error: " Parameter type can't be 'const Type&' ", but accepts "(const Type &inout name)"? (my ver is 2.22.0 r1024)
This looks like a bug. Under which circumstance does it happen?
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
Originally I was thinking about some run-time checks, that reset unwanted handles, but this would be over complicated (not to mention cpu time consuming).
As for possible bug. It happens with simple ref type being passed as const &in.
LPCTHSTR IPC = "IContactList";
r = pScript->RegisterObjectType(IPC,0,asOBJ_REF); assert(r>=0);
r = pScript->RegisterObjectBehaviour(IPC, asBEHAVE_ADDREF, "void f()", asFUNCTION(dummy), asCALL_CDECL_OBJLAST); assert(r>=0);
r = pScript->RegisterObjectBehaviour(IPC, asBEHAVE_RELEASE, "void f()", asFUNCTION(dummy), asCALL_CDECL_OBJLAST); assert(r>=0);
r = pScript->RegisterObjectProperty(IPC, "vec3 AvgPos", offsetof(CN3EventCollide, AvgPos)); assert(r >= 0);
//and so on...
And in AS file:
int OnCollide_plastic_concrete(CRigidBody@ pRB0, uint ShapeID0, CRigidBody@ pRB1, uint ShapeID1, const IContactList &in List) //error with &in but not &inout
{
return 0;
}
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
The type is not allowed to be &in, because the type cannot be instanciated, which also means it cannot be copied.
Even though the function is declared to receive a reference, the &in means that the reference will only be used as input. The way AngelScript guarantees that the original value isn't accidentally modified by the function that said it would only use the value as input, is by making a copy and passing the reference to the copy instead.
I'll improve the error message for this.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
type &in => type
type &out => out type or type out
type &inout => type &
There won't be any difference in passing by value or by &in. Behind the scenes the objects will always be passed by reference, and the primitives by value.
To the C++ application there won't be any difference, but in the script language it will be less confusion.
Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game