Hi,
I'm having some problems with strings. I'm using this code:
void print_str(std::string msg)
{
// std::cout << msg << std::endl;
printf("%s\n", msg.c_str());
}
void print_int(int i)
{
// std::cout << i << std::endl;
printf("%d\n", i);
}
const char * script =
" "
" void do_test() "
" { "
" int i = 42; "
" print_int(i); "
" "
" string msg = \"Hello, World!\"; "
" print_str(msg); "
" } "
" ";
int main()
{
asIScriptEngine * engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
RegisterStdString(engine);
engine->RegisterGlobalFunction("void print_str(string msg)", asFUNCTION(print_str), asCALL_CDECL);
engine->RegisterGlobalFunction("void print_int(int i)", asFUNCTION(print_int), asCALL_CDECL);
OutStream out;
engine->AddScriptSection(0, "name", script, (int)strlen(script), 0);
engine->Build(0, &out);
asIScriptContext *ctx;
engine->CreateContext(&ctx);
ctx->Prepare(engine->GetFunctionIDByDecl(0, "void do_test()"));
int r = ctx->Execute();
if (r != 0)
{
if (r == asEXECUTION_EXCEPTION)
{
printf("Script exception\n");
printf("Func: %s\n", engine->GetFunctionName(ctx->GetExceptionFunction()));
printf("Line: %d\n", ctx->GetExceptionLineNumber());
printf("Desc: %s\n", ctx->GetExceptionString());
}
}
return 0;
}
Which prints the int as expected, but doesn't print the string. I put a breakpoint in print_str and msg is empty. Any ideas?
Secondly, I want to be able to convert between the math types and strings in script as if it were a typeless language.
I think to do this I just need to register asBEHAVE_ASSIGNMENT functions for "string &f(int ∈)" (and other types as needed), and then implement the conversion function as something like:
string IntToString(int i)
{
stringstream stream;
stream << i;
string str;
stream >> str;
return str;
}
Am I on the right track? Have I missed some existing functionality that does this automatically or provides a simpler way?
Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour