• Advertisement

Archived

This topic is now archived and is closed to further replies.

implicitly convert to string

This topic is 4975 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

Hi all Is it possible to implicitely convert values (int, float, double) to bstr in angelscript? If not, this would be a nice feature to add in next versions so we could write the following int i=5; float pi=3.14; bstr msg="Hi i="+i+" pi="+pi; I know I could add some conversion functions myselfe, but if it''s already there... regards Tom

Share this post


Link to post
Share on other sites
Advertisement
There is no implicit conversion to bstr. However you can easily add it by registering operator behaviour for the bstr type that take int and bstr (and float, etc).

Here''s a sample function for formatting an int to a bstr:


asBSTR bstrFormatInt(int number)
{
acCString str;
str.Format("%d", number);

// We must allocate a new bstr that the script engine will free afterwards

asBSTR bstr = asBStrAlloc(str.GetLength());

memcpy(bstr, str, str.GetLength());

return bstr;
}


It is a useful feature you ask for, but AngelScript will not come with it built-in. Instead I will release some code with functions for registering useful functions and operators for the bstr type. Version 1.8.0 will even remove the built-in bstr type, so that each application can register their own string type (or use the code I''ll release on the side to register the same bstr type).



__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library - Tower - free puzzle game

Share this post


Link to post
Share on other sites
Thank You for the quick reply.

I have made a class for string conversion with lots of overloaded static functions but i can''t find the correct way of registering them.

class StringConverter{
static asBSTR toString(int i);
static asBSTR toString(float f);
static asBSTR toString(Vector3& v);
// ...
}

I tried registering with:

engine->RegisterGlobalFunction(
"bstr toString(float f)",
asFUNCTION( (asBSTR (StringConverter::toString)(float) ) ),
asCALL_CDECL);

but I always end up with errors

CBindingsAngelScript.h(128) : error C2440: ''type cast'' : cannot convert from ''overloaded-function'' to ''unsigned char *''

I''m simply no function pointer expert

Any help is highly appreciated.

Thanks
Tom

Share this post


Link to post
Share on other sites
Yeah. It''s always the same.
I post something in a forum and 5 minutes later I find the answer myself
So, sorry for the spamming...

In case anyone is interested the correct way of registering overloaded static functions is:

engine->RegisterGlobalFunction(
"bstr toString(float f)",
asFUNCTION( (asBSTR (*)(float)) StringConverter::toString ),
asCALL_CDECL);

engine->RegisterGlobalFunction(
"bstr toString(int i)",
asFUNCTION( (asBSTR (*)(int)) StringConverter::toString ),
asCALL_CDECL);

cheers
Tom

Share this post


Link to post
Share on other sites

  • Advertisement