passing pointer to angelscript
Is there a preferred way of passing a pointer to a struct from C to angelscript. I am casting pointer to int then passing it. I can also store them in an array and pass the index to AS.
If you store the pointer in a basic int, how do you keep the script from accidentally (or even deliberately) changing its value and then doing something with it that may crash the application?
Or perhaps you don't worry about the safety since you have full control over who is writing the scripts. If this is the case, then storing the pointers as integers is just fine.
For myself, I would create a container class (similar to shared pointers) and register that with the script engine. That way I would be able to prevent the script from altering the pointer, and I would also be able to keep track of the number of references for memory management, so I don't accidentally delete the object while there are still pointers to it.
Regards,
Andreas
Or perhaps you don't worry about the safety since you have full control over who is writing the scripts. If this is the case, then storing the pointers as integers is just fine.
For myself, I would create a container class (similar to shared pointers) and register that with the script engine. That way I would be able to prevent the script from altering the pointer, and I would also be able to keep track of the number of references for memory management, so I don't accidentally delete the object while there are still pointers to it.
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
Thanks. For now I am the only who is writing the scripts. But seeing this can go very wrong, I will use a container.
why can't I use "int &inout"? :/
I want to bind a function like
int doStuffTo(int &val){ val++; return 1;}
do I need a function like
int doStuff2(int inVal, int &outVal)
{ doStuffTo(inVal); outVal=inVal; }
I want to bind a function like
int doStuffTo(int &val){ val++; return 1;}
do I need a function like
int doStuff2(int inVal, int &outVal)
{ doStuffTo(inVal); outVal=inVal; }
This is due to a safety issue.
&inout is only valid for reference types, where AngelScript can guarantee the validity of the reference for the duration of the call by storing a handle to the object.
For 'int' there is no way AngelScript can guarantee that the reference is valid at all time, e.g. if the function resizes an array and the reference was to one of the elements of that array then the reference would no longer be valid.
Note, the same problem can happen in C++, but in C++ the programmer assumes all responsibility to make sure the code is written properly. With scripting its often not possible nor desireable to put that responsibility in the hands of the script writer.
If you are not concerned about the safety issue, you can turn off this control with the following call: engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);.
Regards,
Andreas
&inout is only valid for reference types, where AngelScript can guarantee the validity of the reference for the duration of the call by storing a handle to the object.
For 'int' there is no way AngelScript can guarantee that the reference is valid at all time, e.g. if the function resizes an array and the reference was to one of the elements of that array then the reference would no longer be valid.
Note, the same problem can happen in C++, but in C++ the programmer assumes all responsibility to make sure the code is written properly. With scripting its often not possible nor desireable to put that responsibility in the hands of the script writer.
If you are not concerned about the safety issue, you can turn off this control with the following call: engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);.
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement