Sign in to follow this  

Passing/comparing strings

This topic is 2551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 &in str)
{
if(str == "test") {}
}

What I need to do in order to use operators on strings ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 2551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this