Issues making a daily hat store selector

I’m working on a system for selecting hats in a shop within my game, and I want it to be based on the current day. Here’s what I’m aiming to achieve:

image

  1. Daily Hat Selection:
    Each day should determine which hats are displayed in the shop. To achieve this, I multiply the current day number by three to create a starting index for selecting hats.
  2. Skipping Hats:
    Some hats are marked as NotInCycle, which means they shouldn’t be displayed. I need to make sure that these hats are skipped in the selection process.
  3. Avoiding Repetitions:
    The same hat should not appear more than once in a single day’s selection. Additionally, I want to ensure that the hat sets don’t repeat on consecutive days.

To implement this, I calculate the starting index based on the current day and then select hats in a cyclic manner, skipping those marked as NotInCycle. I use wrapping logic to handle cases where the index goes out of bounds, making sure the selection is smooth and respects the constraints.

Here is my code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class ShopCycleManager : MonoBehaviour
{
    [Serializable]
    public class Hat 
    {
        public Sprite HatArt;
        public int Id;
        public bool NotInCycle;
    }
    public TextMeshProUGUI TimeLeftUnitlRefreshTxt;
    public DateTime CurrentTime;
    public TimeSpan TimeUntilShopRefresh;
    public GameObject[] HatsInCycle;
    public int Day;
    public GameObject Data;
    public int TotHats;
    public Hat[] Hats;
    public int Days;
    // Start is called before the first frame update
    void Awake()
    {
        Data = GameObject.Find("Data");
    }

    // Update is called once per frame
    void Update()
    {
        if (Data.GetComponent<DataStore>().Refresh == false)
        {
            Day = Days*3;
        }
        else
        {
            Day = (System.DateTime.Now.Day+Data.GetComponent<DataStore>().RefreshShopOffset)*3;
        }

        SetHats();
        /*
        CurrentTime = DateTime.Now;
        TimeUntilShopRefresh = CalculateTimeUntilNextNoon(CurrentTime);
        TimeLeftUnitlRefreshTxt.text = $"{(int)TimeUntilShopRefresh.TotalHours}H, {TimeUntilShopRefresh.Minutes}M, {TimeUntilShopRefresh.Seconds}S, left until auto shop refresh";*/
    }

    void SetHats()
    {
        int Hat1, Hat2, Hat3;
        int range = TotHats;

        // Find Hat1
        int i = WrapNumber(Day, range); // Start index

        // Find first valid hat
        while (Hats[i - 1].NotInCycle)
        {
            i = WrapNumber(i + 1, range);
        }
        Hat1 = Hats[i - 1].Id;

        // Find Hat2
        i = WrapNumber(i + 1, range);

        // Find second valid hat
        while (Hats[i - 1].NotInCycle)
        {
            i = WrapNumber(i + 1, range);
        }
        Hat2 = Hats[i - 1].Id;

        // Find Hat3
        i = WrapNumber(i + 1, range);

        // Find third valid hat
        while (Hats[i - 1].NotInCycle)
        {
            i = WrapNumber(i + 1, range);
        }
        Hat3 = Hats[i - 1].Id;

        Debug.Log(Hat1 + ", " + Hat2 + ", " + Hat3);
    }
    int WrapNumber(int value, int range)
    {
        return ((value - 1) % range + range) % range + 1;
    }
    TimeSpan CalculateTimeUntilNextNoon(DateTime currentTime)
    {
        // Calculate the next 12 AM
        DateTime nextMidnight = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day).AddDays(1);

        // Calculate the time span until next 12 AM
        return nextMidnight - currentTime;
    }

}

If you need more info just ask. But yea ive been banging my head against my wall for ages trying to make this work. I’m getting incorrect results and repeating hats. If theres a different way I can do this or if its an easy fix that would be great. Thanks

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Just so you know…

These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

The following applies to ALL types of code listed above, but for simplicity I will call it “inventory.”

Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.

Inventories / shop systems / character selectors all contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation
    → what it looks like lying around in the world? In a chest? On a shelf?
    → what it looks like in the inventory window itself?
    → what it looks like when worn by the player? Does it affect vision (binoculars, etc.)
    → what it looks like when used, destroyed, consumed?

Just the design choices of such a system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

Breaking down large problems in general:

The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.

Various possible inventory data structures in Unity3D: