Forum rules - please read before posting.

Random sequence puzzle?

Hi there, I'm trying to figure out how I would set something like this up and am looking for some guidance.

For context: In a library, the player is meant to sort books onto shelves. I want it so that the player can click on a hotspot, receive 5 books (out of a possible 20 books) into their inventory, put them onto their desired shelf, and then for an actionlist to play so that we understand the task is completed. Alternatively, it could be done by instead clicking on the hotspot 5 times to generate 5 books, but then I would need it to stop after the 5th time.

I would imagine this would start out by running a sequence with a possibility of 20, but I wouldn't know how to get it to give 5 books, or alternatively stop giving out books after clicking on the hotspot 5 times. Secondly, and most importantly, I'm not sure how to set it up so that the player knows they're done once they've put the 5th book away. I'm assuming this is done by keeping a score, but I'm unsure of how to set that up.

Thanks so much

Comments

  • edited March 14

    You can keep track of the player's progress/score with an Integer variable - increasing by 1 each time.

    If this score is needed to be referenced in other scenes, use a Global variable - otherwise make it a Local or Component one.

    The Variable: Set Action's Increase By Value method can be used to increase it, and the Variable: Check Action can be used to compare it with 5.

    For adding 5 random items to the player's Inventory: this is best done with a short custom script. While you could rely on a Variable: Check random number to output 20 different Inventory: Add or remove Actions so that the Player can randomly recieve one of 20 items, it wouldn't account for what the Player is already carrying. That is, the same number/item could be randomly chosen multiple times.

    To have the script know which items are books that it can give the player, use the Inventory Manager's Categories tab to create a pair of categories (one Default, another Books). Put all the book Items in the Books category, and note the Category's ID number.

    You can then use this script to add a random amount of unique Items from that category to the Player's Inventory:

    using AC;
    using UnityEngine;
    
    public class RandomItemGiver : MonoBehaviour
    {
    
        public int categoryID = 1;
        public int numToGive = 5;
    
        public void AddItems()
        {
            var allItems = KickStarter.inventoryManager.GetItemsInCategory(categoryID);
    
            // Random sort
            for (int i = 0; i < allItems.Length; i++)
            {
                int randomIndex = Random.Range(i, allItems.Length);
                (allItems[randomIndex], allItems[i]) = (allItems[i], allItems[randomIndex]);
            }
    
            var invCollection = KickStarter.runtimeInventory.PlayerInvCollection;
            int numGiven = 0;
            foreach (var invItem in allItems)
            {
                if (invCollection.GetCount(invItem.id) > 0) continue;
    
                var invInstance = new InvInstance(invItem, 1);
                invCollection.Add(invInstance);
                numGiven++;
    
                if (numGiven >= numToGive) return;
            }
        }
    
    }
    

    To use it, copy/paste into a C# file named RandomItemGiver, attach to an Empty GameObject, make it a prefab, and remove from the scene. Configure its Inspector values as needed.

    Then, in your ActionList, use the Object: Call event Action to reference the prefab's RandomItemGiver/AddItems function.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.