Advertisement Jump to content
Sign in to follow this  

Returning from AS ( Comprehension Question )

This topic is 2491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, it's me again.
Most likely my question is easy to solve again, but i got hung there for 2 days now. Maybe i searched google and the forum for the wrong key words?

Well: The alst few days i was wrapping my head around AngelScript as good as possible. Now i have the problem that i want a generic method to receive return Values from Angelscript.

It looks like that:

template<class T> T getReturnG() {
asIScriptObject* sObj=*(asIScriptObject**)scriptContext->GetReturnObject();
T ent=(T)sObj;
return ent;

//Some specis for it.
template<> int getReturnG() {
return (int)scriptContext->GetReturnDWord();

template<> float getReturnG() {
return scriptContext->GetReturnFloat();
template<> double getReturnG() {
return scriptContext->GetReturnDouble();

It works fine with reference types, float, int etc.
But when i try to receive an std::string i get an error. It's a value type so i guess it has something to do with that?
( I registered strings with registerStdString() btw)
The compiler tells me, that asIScriptObject* can't be converted to std::string.

I got no idea how to retrieve value types with this convention. Any ideas?

Sorry, if the question is kinda dumb... ^^"

Edit: Oh i forgot:

Thats how i retrieve the return value.
std::string str=sys->getReturnG<std::string>();

Thats how i registered the function:
"string getString()"

Share this post

Link to post
Share on other sites
GetReturnObject() returns a void * to the return object, you don't need to cast it to a script object pointer unless it's actually of script object type. For a std::string, you would just cast it to a std::string pointer.

Share this post

Link to post
Share on other sites
Okay. Worked like a charm >_>
Thanks for the fast reply. I will go and learn some pointer arithmetics now i guess...

Share this post

Link to post
Share on other sites
There are multiple ways, and you may choose which is best for you.

string *str1 = reinterpret_cast<string*>(ctx->GetReturnObject());
string *str2 = reinterpret_cast<string*>(ctx->GetReturnAddress());
string *str3 = reinterpret_cast<string*>(ctx->GetAddressOfReturnValue());

There are subtle differences that depends on what exactly the function is returning, but for functions that return a value type by value they all give the same result.

As you're trying to write a template function to retrieve the return value I suggest you use the GetAddressOfReturnValue() method as it will always return a pointer to the value which can be cast to the expected type, e.g.

// A function declared as int func();
int value = *reinterpret_cast<int*>(ctx->GetAddressOfReturnValue());
// A function declared as float func();
float value = *reinterpret_cast<float*>(ctx->GetAddressOfReturnValue());
// A function declared as string func();
string value = *reinterpret_cast<string*>(ctx->GetAddressOfReturnValue());
// A function declared as obj @func(); where obj is a script class
asIScriptObject *obj = *reinterpret_cast<asIScriptObject**>(ctx->GetAddressOfReturnValue());

Share this post

Link to post
Share on other sites
Is there a special reason to use reinterpret_cast over my solution? Something like a speed improvement or similar?
I read about it a bit and it seems as unsafe as the other solutions i used... So i was just wondering. :)

Share this post

Link to post
Share on other sites
Not really, the result is exactly the same.

reinterpret_cast is the C++ style, and you're using the original C style cast.

C++ defined specific rules for each type of cast, i.e. const_cast, static_cast, reinterpret_cast, and dynamic_cast, where as C did everything in a single way (except the dynamic_cast that is not available in C as far as I know). In C++ each cast has restrictions and the compiler will not allow you to do a cast that is not supported by the form you used. With the C style cast all types of casts are allowed, so the compiler cannot really know which you really wanted, it will simply evaluate each of them and use the one that is possible.

To tell you the truth, I frequently use the C style cast myself out of old habit and don't really have any problems with it since I know how it works, but I'm trying to re-educate myself to use C++ style casts as they are safer. Especially with code where there are multiple programmers of varying level of expertise maintaining it.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!