Check string in script

I was just fooling around with the toolset, and I have a script where the player puts in a string (a name) ingame. I wanted to check if the player didn’t write anything at all, and if that happens, apply a string. I thought it would work with how I scripted it (see below) but it doesn’t. It compiles but no more than that. As I got a little annoyed since there are still so many things I apparently don’t understand when it comes to scripting I just posted this here to see if you could teach me about this:

void main(string sPlayerAnswer)
{

	if(sPlayerAnswer == "")
	{

	//Some code here

	}

}

I guess my question is: Why can’t you write it like this? Bear in mind that there are other stuff in this script that I’ve not posted here, and if you take that “if statement” out of the question, and the player has written the name (the string), everything works. I just want to know: Why can’t you write it like this? I thought for sure it would work.

Do you have to use some kind of function for this to work like GetGlobalString or GetLocalString?

Thanks.

Hi andgalf,

Here is a tip that may help without knowing the full context of your code …

Rather than check for “”, why not check against a random phrase you know they are highly unlikely to write. I have had to do this in a certain situation, but as I do not know how you are accessing this, the tip may or may not be of any help.

For example …

void main(string sPlayerAnswer = "xygkz11")
{

	if(sPlayerAnswer == "xygkz11")
	{

	//Some code here

	}

}

Now if nothing is passed to sPlayerAnswer, your function will use the default value and fire that bit of code. If, however, the function is passed any other value it won’t fire.

EDIT: Of course, it may even work with sPlayerAnswer = “” in the function, as in …

void main(string sPlayerAnswer = "")
{

	if(sPlayerAnswer == "")
	{

	//Some code here

	}

}

So try this last idea first.

Thanks, Lance.

There is yet another alternative. By definition an empty string should contain no characters. Therefore you should be able to check for an empty string by checking the string length like -

    if(GetStringLength(sPlayerAnswer) < 1)

Why not check for it equalling zero? Well that function is supposed to return -1 if an error occurs so doing it the way that I just did accounts for that too, at least to a certain extent.

TR

1 Like

what method are you using to have the player input a string ? a GUI inputbox ?

here are some notes i made, and a solution

void main(string sSpellId)
{
    // NOTE: GUI sends 'sSpellId' with a space tacked on the end, even if blank.
    // NOTE: GUI considers Escape-key as Okay instead of Cancel.
    // NOTE: 'backoutkey=false' doesn't work in GUI.

    sSpellId = TrimString(sSpellId);
    if (sSpellId != "")
    {
    }
}

// Trims spaces off left and right of sString.
string TrimString(string sString)
{
    while (sString != "" && GetStringLeft(sString, 1) == " ")
        sString = GetStringRight(sString, GetStringLength(sString) - 1);

    while (sString != "" && GetStringRight(sString, 1) == " ")
        sString = GetStringLeft(sString, GetStringLength(sString) - 1);

    return sString;
}

 
in any case, some debug for your situation could look like this:

    SendMessageToPC(GetFirstPC(FALSE), "text length= " + IntToString(GetStringLength(sPlayerAnswer)));

    if (sPlayerAnswer == "")
        SendMessageToPC(GetFirstPC(FALSE), "text is blank");
    else
        SendMessageToPC(GetFirstPC(FALSE), "text is _" + sPlayerAnswer + "_");

note that the underscores above would delimit whitespace (or ‘enhance’ any whitespace on the ends of a string)

Not much we can do if we don’t know how that “sPlayerAnswer” is passed, the problem could be that it’s not being passed at all.

Thank you for the replies! I spoke to my brother about it too, and he said something about that in C++ you can’t “check” or “compare” (don’t know what words he used) a string like the way I did, so I wonder if your method @Lance_Botelle would work…even if I thought it was a nice workaround. And that also makes me question if @kevL_s solution would work, but I know the least of all of us here about these things.

@Tarot_Redhand - Your method sounds (to me) like it would be the most probable of the solutions here, to work. (That was a weird sentence but I can’t come up with a way to say this in english right now, sorry, I hope you still get what I mean)

Yes, it’s a GUI inputbox, or DisplayInputBox is the function’s name in the Script Assist.

Edit: I use this in my module that has begun beta testing now, and also on some new ideas I had for another module that won’t probably ever be released, and I realized that players seldom do what you tell them to do. In the inputbox you are supposed to put in a name and then click on “Okay” or “Cancel”, but I realized that if you write nothing and click “Okay”, when you then call on that string it says “…” or something like that, that’s why I wanted something the makes a default name even if the player do it the wrong way by writing nothing and then click “Okay”. Like a failsafe if you will.

Ok, I just tried the @Tarot_Redhand method but strangely it didn’t work.

I’ll try with the other solutions you guys provided.

Edit: @kevL_s : Your script worked a bit. I managed to see that indeed the string that is put there if you write nothing is something of a blank space. However when I tried it in my script I didn’t quite work, but I may do stuff the wrong way here (here’s the two scripts used for this):

//displaybox_show. I got this script from 4760.

void main()
{
object oPC = GetPCSpeaker();
int nMessageStrRef = -1;
string sMessage = ("Your answer is:");
string sOkCB = "gui_checkanswer";
string sCancelCB = "gui_noanswer";
int bShowCancel = TRUE;
string sScreenName = "";
int nOkStrRef = 181744;
string sOkString = "";
int nCancelStrRef = 181745;
string sCancelString = "";
string sDefaultString = ""; //default text in the message box…

DisplayInputBox(oPC, nMessageStrRef, sMessage, sOkCB, sCancelCB, bShowCancel, sScreenName, nOkStrRef, sOkString, nCancelStrRef, sCancelString, sDefaultString);

}
//gui_checkanswer

#include "ginc_ipspeaker"

// Trims spaces off left and right of sString.
string TrimString(string sString)
{
    while (sString != "" && GetStringLeft(sString, 1) == " ")
        sString = GetStringRight(sString, GetStringLength(sString) - 1);

    while (sString != "" && GetStringRight(sString, 1) == " ")
        sString = GetStringLeft(sString, GetStringLength(sString) - 1);

    return sString;
}

void main(string sPlayerAnswer)
{

 	sPlayerAnswer = TrimString(sPlayerAnswer);
    if (sPlayerAnswer == "")
	{
	
	
	SetCustomToken(1001, "Mike");
	//SetLocalString(GetModule(), "CUSTOM24", sPlayerAnswer); //To be able to call this inside a script and just not just in a conversation by <CUSTOM1001>.
	//According to 4760 (Thierry) one should use numbers above 1000 to be safe that it's not already used by the engine, but I read in NWNlexicon that number 24 wasn't used.
	object oPC = GetPCSpeaker();

	SetGlobalInt("playernamed",1);

	AssignCommand(oPC, ClearAllActions());

	CreateIPSpeaker("thenue", "c_a_intro2", GetLocation(GetFirstPC()), 0.5f);

	
	}

	else
	{
	SetCustomToken(1001, sPlayerAnswer);
	//SetLocalString(GetModule(), "CUSTOM24", sPlayerAnswer); //To be able to call this inside a script and just not just in a conversation by <CUSTOM1001>.
	//According to 4760 (Thierry) one should use numbers above 1000 to be safe that it's not already used by the engine, but I read in NWNlexicon that number 24 wasn't used.
	object oPC = GetPCSpeaker();

	SetGlobalInt("playernamed",1);

	AssignCommand(oPC, ClearAllActions());

	SetCutsceneMode(oPC);

	CreateIPSpeaker("thenueh", "c_a_intro2", GetLocation(GetFirstPC()), 0.5f);
	
	}


}

Apparently, a blank input box returns a space. So…

void main(string sPlayerAnswer)
{
	object oPC = GetFirstPC(FALSE);

	if (sPlayerAnswer == "answer")
	{
		SendMessageToPC(oPC, "correct answer");
	}
	
	else if (sPlayerAnswer == " ")	// <-- keep one space between quotes
	{
		SendMessageToPC(oPC, "nothing was input");
	}
	
	else
	{
		SendMessageToPC(oPC, "wrong answer");
	}
}

And exactly how do you pass an argument into the callback script by using the DisplayInputBox function?
It seems like you can only pass scripts without arguments in there, am I missing something?

I think I already checked doing it like this and that didn’t work.

I’m not sure exactly how it works, only that it does (except when clicking “Okay” and not having written something). To be clear: the gui_checkanswer script looks like this normally:

//gui_checkanswer

#include "ginc_ipspeaker"

void main(string sPlayerAnswer)
{
	
	SetCustomToken(1001, sPlayerAnswer);
	//SetLocalString(GetModule(), "CUSTOM24", sPlayerAnswer); //To be able to call this inside a script and just not just in a conversation by <CUSTOM1001>.
	//According to 4760 (Thierry) one should use numbers above 1000 to be safe that it's not already used by the engine, but I read in NWNlexicon that number 24 wasn't used.
	object oPC = GetPCSpeaker();

	SetGlobalInt("playernamed",1);

	AssignCommand(oPC, ClearAllActions());

	SetCutsceneMode(oPC);

	CreateIPSpeaker("thenueh", "c_a_intro2", GetLocation(GetFirstPC()), 0.5f);


}

Strange. I just tested it again and it worked with the space between the quotes.

I tested your whole script this time, travus, and when I press “Okay” without having anything input nothing at all happens. No message to PC at all. To me it sounds like my brother was correct when he said you can’t do it like this when checking a string in this way. Still, by that logic I really thought that the @Tarot_Redhand method would work.

Here’s the description of the function DisplayInputBox:

//PEH-OEI 05/24/06
//This script function displays a text input box popup on the client of the
//player passed in as the first parameter.
//////
// oPC - The player object of the player to show this message box to
// nMessageStrRef- The STRREF for the Message Box message.
// sMessage - The text to display in the message box. Overrides anything
// - indicated by the nMessageStrRef
// sOkCB - The callback script to call if the user clicks OK, defaults
// - to none. The script name MUST start with ‘gui’
// sCancelCB - The callback script to call if the user clicks Cancel, defaults
// - to none. The script name MUST start with ‘gui’
// bShowCancel - If TRUE, Cancel Button will appear on the message box.
// sScreenName - The GUI SCREEN NAME to use in place of the default message box.
// - The default is SCREEN_STRINGINPUT_MESSAGEBOX
// nOkStrRef - The STRREF to display in the OK button, defaults to OK
// sOkString - The string to show in the OK button. Overrides anything that
// - nOkStrRef indicates if it is not an empty string
// nCancelStrRef - The STRREF to dispaly in the Cancel button, defaults to Cancel.
// sCancelString - The string to display in the Cancel button. Overrides anything
// - that nCancelStrRef indicates if it is anything besides empty string
// sDefaultString- The text that gets copied into the input area,
// - used as a default answer

I can confirm what Travus said, I tested it too now, it adds one empty space before everything you type, for some reason (nope this last part only happens if you type nothing).

Eh…now I don’t follow.

Nevermind, that happens only when you type nothing.
But anyways, if you type nothing and press enter it will return one empty space. As if you have pressed spacebar once.

The string will be " " and not “”.

1 Like

You mean this @Clangeddin? In that case you are correct. The script still doesn’t work though. No message is shown when I test it and just press “Okay”.

I tested it and works:

the script I used for the callback was:

void main(string sAnswer)
{
	FloatingTextStringOnCreature("Answer is: " + sAnswer + ". Length:" + IntToString(GetStringLength(sAnswer)), GetFirstPC(), FALSE);
	if (sAnswer == " ") FloatingTextStringOnCreature("Empty String" + sAnswer, GetFirstPC(), FALSE);
}

I did get the second message saying “Empy String”, so the if was working.

Eh…ok. But your script is quite different (I think) from travus’. I’m getting confused now. Guess I’ll try your script you posted here then.