// QuestItem.cs // Click this object, or pick it up if it is Grabbable, to count towards a quest task that is "Done by a script" // (Windows -> Quests), e.g. collect 5 coins. It only counts while the player is doing that task, once per player. // (Tasks of the kind "Click or pick up objects" need no script at all: pick the objects in the Quests window.) using IHXR.Simulation; public class QuestItem : ScriptBase { [Tooltip("The quest's title (or id).")] public string Quest = ""; [Tooltip("The task's title (or id).")] public string Task = ""; [Tooltip("How much it adds.")] public int Amount = 1; [Tooltip("Hide it for that player once clicked (not when picked up).")] public bool HideWhenCollected = true; public string ClickHint = "Pick up"; private readonly HashSet collected = new(); public override void Start() { MakeInteractable(click => Collect(click.Username, HideWhenCollected), true, null, true, 10f, ClickHint); ListenForGrab(grab => { if (grab.IsGrabbed) Collect(grab.Username, false); }); // The player reset their quests: they can collect it again. ListenForQuest(Quest, quest => { if (quest.EventType != QuestEventType.Reset || !collected.Remove(quest.Username)) return; if (HideWhenCollected) SetObjectVisible(true, quest.Username); }); } private void Collect(string user, bool hide) { QuestTaskInfo task = GetQuest(user, Quest)?.GetTask(Task); if (task == null || !task.IsActive || collected.Contains(user)) return; // not on this task right now, or already collected collected.Add(user); AddQuestProgress(user, Quest, Task, Amount); if (hide) SetObjectVisible(false, user); } }