OK, I've stopped being lazy and downloaded the latest version of AS to test out the function below and it does work! Here's the basic declarations needed:
engine->RegisterObjectBehaviour("string", asBEHAVE_ASSIGNMENT, "string & f(int ∈)", asFUNCTION(AssignIntToString), asCALL_CDECL_OBJLAST);engine->RegisterGlobalBehaviour(asBEHAVE_ADD, "string f(string ∈, int ∈)", asFUNCTION(AddIntToString), asCALL_CDECL);
and here's the full (messy) code, I just hacked the "testexecutescript.cpp" file to this:
#include <iostream>#include <string>#include <sstream>#include "stdstring.h"#include "utils.h"static asIScriptEngine * engine;std::string scriptCode ="int main()\n""{\n"" string test;\n"" test = \"Hello\";\n"" test = 3;\n"" test = \"Number:\" + 3;\n"" return 0;\n""}";std::string & AssignIntToString(int & i, std::string & dest){ std::ostringstream stream; stream << i; dest = stream.str(); //Assign to string return dest;}std::string AddIntToString(std::string & dest, int & i){ std::ostringstream stream; stream << i; dest += stream.str(); //Add it onto string return dest;}int LoadScript(){ // Give the code to the script engine int r = engine->AddScriptSection(0, "Test", scriptCode.c_str(), scriptCode.size(), 0); if (r < 0) { std::cout << "An error occured while adding the script section." << std::endl; return r; } RegisterStdString(engine); engine->RegisterObjectBehaviour("string", asBEHAVE_ASSIGNMENT, "string & f(int &in)", asFUNCTION(AssignIntToString), asCALL_CDECL_OBJLAST); engine->RegisterGlobalBehaviour(asBEHAVE_ADD, "string f(string &in, int &in)", asFUNCTION(AddIntToString), asCALL_CDECL); return 0;}int CompileScript(){ // Create an output stream that will receive information about the build COutStream out; int r = engine->Build(0, &out); if (r < 0) { std::cout << "Failed to compile script." << std::endl; return -1; } // If we wish to build again, the script sections has to be added again. // Now we will verify the interface of the script functions we wish to call const char * buffer = engine->GetFunctionDeclaration(engine->GetFunctionIDByName(0, "main")); if( buffer == 0 ) { std::cout << "Failed to retrieve declaration of function 'main'." << std::endl; return -1; } return 0;}bool ExecuteScript(){ // Create a context in which the script will be executed. asIScriptContext * ctx; int r = engine->CreateContext(&ctx); if (r < 0) { std::cout << "Failed to create a context." << std::endl; return true; } // Prepare the context for execution r = ctx->Prepare(engine->GetFunctionIDByName(0, "main")); if (r < 0) { std::cout << "Failed to prepare context." << std::endl; return true; } // Execute script r = ctx->Execute(); if (r < 0) { std::cout << "Unexpected error during script execution." << std::endl; return true; } if (r == asEXECUTION_EXCEPTION) { std::cout << "An exception occured during execution." << std::endl; // Print exception description int funcID = ctx->GetExceptionFunction(); std::cout << "func: " << engine->GetFunctionName(funcID) << std::endl; std::cout << "line: " << ctx->GetExceptionLineNumber() << std::endl; std::cout << "desc: " << ctx->GetExceptionString() << std::endl; } ctx->Release(); return false;}int main(){ engine = asCreateScriptEngine(ANGELSCRIPT_VERSION); if (LoadScript() < 0 ) return -1; CompileScript(); ExecuteScript(); engine->Release(); engine = NULL; system("PAUSE"); return 0;}
Like I said, very messy, but it was just to prove that it works.
Happy coding!