Sign in to follow this  
blubberbernd

Pointers, references, values between c++ and AngelScript

Recommended Posts

Hi,

I already managed to pass a string and a string array to AS from my c++ code, but I'm not sure if I did it right (I think I passed as value from c++ to as).
[code]
//AS code
void bar(string str, string[] strArr){}

//in C++ code
std::string str = "foo";

CScriptArray* strArr;
//initilaise array here

SetArgObject(0, str);
SetArgObject(1, strArr)
[/code]

Can someone give me a short overview of all the pointer, reference, @, *, &, in, out, inout operators and a short example when to use which in both languages when passing variables between the two (C++ and AS)? And how to pass variables if I only can acces them through a const reference (const string&)?
I am especially interested in string and array types.

I find this whole passing of variables confusing. :D

Share this post


Link to post
Share on other sites
It can indeed be a bit confusing. Here's a few tips on how to think of the differences:

In C++ a pointer and a reference is essentially the same thing, in fact in the memory they are represented exactly the same way, i.e. as an address to something. The only difference (besides syntax) is that a pointer can be null, but a reference cannot (well, it can, but is not supposed to be).

A reference in AngelScript is an exact match to the C++ reference. The 'in', 'out', and 'inout' keywords added in the AngelScript syntax tells the AngelScript compiler how the value pointed to by the reference will be used. An 'in' reference is used as an input parameter to the function, i.e. the function will read from the reference but will not update it. An 'out' reference is contrary, the parameter will be updated by the function to return a value to the calling function. To C++ these keywords don't mean anything, but they are very important to AngelScript in order to keep the script from doing things it mustn't do (sandboxing).

An object handle in AngelScript is represented in memory as an ordinary C++ pointer, but the object it points to is a reference counted object. When a C++ function receives a handle from a script in a parameter it is responsible for releasing that reference when it is done with it. Likewise, when a C++ function returns a handle to the script it must make sure to increment the reference count.

If a C++ function takes a parameter as 'const string &' then you will want to register it as 'const string &in'. If the C++ function takes a 'string &' and is really meant to be an output parameter then you want to register it as 'string &out'. If the C++ function takes a 'string &' and uses the parameter both as input argument and output argument, then you will need to implement a wrapper function as it is not allowed to register a value type as 'string &inout' (because it isn't possible to always guarantee the lifetime of the reference in this case).

For reference types, like the array, you will usually use either @ or &. Use @ if it is a valid scenario to have null instead of an object, and & if it is not. If you do not use either of the two, it means that you're passing the object by value, which makes the AngelScript compiler make a copy of the object (which can be hurtful to the performance if the object is very complex).

Here's a pair of articles of interest in the manual:

[url="http://www.angelcode.com/angelscript/sdk/docs/manual/doc_as_vs_cpp_types.html"]Understanding datatypes in AngelScript and C++[/url]
[url="http://www.angelcode.com/angelscript/sdk/docs/manual/doc_obj_handle.html"]Understanding object handles[/url]

I hope that helps to clarify things a bít.

Share this post


Link to post
Share on other sites
Thank you very much, this already helps a lot. :)

Just so that i got it right:
In Angelscript code I can NEVER have a reference "type&" like in c++? I can only have "type &in" (or &out, &inout)?
And AngelScript code does NOT support the asterisk pointer (*), only the "@", which works basically the same?

And a new question:
When should I use SetArgObject and when SetArgAddress? Do they HAVE TO match the function declaration in the AS code (like Object for value types and Address for &in or @type)?

Share this post


Link to post
Share on other sites
[quote name='blubberbernd' timestamp='1313586915' post='4850299']
Thank you very much, this already helps a lot. :)

Just so that i got it right:
In Angelscript code I can NEVER have a reference "type&" like in c++? I can only have "type &in"  (or &out, &inout)?
And AngelScript code does NOT support the asterisk pointer (*), only the "@", which works basically the same?

And a new question:
When should I use SetArgObject and when SetArgAddress? Do they HAVE TO match the function declaration in the AS code (like Object for value types and Address for &in or @type)?
[/quote]


type& is allowed for reference types. It is equivalent to type&inout. If you set the engine property asEP_ALLOW_UNSAFE_REFERENCES like SiCrane mentioned, type& will also be allowed for value types and primitives, but as the name of the property suggests, this is unsafe and should not be used unless you don't care about the safety of your application.

Correct. AngelScript doesn't support the asterisk pointer (*). That tool is not safe in the hands of inexperienced script writers. :)

[url="http://www.angelcode.com/angelscript/sdk/docs/manual/classas_i_script_context.html#a8de8f21dfbb2cdf0becbabaa3e883ed"]SetArgAddress[/url] can be used for both references and handles. For handles it will not automatically increment the object reference. [url="http://www.angelcode.com/angelscript/sdk/docs/manual/classas_i_script_context.html#09044a12dfb2d44d19bd8a4025cb814d"]SetArgObject[/url] can be used for handles, references to objects, and objects sent by value. For handles it will automatically increment the reference, and for objects sent by value it will make a copy of the object. Usually you'll want to use SetArgAddress for references, and SetArgObject for handles and objects sent by value.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this