Jump to content

User defined variables in a string?


GTASkinCentral
 Share

You are about to reply to a thread that has been inactive for 3426 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hello! I'm in the process of writing an advanced tip jar script and wondered if there is any way to use some kind of 'find and replace' feature. I was hoping for the user to input their own variables into a string like this:

 

string p_Msg = "%name% has just donated %tip% to help us out!";



I've been experimenting with llSubStringIndex(string,variable) however have no way to substitute a certain value for that variable.

Here is the script in progress, feel free to modify and re-post here with suggestions!

Thank you!

 

list    pay_buttons = [1,5,10,25];

string  title     = "Example Tip Jar";

integer p_say     = FALSE;
integer p_shout   = TRUE ;
integer p_whisper = FALSE;
string  p_Msg     = "%name% has just donated %tip% to help us out!";

integer IM_Giver   = TRUE ;
string  IM_G_Msg   = "Thank you for donating!";

string  name;
integer tip;
integer total;

text()
{
    llSetText(title + "\nTotal Tips: L$" + (string)total,<1,1,1>,1);
}

default
{
    
    state_entry()
    {
        total = 0;
        tip   = 0;
        name  = "";

        text();
        
        llSetPayPrice(0,pay_buttons);
    }
    
    on_rez(integer start_param)
    {
        llResetScript();
    }
    
    money(key giver, integer amount)
    {
        name   = llKey2Name(giver);
        tip    = amount;
        total += amount;

        text();

        if(IM_Giver)
        {
            llInstantMessage(giver,IM_G_Msg);
        }
        
        if(p_say)
        {
            llSay(0,p_Msg);
        }

        if(p_shout)
        {
            llShout(0,p_Msg);
        }

        if(p_whisper)
        {
            llWhisper(0,p_Msg);
        }
        
    }
}

 

Link to comment
Share on other sites

Does it have to be anything more complicated than this ?

string p_Msg (string name, integer amt){     return( name + " has just donated L$" + (string)amt + " to help us out!");}

 Call it from your script with

llSay(0,p_Msg(name, tip));

Or maybe I'm missing the point?

  • Like 1
Link to comment
Share on other sites

Oh, I see.  The owner gets to design a template that will appear in the end user's system.  That's more complicated.  Maybe this will work (off the top of my head ....)

string p_Msg (string template, string name){    string output;    integer idot = llSubStringIndex(name,".");    string First = llGetSubString(name,0,idot-1);    string Last = llGetSubString(name,idot+1,-1);    string Whole = First + " " + Last;    list Placeholders = ["%NAME%","%FIRST_NAME%","%LAST_NAME%"];    integer idx;    integer i;    while ( i < 3)    {        string Token = llList2String(Placeholders,i);        if ( ~( idx = llSubStringIndex(template,Token) ) )        {            output = llGetSubString(template,0,idx-1) + llList2String([Whole,First,Last],i) + llGetSubString(template,idx+llStringLength(Token)+1,-1);        }        else        {            output = template;        }        ++i;    }    return output;}

 So, the string variable template contains the value that is input from your TextBox..

llTextBox(llGetOwner," \n Type your user template here, using %NAME%,%FIRST_NAME%, or %LAST_NAME% as possible placeholders",iChannel);

 Then, elsewhere in your script, when the end user activates the money event to leave a tip, you call the p_Msg routine with

llSay(0,p_Msg(template,llGetUsername(id));

You'd have to make everything more complicated to accept a Display Name, but the same generalk approach would work, I think.

 EDIT:  Oh, I forgot about the tip.  You can stuff that into the message easily enough.  That's not the hard part.  :smileywink:

DOUBLE EDIT: Heh.  I also mixed up the order of names in my ouput calculation -- I told you it was off the top of my head --- so I just fixed it above.

           

--||-
  • Like 1
Link to comment
Share on other sites

Something like this?

list    pay_buttons = [1,5,10,25];string  title     = "Example Tip Jar";integer p_say     = TRUE;integer p_shout   = FALSE ;integer p_whisper = FALSE;string  p_Msg     = "%name% has just donated %tip% L$ to help us out!";integer IM_Giver   = TRUE ;string  IM_G_Msg   = "Thank you for donating!";string  name;integer tip;integer total;text(){    llSetText(title + "\nTotal Tips: L$" + (string)total,<1,1,1>,1);}default{        state_entry()    {        total = 0;        tip   = 0;        name  = "";        text();                llSetPayPrice(0,pay_buttons);        llSetClickAction(CLICK_ACTION_PAY);    }        on_rez(integer start_param)    {        llResetScript();    }        money(key giver, integer amount)    {        name   = llGetDisplayName(giver);        tip    = amount;        total += amount;                string tempstring = p_Msg;        list templist = llParseString2List(p_Msg, ["%name%"], []);          if (llSubStringIndex(p_Msg, "%name%") == 0) {            p_Msg = name + llList2String(templist, 0);        }        else {            p_Msg = llList2String(templist, 0) + name + llList2String(templist, 1);        }                    templist = llParseString2List(p_Msg, ["%tip%"], []);          if (llSubStringIndex(p_Msg, "%tip%") == 0) {            p_Msg = (string)tip + llList2String(templist, 0);        }        else {            p_Msg = llList2String(templist, 0) + (string)tip + llList2String(templist, 1);        }        text();        if(IM_Giver)        {            llInstantMessage(giver,IM_G_Msg);        }                if(p_say)        {            llSay(0,p_Msg);        }        if(p_shout)        {            llShout(0,p_Msg);        }        if(p_whisper)        {            llWhisper(0,p_Msg);        }                p_Msg = tempstring;          }}

 

  • Like 1
Link to comment
Share on other sites

Thanks to you both! This is what I have now...

What if the user does not enter %name% or %tip% into the string, any way to make it not show?

I'm great with scripting, just not with parsing strings lol...

// General Optionsstring  title       = "Example Tip Jar"; // Tip jar name.list    pay_buttons = [ 1, 5, 10, 25 ];  // Pay buttons.// Local Speech Settings// Use %name% for the tipper's name and %tip% for the tip amount.integer p_say     = TRUE;  // Say the tip message in local chat (TRUE or FALSE).integer p_shout   = FALSE; // Shout the tip message in local chat (TRUE or FALSE).integer p_whisper = FALSE; // Whisper the tip message in local chat (TRUE or FALSE).string  p_Msg     = "%name% has just donated %tip% to help us out!"; // Tip message to be said in local chat.// IM Tipper Optionsinteger IM_Giver   = TRUE ; // IM the tipper (TRUE or FALSE).string  IM_G_Msg   = "Thank you for donating!"; // Tipper IM message.// Hovertext Optionsinteger show_text      = TRUE; // Show hovertext (TRUE or FALSE).integer show_last_tip  = TRUE; // Show last tipper (TRUE or FALSE).vector  text_col       = <1, 0.8, 0>; // Text color in RGB (LSL) format.// Tip Variablesstring  name;  // Last tipper's name.integer tip;   // Last tip.integer total; // Total tips.text(){    if (show_text)    if (show_last_tip)    llSetText(title + "\n \nTotal Tips: L$" + (string)total + "\n \n Last Tip: L$" + (string)tip + "\nby " + name ,text_col,1);    else    llSetText(title + "\n \nTotal Tips: L$" + (string)total,text_col,1);    else    llSetText("",<0,0,0>,0);}default{    state_entry()    {        total = 0;        tip   = 0;        name  = "Nobody";        text();        llSetClickAction(CLICK_ACTION_PAY);        llSetPayPrice(0,pay_buttons);    }        on_rez(integer start_param)    {        llResetScript();    }        money(key giver, integer amount)    {        name   = llKey2Name(giver);        tip    = amount;        total += amount;        text();                string tempstring = p_Msg;        list templist = llParseString2List(p_Msg, ["%name%"], []);                  if (llSubStringIndex(p_Msg, "%name%") == 0) { p_Msg = name + llList2String(templist, 0); }        else { p_Msg = llList2String(templist, 0) + llList2String(templist, 1); }                    templist = llParseString2List(p_Msg, ["%tip%"], []);                  if (llSubStringIndex(p_Msg, "%tip%") == 0) { p_Msg = (string)tip + "L$" + llList2String(templist, 0); }        else { p_Msg = llList2String(templist, 0) + (string)tip + "L$" + llList2String(templist, 1); }                if(p_say)     { llSay    (0, p_Msg); }        if(p_shout)   { llShout  (0, p_Msg); }        if(p_whisper) { llWhisper(0, p_Msg); }        if(IM_Giver)  { llInstantMessage(giver,IM_G_Msg); }    }}

 

Link to comment
Share on other sites

    money(key giver, integer amount)    {        name   = llGetDisplayName(giver);        tip    = amount;        total += amount;                string tempstring = p_Msg;        list templist;          if (llSubStringIndex(p_Msg, "%name%") != -1) {            templist = llParseString2List(p_Msg, ["%name%"], []);             if (llSubStringIndex(p_Msg, "%name%") == 0) {                p_Msg = name + llList2String(templist, 0);            }            else {                p_Msg = llList2String(templist, 0) + name + llList2String(templist, 1);            }        }                        if (llSubStringIndex(p_Msg, "%tip%") != -1) {            templist = llParseString2List(p_Msg, ["%tip%"], []);              if (llSubStringIndex(p_Msg, "%tip%") == 0) {                p_Msg = (string)tip + llList2String(templist, 0);            }            else {                p_Msg = llList2String(templist, 0) + (string)tip + llList2String(templist, 1);            }        }        text();        if(IM_Giver)        {            llInstantMessage(giver,IM_G_Msg);        }                if(p_say)        {            llSay(0,p_Msg);        }        if(p_shout)        {            llShout(0,p_Msg);        }        if(p_whisper)        {            llWhisper(0,p_Msg);        }                p_Msg = tempstring;          }}

In other words:

First check if "llSubStringIndex() == -1" which means the substring is not found.

If so then you don't need to change the p_Msg.

  • Like 1
Link to comment
Share on other sites

How about using Haravikk Mistrals pretty neat strReplace function. It would just add 3 lines of code to your script.

string p_Msg = "%name% has just donated %tip% to help us out!";// http://wiki.secondlife.com/wiki/Library_Combined_Library#str_replacestring strReplace(string str, string search, string replace) {    return llDumpList2String(llParseStringKeepNulls((str = "") + str, [search], []), replace);}default{    touch_start(integer total_number)    {        string sName = llDetectedName(0);        integer iTip = 10;        llOwnerSay(strReplace(strReplace(p_Msg, "%name%", sName), "%tip%", (string)iTip));    }}

 

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3426 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...