Hi
I wrote a simple code for use angel script. but it can config the string. where is it problem ?
#include <iostream>
#include <angelscript.h>
#include <scriptstdstring/scriptstdstring.h>
#include <string>
#include <fstream>
using namespace std;
void PrintS(string& text)
{
cout << text;
}
void MessageCallback(const asSMessageInfo *msg, void *param)
{
const char *type = "ERR ";
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, msg->message);
}
int main(int argc,char* argv[])
{
int result;
asIScriptEngine* scriptEngine=asCreateScriptEngine(ANGELSCRIPT_VERSION);
scriptEngine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
RegisterStdString(scriptEngine);
result = scriptEngine->RegisterGlobalFunction("void Print(string &text)",asFUNCTION(PrintS),asCALL_CDECL);
cout << result<< "\n";
string script;
string newLine;
fstream file("script.as",std::ios_base::in);
while (!file.eof())
{
getline(file,newLine);
script+=newLine+="\n";
}
asIScriptModule* scriptModule=scriptEngine->GetModule(0,asGM_ALWAYS_CREATE);
result = scriptModule->AddScriptSection("script",&script[0],script.length());
cout << result<< "\n";
result = scriptModule->Build();
cout << result<< "\n";
asIScriptContext* scriptContext=scriptEngine->CreateContext();
asIScriptFunction* scriptFunction=scriptEngine->GetModule(0)->GetFunctionByDecl("float main()");
result = scriptContext->Prepare(scriptFunction);
cout << result<< "\n";
result = scriptContext->Execute();
cout << result<< "\n";
float rVal=scriptContext->GetReturnFloat();
cout << rVal << "\n";
system("pause");
return 0;
}
but it work :
#include <iostream>
#include <angelscript.h>
#include <scriptstdstring/scriptstdstring.h>
#include <string>
#include <fstream>
using namespace std;
void PrintS(string& text)
{
cout << text;
}
void MessageCallback(const asSMessageInfo *msg, void *param)
{
const char *type = "ERR ";
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, msg->message);
}
int main(int argc,char* argv[])
{
int result;
asIScriptEngine* scriptEngine=asCreateScriptEngine(ANGELSCRIPT_VERSION);
scriptEngine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
RegisterStdString(scriptEngine);
cout << result<< "\n";
string script;
string newLine;
fstream file("script.as",std::ios_base::in);
while (!file.eof())
{
getline(file,newLine);
script+=newLine+="\n";
}
asIScriptModule* scriptModule=scriptEngine->GetModule(0,asGM_ALWAYS_CREATE);
result = scriptModule->AddScriptSection("script",&script[0],script.length());
cout << result<< "\n";
result = scriptModule->Build();
cout << result<< "\n";
asIScriptContext* scriptContext=scriptEngine->CreateContext();
asIScriptFunction* scriptFunction=scriptEngine->GetModule(0)->GetFunctionByDecl("float main()");
result = scriptContext->Prepare(scriptFunction);
cout << result<< "\n";
result = scriptContext->Execute();
cout << result<< "\n";
float rVal=scriptContext->GetReturnFloat();
cout << rVal << "\n";
system("pause");
return 0;
}