No conversion from bool to string -- user error or bug?

Started by
3 comments, last by WitchLord 13 years, 8 months ago
I didn't see any mention in the docs about limitations of the bool type, but maybe I missed it. I'm trying to using the Print function included with the tutorial on a bool type but I am getting an error. If I use say uint8 instead, it works fine:

Quote:
script (1, 1) : INFO : Compiling void print_bool(bool)
script (1, 45) : ERROR : No conversion from 'string@&' to math type available.
Build() failed.


Here is a example that shows it:
// Comment out to see expected behavior with uint8#define BUG_BOOL#include "angelscript.h"#include "scriptstring.h"#include <cassert>asIScriptEngine * engine = 0;asIScriptContext * ctx = 0;void Cleanup(){	if(ctx)	{		ctx->Release();		ctx = 0;	}	if(engine)	{		engine->Release();		engine = 0;	}}void MessageCallback(const asSMessageInfo *msg, void *param){	std::string type = "UNKNOWN";	if( msg->type == asMSGTYPE_ERROR )	{		type = "ERROR";	}	else if( msg->type == asMSGTYPE_WARNING ) 	{		type = "WARN";	}	else if( msg->type == asMSGTYPE_INFORMATION ) 	{		type = "INFO";	}	printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type.c_str(), msg->message);}void PrintString(std::string & str){	printf("%s\n", str.c_str());}int main(int argc, char **argv){	int r;	atexit(Cleanup);	engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);	if( engine == 0 )	{		printf("Failed to create script engine.\n");		return -1;	}	r = engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true); assert( r >= 0 );	r = engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL); assert( r >= 0 );	RegisterScriptString(engine);	std::string options = asGetLibraryOptions();	if(options.find("AS_MAX_PORTABILITY") != std::string::npos)	{		printf("This configuration is not being supported. [%s]\n", options.c_str());		return -1;	}	r = engine->RegisterGlobalFunction("void Print(string &in)", asFUNCTION(PrintString), asCALL_CDECL); assert( r >= 0 );#ifdef BUG_BOOL	std::string script = "void print_bool(bool bval) { Print('bval: ' + bval + '\\n'); }";#else	std::string script = "void print_bool(uint8 bval) { Print('bval: ' + bval + '\\n'); }";#endif	asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE);	r = mod->AddScriptSection("script", script.c_str(), script.size());	if( r < 0 ) 	{		printf("AddScriptSection() failed.\n");		return -1;	}	r = mod->Build();	if( r < 0 )	{		printf("Build() failed.\n");		return -1;	}	ctx = engine->CreateContext();	if( ctx == 0 ) 	{		printf("Failed to create the context.\n");		return -1;	}#ifdef BUG_BOOL	int funcId = engine->GetModule(0)->GetFunctionIdByDecl("void print_bool(bool bval)");#else	int funcId = engine->GetModule(0)->GetFunctionIdByDecl("void print_bool(uint8 bval)");#endif	if( funcId < 0 )	{		printf("The function was not found.\n");		return -1;	}	r = ctx->Prepare(funcId);	if( r < 0 ) 	{		printf("Failed to prepare the context.\n");		return -1;	}#ifdef BUG_BOOL	bool val = true;#else	unsigned char val = 1;#endif	ctx->SetArgByte(0, val);	r = ctx->Execute();	if( r != asEXECUTION_FINISHED )	{		if( r == asEXECUTION_ABORTED )		{			printf("The script was aborted before it could finish. Probably it timed out.\n");		}		else if( r == asEXECUTION_EXCEPTION )		{			printf("The script ended with an exception.\n");			int funcID = ctx->GetExceptionFunction();			asIScriptFunction *func = engine->GetFunctionDescriptorById(funcID);			printf("func: %s\n", func->GetDeclaration());			printf("modl: %s\n", func->GetModuleName());			printf("sect: %s\n", func->GetScriptSectionName());			printf("line: %i\n", ctx->GetExceptionLineNumber());			printf("desc: %s\n", ctx->GetExceptionString());		}		else		{			printf("The script ended for some unforeseen reason (%i).\n", r);		}	}	else	{	}	return 0;}


I am on Windows XP, Visual Studio 2008 (w/ SP1), using version 2.19.1 compiling with source as part of the project. Thanks ahead of time for any advice on this issue!
Advertisement
IIRC, this is intended behavior. Unlike C or C++, bool in AngelScript isn't an arithmetic type so the trying to use the integral addition with bool will fail. AngelScript doesn't define a built in string conversion for bool because various applications can have different desired string conversion. For example, developer one would want true to map to "1" and false to map to "0", but developer two wants "true" and "false" and developer three thinks "はい" and "いいえ" makes the most sense. If you want to add a string conversion for bool you can do it the same way that ints or floats are converted in scriptstring.cpp.
SiCrane is correct in his response.

It's neither a bug, nor a user error. What is missing is simply an overloaded operator 'opAdd' for the bool type in the CScriptString add-on.

You can add the following to the CScriptString add-on to add this support if you wish.

static CScriptString *AddStringBool(bool b, const CScriptString &str){	char buf[100];	sprintf(buf, "%s", b ? "true" : "false");	return new CScriptString(str.buffer + buf);}static CScriptString *AddBoolString(bool b, const CScriptString &str){	char buf[100];	sprintf(buf, "%s", b ? "true" : "false");	return new CScriptString(buf + str.buffer);}        // This goes into the RegisterScriptString_Native function	r = engine->RegisterObjectMethod("string", "string@ opAdd(bool) const", asFUNCTION(AddStringBool), asCALL_CDECL_OBJFIRST); assert( r >= 0 );	r = engine->RegisterObjectMethod("string", "string@ opAdd_r(bool) const", asFUNCTION(AddBoolString), asCALL_CDECL_OBJLAST); assert( r >= 0 );

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thank you both SiCrane and WitchLord. I've added the code posted and everything is working as expected now. I was testing out how I could debug various types in scripts when I ran into that problem.

Having the native output of true/false is a lot more convenient in my opinion simply because the bool type only accepts true or false values in the scripts. I see now I could also wrap those bool values in a function that returns a string to accomplish the same thing, but I think having those modifications as part of the scriptstring add-on would be nice for future versions. Cevs could then just go in and simply change the mapping as SiCrane pointed out if they really wanted to without having to re-add the code each future update. The same goes for the generic versions, as adding those is easy too based on the provided code.

Anyways, thanks a lot for the speedy help to both of you, I have one more question but I'll start a new thread for it.
I agree. I'll have this added to the add-on for the next release.

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