Help with SetText
|
|
Robertt Goodliffe
Registered User
Join date: 1 Jan 2006
Posts: 16
|
09-18-2006 12:19
I am having problem with the following script. Its just a simple script for a donation box that keeps track of the last 5 peaople to donate and how much they have donated. If a 6th person adds a donation the first person drops off the list the new person is added to the top.
However the problem i am having is that when a 2nd, 3rd or 4th person donates one of the previous names disappears off the list just showing the amount paid. Copy and paste the script to test it for yourself to see what i mean. You will need upto at least 5 people to fully test it to see what the bug is.
key rec1 = NULL_KEY; key rec2 = NULL_KEY; key rec3 = NULL_KEY; key rec4 = NULL_KEY; key rec5 = NULL_KEY; key rec0 = NULL_KEY;
integer paid1 = 0; integer paid2 = 0; integer paid3 = 0; integer paid4 = 0; integer paid5 = 0; integer cavalue = 0;
default { state_entry() { llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5); } money(key rec0, integer cavalue) { if ( rec0 == rec1) { paid1 = paid1 + cavalue; return; } else if ( rec0 == rec2) { paid2 = paid2 + cavalue; return; } else if ( rec0 == rec3) { paid3 = paid3 + cavalue; return; } else if ( rec0 == rec4) { paid4 = paid4 + cavalue; return; } else if ( rec0 == rec5) { paid5 = paid5 + cavalue; return; } else rec5 = rec4; rec4 = rec3; rec3 = rec2; rec2 = rec1; rec1 = rec0; paid5 = paid4; paid4 = paid3; paid3 = paid2; paid2 = paid1; paid1 = cavalue; llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5); } }
|
|
Lazink Maeterlinck
Registered User
Join date: 8 Nov 2005
Posts: 332
|
09-18-2006 13:09
this may help some people read this better key rec1 = NULL_KEY; key rec2 = NULL_KEY; key rec3 = NULL_KEY; key rec4 = NULL_KEY; key rec5 = NULL_KEY; key rec0 = NULL_KEY;
integer paid1 = 0; integer paid2 = 0; integer paid3 = 0; integer paid4 = 0; integer paid5 = 0; integer cavalue = 0;
default { state_entry() { llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5);
} money(key rec0, integer cavalue) { if ( rec0 == rec1) { paid1 = paid1 + cavalue; return; } else if ( rec0 == rec2) { paid2 = paid2 + cavalue; return; } else if ( rec0 == rec3) { paid3 = paid3 + cavalue; return; } else if ( rec0 == rec4) { paid4 = paid4 + cavalue; return; } else if ( rec0 == rec5) { paid5 = paid5 + cavalue; return; } else rec5 = rec4; rec4 = rec3; rec3 = rec2; rec2 = rec1; rec1 = rec0; paid5 = paid4; paid4 = paid3; paid3 = paid2; paid2 = paid1; paid1 = cavalue; llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5); } }
|
|
Trevor Langdon
Second Life Resident
Join date: 20 Oct 2004
Posts: 149
|
09-18-2006 14:03
Robert-- I see a problem with your final else clause. The following: else rec5 = rec4; rec4 = rec3; rec3 = rec2; rec2 = rec1; rec1 = rec0; paid5 = paid4; paid4 = paid3; paid3 = paid2; paid2 = paid1; paid1 = cavalue;
should be: else { rec5 = rec4; rec4 = rec3; rec3 = rec2; rec2 = rec1; rec1 = rec0; paid5 = paid4; paid4 = paid3; paid3 = paid2; paid2 = paid1; paid1 = cavalue; }
As you have it, only the first statement "rec5=rec4;" is tied to the "else" when a new person touches the object. The remaining "shuffle" statements occur everytime someone touches the object.
|
|
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
|
09-18-2006 14:05
ok.. several Problems I see in that.. first off, llKey2Name only works if the agent (person) is in the same sim.. once they leave the sim and someone else pays something.. it's gone..
You can counter this two ways.. first use llRequestAgentData and the dataserver event go get the name, this might be better anyway in case someone pays a donation from outside the sim (is that possible? seems to me it could be). The other idea, which I would most definately do, is instead of saving the keys of the people who donate, save thier name.
secondly, I'd be tempted to use a strided list or two seperate lists instead of hard coded variables.. this could also allow you to easily adjust the number of names shown.
and Thirdly.. Put some While loops in there.. or some For loops.. You can cut the amount of code down considerably. Heck, I'd even go for some Fruit Loops about now..
Someone else might alread be on it, but if they don't post first, let me see if i can come up with a piece of code to replace that.
|
|
ed44 Gupte
Explorer (Retired)
Join date: 7 Oct 2005
Posts: 638
|
09-18-2006 14:10
key rec1 = NULL_KEY; key rec2 = NULL_KEY; key rec3 = NULL_KEY; key rec4 = NULL_KEY; key rec5 = NULL_KEY;
integer paid1 = 0; integer paid2 = 0; integer paid3 = 0; integer paid4 = 0; integer paid5 = 0;
default { state_entry() { llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5);
} money(key rec0, integer cavalue) { if ( rec0 == rec1) paid1 = paid1 + cavalue; else if ( rec0 == rec2) paid2 = paid2 + cavalue; else if ( rec0 == rec3) paid3 = paid3 + cavalue; else if ( rec0 == rec4) paid4 = paid4 + cavalue; else if ( rec0 == rec5) paid5 = paid5 + cavalue; else { rec5 = rec4; rec4 = rec3; rec3 = rec2; rec2 = rec1; rec1 = rec0; paid5 = paid4; paid4 = paid3; paid3 = paid2; paid2 = paid1; paid1 = cavalue; } llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5); } }
|
|
Frans Charming
You only need one Frans
Join date: 28 Jan 2005
Posts: 1,847
|
09-18-2006 14:31
key rec1 = NULL_KEY; key rec2 = NULL_KEY; key rec3 = NULL_KEY; key rec4 = NULL_KEY; key rec5 = NULL_KEY;
integer paid1 = 0; integer paid2 = 0; integer paid3 = 0; integer paid4 = 0; integer paid5 = 0;
default{ state_entry(){ llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5);
}
money(key rec0, integer cavalue){ if ( rec0 == rec1) paid1 += cavalue; else if ( rec0 == rec2) paid2 += cavalue; else if ( rec0 == rec3) paid3 += cavalue; else if ( rec0 == rec4) paid4 += cavalue; else if ( rec0 == rec5) paid5 += cavalue; else { rec5 = rec4; rec4 = rec3; rec3 = rec2; rec2 = rec1; rec1 = rec0; paid5 = paid4; paid4 = paid3; paid3 = paid2; paid2 = paid1; paid1 = cavalue; } llSetText("Last 5 Donations\n" + llKey2Name(rec1) + " $" + (string)paid1 + " \n" + llKey2Name(rec2) + " $" + (string)paid2 + " \n" + llKey2Name(rec3) + " $" + (string)paid3 + " \n" + llKey2Name(rec4) + " $" + (string)paid4 + " \n" + llKey2Name(rec5) + " $" + (string)paid5, <1,1,1>, 1.5); } }

|
|
Robertt Goodliffe
Registered User
Join date: 1 Jan 2006
Posts: 16
|
Thankyou
09-18-2006 14:57
From: Aakanaar LaSalle ok.. several Problems I see in that.. first off, llKey2Name only works if the agent (person) is in the same sim.. once they leave the sim and someone else pays something.. it's gone..
It seems you hit the nail on the head Aakanaar, the problem was due to the people on the list no longer being around. So I changed the code to the following and it now seems to work.
string name0 = ""; string name1 = ""; string name2 = ""; string name3 = ""; string name4 = ""; string name5 = "";
integer paid1 = 0; integer paid2 = 0; integer paid3 = 0; integer paid4 = 0; integer paid5 = 0;
default { state_entry() { llSetText("Last 5 Donations\n" + name1 + " $" + (string)paid1 + " \n" + name2 + " $" + (string)paid2 + " \n" + name3 + " $" + (string)paid3 + " \n" + name4 + " $" + (string)paid4 + " \n" + name5 + " $" + (string)paid5, <1,1,1>, 1.5);
} money(key rec0, integer cavalue) { name0 = llKey2Name(rec0); if ( name0 == name1) paid1 = paid1 + cavalue; else if ( name0 == name2) paid2 = paid2 + cavalue; else if ( name0 == name3) paid3 = paid3 + cavalue; else if ( name0 == name4) paid4 = paid4 + cavalue; else if ( name0 == name5) paid5 = paid5 + cavalue; else { name5 = name4; name4 = name3; name3 = name2; name2 = name1; name1 = name0; paid5 = paid4; paid4 = paid3; paid3 = paid2; paid2 = paid1; paid1 = cavalue; } llSetText("Last 5 Donations\n" + name1 + " $" + (string)paid1 + " \n" + name2 + " $" + (string)paid2 + " \n" + name3 + " $" + (string)paid3 + " \n" + name4 + " $" + (string)paid4 + " \n" + name5 + " $" + (string)paid5, <1,1,1>, 1.5); } }
|
|
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
|
09-18-2006 15:01
ok.. I wrote this up as well. This didn't save a lot of coding but should work nicely. It is, however Untested. it may need to be tweaked. integer intShown = 5; // Number of names to keep and show vector clrColor = <1.0, 1.0, 1.0>; // Color of text float fltAlpha = 1.0; // And alpha of text (0.0 - 1.0)
list lstDonations; // This is a strided list. ["name", value]
// This function sets the text fnSetText() { integer intCount = llGetListLength(lstDonations) / 2; // This next line is just prevention and should never happen. if (intCount > intShown) { intCount = intShown; } string strText = "Last " + (string)intShown + " Donations:\n"; integer i = 0; while (i < intCount) { strText += llList2String(lstDonations, (i * 2)) + " $"; strText += llList2String(lstDonations, (i * 2) + 1) + "\n"; ++i; } // Knock off that last "\n" strText = llGetSubString(strText, 0, -3); llSetText(strText, clrColor, fltAlpha); }
// This functions updates the donation list. It searches for name // first, if found, updates value, if not, adds new line. Then it // shortens list to intShown entries. fnUpdateDonations(string strName, integer intAmount) { integer intIndex = llListFindList(lstDonations, [strName]); if (intIndex > -1) // If found { integer intDonated = llList2Integer(lstDonations, intIndex + 1); intDonated += intAmount; llListReplaceList(lstDonations, [intDonated], intIndex + 1, intIndex + 1); } else // Not found { lstDonations = [strName, intAmount] + lstDonations } // This is done outside the Else statement above as precautionary effort. // Basically if the list is too long before, it will be fixed now. integer intCount = llGetListLength(lstDonations); if (intCount > (intShown * 2)) { intCount = (intShown * 2); } lstDonations = llList2List(lstDonations, 0, intCount - 1); }
default { state_entry { fnSetText(); } money(key keyID, integer intAmount) { string strName = llKey2Name(keyID); fnUpdateDonations(strName, intAmount); fnSetText(); } }
Give it a shot, but until it's been tested, keep your original on standby. Edit: Fixed one piece of code that worked, but i thought would work better differently. Nothing major. Edit again: Found a piece that would have broke the code. Fixed it. the llListFindList wants a list as the second argument, not a string. My fault. Edit number 3: argh.. forgot to update the text after adding name in the money event. Done now.
|