Advertisement

Passing/comparing strings

Started by December 22, 2010 10:00 AM
4 comments, last by solenoidz 13 years, 11 months ago
I know it's a very basic question, but here it is anyway.
I need to pass a string as an argument to a script function and compare that argument inside the function. I'm registering a type with RegisterStdString();
Am I suppposed to use setArgObj(..) to pass the string, and how my script function should be declared in order this to work ?
Can I use something like this ?

//script function
void CompareStr(string& str)
{
if(str == "foo")
{
//do something
}

}


Am I on the right track here, or I'm just doing it wrong ?
Thanks for your time.
You'd probably want to use an input reference (string & in str) rather than an unsafe reference (string & str), but you seem on the right track.
Advertisement
Thanks.
I'm asking, because I'm getting a compile error, stating :

ERR : No matching operator that takes the types 'string&' and 'string&' found.

And then

ERR : Expression must be of boolean type.

When I try to compare the strings in the script like so :
void CompareStr(string ∈ str)
{
if(str == "test") {}
}

What I need to do in order to use operators on strings ?
You shouldn't have to do anything special. The == operator is supported through the opEquals method registered for the string type by RegisterStdString.

I just tested this to make sure it works and didn't have any trouble with the latest version. What version of AngelScript are you using?



		engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);		engine->SetMessageCallback(asMETHOD(COutStream,Callback), &out, asCALL_THISCALL);		RegisterStdString(engine);		mod = engine->GetModule(0, asGM_ALWAYS_CREATE);		mod->AddScriptSection("mod", 			"void func(string &in s) \n"			"{ \n"			"  if( s == 'test' ) \n"			"  { \n"			"  } \n"			"} \n");		if( mod->Build() < 0 )			TEST_FAILED;		engine->Release();


If the function takes the string by reference, you should use asIScriptContext::SetArgAddress to pass the address of the string to the function.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thank you for the answer.
I'm using version "2.16.0" and just pasted the
module->AddScriptSection("mod", 			"void func(string &in s) \n"			"{ \n"			"  if( s == 'test' ) \n"			"  { \n"			"  } \n"			"} \n");


just before building of the module and that failed too with the same messages.
Probably I need to upgrade to the latest version. Will keep you in touch. [smile]
Despite of the warning that it has problems, I just upgraded to the latest "2.20.1" version and all is working just fine!
Thanks again.

This topic is closed to new replies.

Advertisement