// StoreOffer.cs // Offers one store item for sale right in the world: a buy panel (desktop, mobile and VR) with the item's picture, name, // creator, price and the player's balance. Buy is a normal store purchase (the coins go to the seller) and the item goes // straight into the player's inventory; they see that it was delivered, or why not. No store or inventory panel opens. // // Set up: put this script on a clickable object (a shop sign, a mannequin). In the Inspector choose the Store the item is // sold in (Scene = Scene Editor store, Avatar = Avatar Editor store) and paste the Item: its store id, or its store link. // A private item needs its private link (Edit Store Details -> Copy Private Link), which carries its key. // Clicking offers it to the player who clicked. Optional: Everyone Button (offers it to everybody in the world at once) // and Offer When Players Arrive. using IHXR.Simulation; public class StoreOffer : ScriptBase { [Tooltip("The store the item is sold in: Scene (Scene Editor store) or Avatar (Avatar Editor store).")] public StoreKind Store = StoreKind.Scene; [Tooltip("The item's store id, or its store link (a private item needs its private link).")] public string Item = ""; [Tooltip("Optional line shown above the item.")] public string Message = "Like it? Get your own!"; public string ClickHint = "Buy this item"; [Tooltip("Optional: an object that offers the item to everybody in the world at once.")] public SceneObject EveryoneButton; [Tooltip("Offer the item to each player when they arrive.")] public bool OfferWhenPlayersArrive = false; [Tooltip("Say thank you in chat when somebody buys it.")] public bool ThankInChat = true; public override void Start() { if (string.IsNullOrWhiteSpace(Item)) { LogWarning("StoreOffer: paste the store item's id or link in the Item field."); return; } // The player who clicks gets the offer. MakeInteractable(click => OfferStoreItem(click.Username, Store, Item, OnResult, Message), true, null, true, 10f, ClickHint); if (EveryoneButton != null && !EveryoneButton.IsNull) MakeInteractable(EveryoneButton, click => OfferStoreItemToEveryone(Store, Item, OnResult, Message), true, null, true, 10f, "Offer to everyone"); } public override void OnUserJoin(UserInfo user) { if (OfferWhenPlayersArrive && !string.IsNullOrWhiteSpace(Item)) OfferStoreItem(user, Store, Item, OnResult, Message); } // Runs once for each player who answers. Purchases are confirmed with the store before this is called. void OnResult(StoreOfferResult result) { switch (result.Status) { case StoreOfferStatus.Purchased: Log(result.Username + " bought " + result.ItemId); if (ThankInChat) SendScriptChat("Thank you for your purchase, " + result.Username + "!"); break; case StoreOfferStatus.Redelivered: Log(result.Username + " got " + result.ItemId + " back in their inventory"); break; case StoreOfferStatus.AlreadyOwned: Log(result.Username + " already has " + result.ItemId); break; case StoreOfferStatus.Declined: Log(result.Username + " said not now"); break; default: LogWarning("Offer to " + result.Username + " did not work: " + result.Message); break; } } }