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
implicitly convert to string
Started by zola, Jun 06 2004 07:17 AM
3 replies to this topic
Ad:
#2 Moderators - Reputation: 2309
Posted 06 June 2004 - 08:04 AM
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:
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
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
#3 Members - Reputation: 122
Posted 06 June 2004 - 09:08 AM
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
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
#4 Members - Reputation: 122
Posted 06 June 2004 - 09:25 AM
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
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






