Aller au contenu

Photo

Local String issue - How do I reset/clear a local string on the PC?


  • Veuillez vous connecter pour répondre
8 réponses à ce sujet

#1
Darin

Darin
  • Members
  • 282 messages
I have a puzzle that's fairly involved, but it ends up that you select letters from a list until you have the "word" you want to say, then you go for it.

If the selected letters are correct, you succeed
If not, you fail.

when the conversation opens, I run:

void main()
{
object oPC = GetPCSpeaker();
DeleteLocalString(oPC,"sSpeak");
SetLocalString(oPC,"sSpeak","");
string sWords = GetLocalString(oPC,"sSpeak");
SetCustomToken(123, sWords);
}


Then, every time you select a letter, it appends as sAdd (exaple, if you pick "A" sAdd = A) in the following:

void main(string sAdd)
{
object oPC = GetPCSpeaker();
string sOld = GetLocalString(oPC, "sSpeak");
string sNew = sOld+sAdd;
SetLocalString(oPC, "sSpeak", sNew);
string sWords = GetLocalString(oPC,"sSpeak");
SetCustomToken(123, sWords);
}


When you select to stop adding and just say it already, it checks to see if it ios correct via TestStringAgainstPattern("THISISCORRECTPATTERN",sWords) where sWords was found by string sWords = GetLocalString(oPC,"sSpeak");

My question is this:
How do I reset  the LocalString sSpeak back to empty if the player fails and wants to try again?  every time I run the conversation and get it wrong (I typically just hit the letter N and move on) when I restart the conversation <CUSTOM123> shows up as "N" instead of "".

#2
M. Rieder

M. Rieder
  • Members
  • 2 530 messages
Could you post the script that runs if you don't get the right pattern?

Maybe the problem is that <CUSTOM123> is not being updated when you clear the string. Maybe the string is being cleared, but the token does not reflect this. You could use FloatingTextStringOnCreature(sSpeak,GetFirstPC()); to check sSpeak and see if it is the local string or the custom token that is the problem.

#3
Darin

Darin
  • Members
  • 282 messages
I thought about the custom being the issue as I left for the Gym, i'll give that a shot, thank you.

#4
painofdungeoneternal

painofdungeoneternal
  • Members
  • 1 799 messages
use

SetLocalString(oPC, "sSpeak", "");

or

DeleteLocalString( oPC, "sSpeak");

The first is probably more efficient as you will be reusing it anyway. You could use that every time you test against pattern, or create a custom lever or somesuch you can use with that when you want to reset it.

I'd be sending debug messages showing the contents of all inputs and variables --

// oPC is already defined as GetPCSpeaker();
SendMessageToPC( oPC, "sSpeak="+GetLocalString(oPC, "sSpeak")+" sOld="+sOld+" sNew="+sNew+" sWords="+sWords+" sAdd="+sAdd );

That shows how to combine inputs and give you some decent output, showing both using variables and functions depending on the situation and should work in your void main( string sAdd).

Note that you can use other functions besides TestStringAgainstPattern which is designed for regular expressions, well it is kind of like regular expressions. An example is as follows of an alternative way to find if sFind is in sString. I am pretty sure you need some ** around the string to allow wildcards, but i've never seen anything i really needed to use that function for except validation of strings to remove illegal characters ( basically dealing with hacker exploits ).
int nPosition = FindSubString(sString, sFind); // if not found returns -1, otherwise returns 0 if it's the first character
if (nPosition != -1)
{ 
 this means it was found
}
else
{
this means not found
}

and of course testing if they are the same


// some thing I rarely use, but it's a wrapper on the C++ function that does this
//* Returns 0 if the strings are the same
//* Returns a negative value if string 1 is less than string 2
//* Returns a positive value if string 1 is greater than string 2
// parameter allows it to ignore case
if ( StringCompare( sPrevious,sCurrent, FALSE ) > 0 ) 
{

}

or even ( if you add in some uppercase to the inputs this generally works well )
if ( sPrevious == sCurrent )


#5
Darin

Darin
  • Members
  • 282 messages
Wow, I'm an idiot, apparently the first script wasn't set to run on the opening of the Conversation, so it was just adding and checking, like it's supposed to...without the initialization. Sorry to waste space on the forums, and thank you for the responses.

#6
Morbane

Morbane
  • Members
  • 1 883 messages
Just for future reference you can just reset the local string/int (whatever) to a value that is not in use or will not ever be used in your module.

#7
M. Rieder

M. Rieder
  • Members
  • 2 530 messages

EpicFetus wrote...

Wow, I'm an idiot, apparently the first script wasn't set to run on the opening of the Conversation, so it was just adding and checking, like it's supposed to...without the initialization. Sorry to waste space on the forums, and thank you for the responses.



No problem.  I'm always forgetting to call my scripts from the conversations where they belong.  I've spent hours debugging scripts only to find that it was the convo.  Lots of fun, eh?

#8
Morbane

Morbane
  • Members
  • 1 883 messages

M. Rieder wrote...

EpicFetus wrote...

Wow, I'm an idiot, apparently the first script wasn't set to run on the opening of the Conversation, so it was just adding and checking, like it's supposed to...without the initialization. Sorry to waste space on the forums, and thank you for the responses.



No problem.  I'm always forgetting to call my scripts from the conversations where they belong.  I've spent hours debugging scripts only to find that it was the convo.  Lots of fun, eh?


I always get a rise out of finding a solution  even if it is founded in a overlooked tidbit or in hindsight. B)

#9
painofdungeoneternal

painofdungeoneternal
  • Members
  • 1 799 messages
Add a debugging mode to your module - ie a variable named debugging. Add to the top of all your scripts some code like the following.

if ( DEBUGGING > 5 )
{
sendmessage etc
}


in global scope ( in the include and not in the main() at all ) have a line like, this is done very rarely, mainly in the AI ( and will have major bugs in the vanilla compiler according to skywing. )

int DEBUGGING = GetLocalInt( GetModule(), "debugging");

or

int DEBUGGING = 6; // second way you have to recompile things, but it has less overhead

Then when you work on a module and set up things, you can just add a var to your module ( i use a chat command to do it myself, you will see something extensively done like that in the CSL library ) and all your scripts will echo out lots of useful information.

Pretty much eliminates your not being sure if the script is running or not, of course just doing the sendmessage first thing when you have an issue would also spot it, but i'd imagine its helpful when setting up convos to have feedback across the board.