Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "interpolate" these names into a method / variable that I want to update?

In my new game I have several different animals that I can add and remove from specific paddocks. The code is working as intended but it is very repetitive and long. Currently, I am using (multiple) switch statements to grab a string called itemName from scriptable objects that hold the animal info.

As you can see in my code below, its unsightly with lots of duplicated code! Is there a suggested way to reduce and improve this given the chunks of code in each case are identical apart, from the animal name string? I use multiple switch statements in the same type of way throughout my project and it would reduce hundreds of lines.

I am relatively new to programming. When looking through documentation, I thought string interpolation might be the way to do it but couldn't figure it out how to make it actually refer to the right variable (like an int in my save file).

Essentially, what I would want to do is take the item.itemName, remove any spaces and make all lowercase, and then "plug it in" where needed, like saveMgr.myStats.{theNewItemName}-= 1; or similar. Then I would need like 6 lines instead of like 300.

Here is my code. The content I left in is just to show how duplicated it is right now.

            switch (item.itemName)
            {
                case "Albino Goat":
                    saveMgr.myStats.albinogoat -= 1;
                    specificPaddock.albinogoat += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.albinogoat, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Arabian Horse":
                    saveMgr.myStats.arabianhorse -= 1;
                    specificPaddock.arabianhorse += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.arabianhorse, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Bull Calf":
                    saveMgr.myStats.bullcalf -= 1;
                    specificPaddock.bullcalf += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.bullcalf, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Cow Calf":
                    saveMgr.myStats.cowcalf -= 1;
                    specificPaddock.cowcalf += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.cowcalf, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Merino Lamb":
                    saveMgr.myStats.merinolamb -= 1;
                    specificPaddock.merinolamb += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.merinolamb, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Piglet":
                    saveMgr.myStats.piglet -= 1;
                    specificPaddock.piglet += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.piglet, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Quarter Horse":
                    saveMgr.myStats.quarterhorse -= 1;
                    specificPaddock.quarterhorse += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.quarterhorse, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Azteca Horse":
                    saveMgr.myStats.aztecahorse -= 1;
                    specificPaddock.aztecahorse += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.aztecahorse, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Bull":
                    saveMgr.myStats.bull -= 1;
                    specificPaddock.bull += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.bull, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Cow":
                    saveMgr.myStats.cow -= 1;
                    specificPaddock.cow += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.cow, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Grey Goat":
                    saveMgr.myStats.greygoat -= 1;
                    specificPaddock.greygoat += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.greygoat, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Merino Sheep":
                    saveMgr.myStats.merinosheep -= 1;
                    specificPaddock.merinosheep += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.merinosheep, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Pig":
                    saveMgr.myStats.pig -= 1;
                    specificPaddock.pig += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.pig, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Black Lamb":
                    saveMgr.myStats.blacklamb -= 1;
                    specificPaddock.blacklamb += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.blacklamb, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Black Sheep":
                    saveMgr.myStats.blacksheep -= 1;
                    specificPaddock.blacksheep += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.blacksheep, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Dark Horse":
                    saveMgr.myStats.darkhorse -= 1;
                    specificPaddock.darkhorse += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.darkhorse, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Dorper Sheep":
                    saveMgr.myStats.dorpersheep -= 1;
                    specificPaddock.dorpersheep += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.dorpersheep, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Dorper Lamb":
                    saveMgr.myStats.dorperlamb -= 1;
                    specificPaddock.dorperlamb += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.dorperlamb, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Berkshire Pig":
                    saveMgr.myStats.berkshirepig -= 1;
                    specificPaddock.berkshirepig += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.berkshirepig, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Berkshire Piglet":
                    saveMgr.myStats.berkshirepiglet -= 1;
                    specificPaddock.berkshirepiglet += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.berkshirepiglet, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Swiss Cow":
                    saveMgr.myStats.swisscow -= 1;
                    specificPaddock.swisscow += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.swisscow, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
                case "Swiss Cow Calf":
                    saveMgr.myStats.swisscowcalf -= 1;
                    specificPaddock.swisscowcalf += 1;
                    PaddockAddEvent(item.itemName, specificPaddock.swisscow, specificPaddock, item);
                    UpdateStatOnAnimalAddEvent(onePercent);
                    break;
            }

So yeah, the code works as intended but I am unhappy with the messiness and want to understand alternative ways to plan this logic, especially for planning my next game. Thank you for reading.

like image 595
Melissa K Avatar asked Dec 27 '25 14:12

Melissa K


2 Answers

You can think animal is an object. Let's go into the OOP world.

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

public class Example : MonoBehaviour
{
    private void Start()
    {
        Item item = new Item("Albino Goat");
        SaveManager saveMgr = new SaveManager(new List<Animal>() { new Animal("Albino Goat") });
        Paddock specificPaddock = new Paddock();

        Animal animal = saveMgr.Remove(item.Name);
        specificPaddock.Add(animal);

        //UpdateStatOnAnimalAddEvent(onePercent);
    }

    private class SaveManager
    {
        private List<Animal> animals = null;

        public SaveManager(List<Animal> animals)
        {
            this.animals = animals ?? throw new ArgumentNullException(nameof(animals));
        }

        public Animal Remove(string animalName)
        {
            Predicate<Animal> match = (x) => x.Name.Equals(animalName);
            if (animals.Exists(match))
            {
                return animals.Find(match);
            }

            return null;
        }
    }

    private struct Item
    {
        public string Name { get; private set; }

        public Item(string name)
        {
            Name = name ?? throw new ArgumentNullException(nameof(name));
        }
    }

    private class Animal
    {
        public string Name { get; private set; }

        public Animal(string name)
        {
            Name = name ?? throw new ArgumentNullException(nameof(name));
        }
    }

    private class Paddock
    {
        private List<Animal> animals = null;

        public Paddock()
        {
            animals = new List<Animal>();
        }

        public void Add(Animal animal)
        {
            animals.Add(animal);
            Debug.LogFormat("Animal count in Paddock : {0}", animals.Count);
        }
    }
}

I wrote every private class as temp.

Just take my thinking about relationship with each object from this code.

Don't need to save every animal as int.

Code will be working so just copy and paste and try to see what's going on in the editor.

like image 140
Beautyfull Castle Avatar answered Dec 30 '25 05:12

Beautyfull Castle


If you were to use a Dictionary<string,int>, then you could replace the whole switch statement with something like

saveMgr.myStats[item.itemName] -= 1;
specificPaddock[item.itemName] += 1;
PaddockAddEvent(item.itemName, specificPaddock[item.itemName], specificPaddock, item);
UpdateStatOnAnimalAddEvent(onePercent);

For further safety, also consider creating an enum AnimalType, and turning the dictionary into <AnimalType,int>, as well as look into the animal-as-class-object approach others suggest. You are on the right track by wanting to follow DRY, aka Don't Repeat Yourself -- Good luck!

like image 45
Philipp Lenssen Avatar answered Dec 30 '25 05:12

Philipp Lenssen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!