mohammadali1375
یک شنبه 05 خرداد 1392, 16:48 عصر
سلام.
من تصمیم گرفتم فعلا رو همین angel کار کنم برای اسکریپت. نیازم هم برطرف میکنه. فعلا برای دو بعدی میخام اش استفاده کنم و کلا پروژه هایی که اسکریپت میخاد :لبخندساده:
اولین مشکلی که بهش برخوردم اضافه کردن رشته به اسکریپت هست که خوب توی اولین مثال خود angel اینکارو کرده و نوع داده ای string رو اضافه کرده. من سعی کردم یه کد خیلی ساده تر رو برای تمرین بنویسم از روی اون ولی نمیتونم تاعبعی با ورودی string رو تعریف کنم و خود angel ارور داره. این هم کد خیلی سادش که نمیدونم کجاش اشکال داره :متفکر:
#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[])
{
asIScriptEngine* scriptEngine=asCreateScriptEngine(ANGELSCRIPT_VERS ION);
scriptEngine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
RegisterStdString(scriptEngine);
scriptEngine->RegisterGlobalFunction("void Print(string& text)",asFUNCTION(PrintS),asCALL_CDECL);
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);
scriptModule->AddScriptSection("script",&script[0],script.length());
scriptModule->Build();
asIScriptContext* scriptContext=scriptEngine->CreateContext();
asIScriptFunction* scriptFunction=scriptEngine->GetModule(0)->GetFunctionByDecl("float main()");
scriptContext->Prepare(scriptFunction);
scriptContext->Execute();
float rVal=scriptContext->GetReturnFloat();
cout << rVal << "\n";
system("pause");
return 0;
}
اما با تعریف نکردن تابع Print مشکلی پیش نمیاد حتی میتونم توی خود اسکریپ رشته رو تعریف کنم :
float main()
{
string stVal;
return 2;
}
من تصمیم گرفتم فعلا رو همین angel کار کنم برای اسکریپت. نیازم هم برطرف میکنه. فعلا برای دو بعدی میخام اش استفاده کنم و کلا پروژه هایی که اسکریپت میخاد :لبخندساده:
اولین مشکلی که بهش برخوردم اضافه کردن رشته به اسکریپت هست که خوب توی اولین مثال خود angel اینکارو کرده و نوع داده ای string رو اضافه کرده. من سعی کردم یه کد خیلی ساده تر رو برای تمرین بنویسم از روی اون ولی نمیتونم تاعبعی با ورودی string رو تعریف کنم و خود angel ارور داره. این هم کد خیلی سادش که نمیدونم کجاش اشکال داره :متفکر:
#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[])
{
asIScriptEngine* scriptEngine=asCreateScriptEngine(ANGELSCRIPT_VERS ION);
scriptEngine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
RegisterStdString(scriptEngine);
scriptEngine->RegisterGlobalFunction("void Print(string& text)",asFUNCTION(PrintS),asCALL_CDECL);
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);
scriptModule->AddScriptSection("script",&script[0],script.length());
scriptModule->Build();
asIScriptContext* scriptContext=scriptEngine->CreateContext();
asIScriptFunction* scriptFunction=scriptEngine->GetModule(0)->GetFunctionByDecl("float main()");
scriptContext->Prepare(scriptFunction);
scriptContext->Execute();
float rVal=scriptContext->GetReturnFloat();
cout << rVal << "\n";
system("pause");
return 0;
}
اما با تعریف نکردن تابع Print مشکلی پیش نمیاد حتی میتونم توی خود اسکریپ رشته رو تعریف کنم :
float main()
{
string stVal;
return 2;
}