0
\$\begingroup\$

I'm creating a text based arena RPG where each day new monsters are added to a list, but with my current code, only one monster gets added to the list repeatly with the same stats (which for some reason keep increasing each day).

This method calls the creator.

public static List<Monster> MonsterOfTheDay()
  {
    int count = 0;
    List<Monster> MonstersListOfTheDay = new List<Monster>();

    while(count <= 5)
    {
      MonstersListOfTheDay.Add(Creator());
      count++;
    }

    return MonstersListOfTheDay;
  }

This is the creator

public static Monster Creator()
{
    Random random = new Random();
    Monster monsterChoosen = monsterListPrefab.Find(m => m.Id == random.Next(0, monsterListPrefab.Count -1));

    monsterChoosen.Level = random.Next(monsterChoosen.Level, monsterChoosen.Level + 3);

    //1Offensive, 2Defensive, 3Balance 
    monsterChoosen.Type = (Types)typeList.GetValue(random.Next(1, typeList.Length));

    Console.WriteLine("Estou Aqui");

    int atributes = monsterChoosen.Level * 3;
    int spend = 0;

    Console.WriteLine("Estou Aqui");
    while(spend != atributes)
    {
      int chance = random.Next(0, 100); 
      if(monsterChoosen.Type == Types.Offensive)
      {
        if(chance >= 0 && chance <= 60)
        {
          monsterChoosen.Str++;
          spend++;
        }

        if(chance >= 61 && chance <= 70)
        {
          monsterChoosen.Int++;
          spend++;
        }

        if(chance >= 71 && chance <= 85)
        {
          monsterChoosen.Agi++;
          spend++;
        }

        if(chance >= 86 && chance <= 100)
        {
          monsterChoosen.Vig++;
          spend++;
        }
      }
      else if(monsterChoosen.Type == Types.Defensive)
      {
        if(chance >= 0 && chance <= 60)
        {
          monsterChoosen.Vig++;
          spend++;
        }

        if(chance >= 61 && chance <= 70)
        {
          monsterChoosen.Str++;
          spend++;
        }

        if(chance >= 71 && chance <= 85)
        {
          monsterChoosen.Int++;
          spend++;
        }

        if(chance >= 86 && chance <= 100)
        {
          monsterChoosen.Agi++;
          spend++;
        }
      }
      else if(monsterChoosen.Type == Types.Balance)
      {
        if(chance >= 0 && chance <= 25)
        {
          monsterChoosen.Str++;
          spend++;
        }

        if(chance >= 26 && chance <= 50)
        {
          monsterChoosen.Int++;
          spend++;
        }

        if(chance >= 51 && chance <= 75)
        {
          monsterChoosen.Agi++;
          spend++;
        }

        if(chance >= 76 && chance <= 100)
        {
          monsterChoosen.Vig++;
          spend++;
        }
      }
      else if(monsterChoosen.Type == Types.Prefab)
      {
        spend++;
      }
      else
      {
        Console.WriteLine("Error");
      }
    }

    return monsterChoosen;
  }
\$\endgroup\$
4
  • 1
    \$\begingroup\$ You're modifying your prefab to have your chosen stats. That will modify the stats of all other references to that prefab in your list. Did you mean to instantiate the prefab first, or store a separate stats object to assign when instantiated? \$\endgroup\$
    – DMGregory
    Feb 25, 2021 at 16:04
  • \$\begingroup\$ instantiate the prefab first, i think that what i need \$\endgroup\$ Feb 25, 2021 at 16:07
  • 1
    \$\begingroup\$ as for why the same monster gets added to the list. Just a guess, for some reasons your monster id has a higher value than the total monsters in the list with just for chosen monster fitting. (If you have 3 monsters with id 2, 3, 4 and you are drawing from 0 to monsterlist.size and search the random number in that monster list, only monster 2 gets pulled). Could you double check what the id of all your monsters is? \$\endgroup\$
    – Zibelas
    Feb 25, 2021 at 16:45
  • \$\begingroup\$ Currently only the id 0 and 1 is added \$\endgroup\$ Feb 25, 2021 at 17:13

1 Answer 1

1
\$\begingroup\$

I changed the initial 3 lines of the Creator method, and now it's working properly.

Random random = new Random();
int randId = random.Next(2);

Monster monsterChoosen = new Monster(monsterListPrefab.Find(m => m.Id == randId));

The reason is, as Philipp said, the old code kept the information of the monsterChoosen variable, so it would not generate a new one when called again, keeping the same result and only increasing its stats and level. Now with the new line of the variable, monsterChoosen can update every time the method is called.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .