Forum begins after the advertisement:

 


[Part 26] Problem with keeping chickens

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity [Part 26] Problem with keeping chickens

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #17897
    Foile
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I put 2 eggs in incubators and after hatching I named the chicks 1 and 2. And went to sleep. The next day I checked that the chicks were there and closed the game. Then I launch the save and in the henhouse there is only a chick named 2.

    #17898
    Tarha Apustaja
    Level 4
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information, there’s a hint about a potential issue in the egg hatching and naming process. The code snippet suggests that when eggs hatch, a naming prompt is triggered, but there’s no explicit mechanism shown for ensuring both eggs are properly saved or spawned.

    The reference information mentions: “we want to implement a queuing system so that when multiple eggs hatch, they are queued to trigger the naming prompt instead of overwriting the previous prompt.”

    However, the exact details of why only one chick is saved are NOT_FOUND in the provided reference information.

    To diagnose this issue more precisely, I would ask: Can you provide more details about how the chicks are spawned and saved after naming? Are there any specific save or spawn methods for the chickens that might be losing data?

    #17900
    Foile
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I tried hatching the eggs on different days and everything became normal. I can assume that the problem is that they hatch at the same time. I am not very good at coding and did everything according to the guide. I think that if I add a pause during pop-up windows, like opening the inventory, store, or assigning a name to an animal. Maybe this will fix the root of the problem. + I noticed that while my inventory is open, I can walk and dig beds without turning it off, which doesn’t look very cool. But how can I do this so as not to break everything that is already working?

    #17901
    Tarha Apustaja
    Level 4
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It sounds like you’ve identified that the issue may be related to simultaneous hatching of the chicks causing conflicts in the naming process. Adding a pause or delay when a naming prompt appears could indeed help ensure that the game properly registers both chicks without overwriting one another.

    To implement a pause during pop-up windows, you can follow a general approach like this (assuming you’re working with a game engine or programming environment that supports coroutines or timers):

    1. Add a Coroutine or Timer: When the chicks hatch, start a coroutine or a timer that will delay the prompt for naming the chicks.

    2. Queue the Naming Prompts: Instead of directly calling the naming function for both chicks, you can queue them and process one at a time.

    Here’s a simplified example in pseudo-code:

    function hatchChicks(chick1, chick2) {
        // Start a coroutine for naming
        startCoroutine(nameChick(chick1))
        startCoroutine(nameChick(chick2))
    }
    
    function nameChick(chick) {
        // Wait for a specific duration (e.g., 1 second)
        wait(1)
    
        // Show naming prompt
        showNamingPrompt(chick)
    
        // Wait for the player to finish naming
        waitForPlayerInput()
    }

    In this example, wait(1) adds a pause before showing the naming prompt for each chick. Adjust the duration based on your needs.

    As for the inventory issue where you can interact with the environment while it’s open, you might want to lock player controls while the inventory is open. You can do this by toggling a boolean variable that checks if the inventory is active. Here’s a simple way to do this:

    isInventoryOpen = false
    
    function toggleInventory() {
        isInventoryOpen = !isInventoryOpen
        updateInventoryUI(isInventoryOpen)
    
        // Lock or unlock player movement
        if (isInventoryOpen) {
            lockPlayerControls()
        } else {
            unlockPlayerControls()
        }
    }

    This way, you can prevent players from walking or interacting with the environment while the inventory is open.

    Make sure to test these changes thoroughly to ensure they don’t interfere with existing functionality. Good luck, and let me know if you need any further assistance!

    #17906
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    Nice catch, it’s probably because having them hatch at the same time causes a race condition on the animal count and id values. We will address this along with the environmental interaction in the next part, but in the meantime you can implement this fix:

    // Modify the UpdateEggs method in IncubationManager.cs
    public static void UpdateEggs()
    {
        //Eggs must be incubating
        if (eggsIncubating.Count == 0) return;
    
        // Store eggs ready to hatch to process them one at a time
        List<EggIncubationSaveState> eggsToHatch = new List<EggIncubationSaveState>();
    
        foreach(EggIncubationSaveState egg in eggsIncubating.ToList())
        {
            //Update the egg
            egg.Tick();
            onEggUpdate?.Invoke(); 
    
            if(egg.timeToIncubate <= 0)
            {
                eggsIncubating.Remove(egg);
                eggsToHatch.Add(egg);
            }
        }
    
        // Process hatching eggs sequentially instead of all at once
        if (eggsToHatch.Count > 0)
        {
            ProcessNextEgg(eggsToHatch, 0);
        }
    }
    
    // Add this new method to IncubationManager.cs
    private static void ProcessNextEgg(List<EggIncubationSaveState> eggs, int index)
    {
        if (index >= eggs.Count) return;
    
        //Get the animal data of chicken
        AnimalData chickenData = AnimalStats.GetAnimalTypeFromString("Chicken");
    
        // Start animal creation with a callback to process the next egg
        AnimalStats.StartAnimalCreation(chickenData, () => {
            ProcessNextEgg(eggs, index + 1);
        });
    }
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: