integer defaultPrice = 100; integer price = defaultPrice; string item; string notecardName; integer notecardSize; key owner; key notecardRequest; setInfo() { item = llGetInventoryName(INVENTORY_OBJECT, 0); if (item == "") { llInstantMessage(owner, "Please put an item to my inventory."); } else { notecardName = llGetInventoryName(INVENTORY_NOTECARD, 0); if (notecardName == "") { price = defaultPrice; llInstantMessage(owner, "The price is set to L$" + (string)defaultPrice + " (default) because there is no notecard in my inventory."); setItemPrice(); } else { integer value = (integer)notecardName; if (value > 0) { price = value; llInstantMessage(owner, "The price is set to L$" + (string)price + " from the notecard name."); setItemPrice(); } else { notecardSize = -1; notecardRequest = llGetNumberOfNotecardLines(notecardName); // -> dataserver() } } } } setItemPrice() { llSetObjectName(item + " - item seller"); llSetPayPrice(price, [price, PAY_HIDE, PAY_HIDE, PAY_HIDE]); llSetText(replaceName(item) + "\nL$" + (string)price, <1.0, 1.0, 1.0>, 1.0); string texture = llGetInventoryName(INVENTORY_TEXTURE, 0); if (texture != "") { llSetTexture(texture, ALL_SIDES); } } string replaceName(string s) { integer n = 0; while (n >= 0) { n = llSubStringIndex(s, " - "); if (n >= 0) { s = llDeleteSubString(s, n, n+2); s = llInsertString(s, n, "\n"); } } return s; } default { state_entry() { owner = llGetOwner(); llSetClickAction(CLICK_ACTION_PAY); setInfo(); } changed(integer change) { if (change & CHANGED_INVENTORY) { setInfo(); } } dataserver(key query, string data) { if (query == notecardRequest) { if (notecardSize < 0) { notecardSize = (integer)data; if (notecardSize <= 0) { price = defaultPrice; llInstantMessage(owner, "The price is set to L$" + (string)price + " (default) because the notecard \"" + notecardName + "\" is empty."); setItemPrice(); } else { notecardRequest = llGetNotecardLine(notecardName, 0); // -> dataserver() } } else { price = (integer)data; if (price < 0) { price = 0; llInstantMessage(owner, "The price for \"" + item + "\" is set to L$" + (string)price + " because the price of notecard \"" + notecardName + "\" is negative."); } else { llInstantMessage(owner, "The price for \"" + item + "\" is set to L$" + (string)price + " from the notecard \"" + notecardName + "\"."); } setItemPrice(); } } } money(key buyer, integer amount) { if (amount >= price) { llInstantMessage(owner, llKey2Name(buyer) + " paid you L$" + (string)amount + " for \"" + item + "\"."); llInstantMessage(buyer, "Thank you for your purchase of \"" + item + "\" with L$" + (string)amount + "."); llGiveInventory(buyer, item); } else { llInstantMessage(buyer, "You paid L$" + (string)amount + ", but it is less than the price L$" + (string)price + ", so your payment is considered as a donation. Thank you for your donation."); llInstantMessage(owner, llKey2Name(buyer) + " paid you L$" + (string)amount + " as donation."); } } } |
価格は、前回と同様、inventory にある notecard から取得しますが、メンテナンスを容易にするため、もし notecard の名前が数字の場合は、それを価格にします。また、売り物であるオブジェクトの名前を
Avator が Pay を実行すると、money() イベントが発生しますが、そこでは、支払われた金額と価格を比較して、価格に満たない
なお、llSetPayPrice() で、最初のパラメータも PAY_HIDE にして、llSetPayPrice(PAY_HIDE, [price, PAY_HIDE, PAY_HIDE, PAY_HIDE]); とすると、指定した金額のボタンだけが現れます。
Tags: programming, second_life