Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I wrote a simple code for use angel script. but it can config the string. where is it problem ?

C++
#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 :

C++
#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;

}
Posted
Updated 26-May-13 2:28am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900