ورود

View Full Version : سوال: اضافه کردن رشته به angel script



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;
}

mohammadali1375
یک شنبه 05 خرداد 1392, 16:52 عصر
مشکل رو تا حدودی پیدا کردم اما هنوز توی مثال خود 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;

}

mohammadali1375
یک شنبه 05 خرداد 1392, 17:44 عصر
آخ آخ عجب اشتباهی. نمیدونستم بجا نام متغیر باید نوعش ( ارجعی یا .... رو مشخص کنیم )
ReferenceDescription &inA copy of the value is always taken and the reference to the copy is passed to the function. For script functions this is not useful, but it is maintained for compatibility with application registered functions. const &inIf the life time of the value can be guaranteed to be valid during the execution of the function, the reference to the true object is passed to the function, otherwise a copy is made. &outA reference to an unitialized value is passed to the function. When the function returns the value is copied to the true reference. The argument expression is evaluated only after the function call. This is the best way to have functions return multiple values. const &outUseless as the function wouldn't be able to modify the value. &inoutThe true reference is always passed to the function. If the life time of the value cannot be guaranteed, a compile time error is generated and the script writer will have to copy the value to a local variable first. Objects that support object handles are best suitable for this type as they can always be guaranteed. const &inoutThe reference cannot be changed by the function.