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 at 16:04
  • \$\begingroup\$ instantiate the prefab first, i think that what i need \$\endgroup\$ Feb 25 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 at 16:45
  • \$\begingroup\$ Currently only the id 0 and 1 is added \$\endgroup\$ Feb 25 at 17:13
1
\$\begingroup\$

I chance the initial 3 lines of the Creator method, now its working properly, the reason is, as Phillipp said, the old code keep the information of the monsterChoosen variable and won't 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 its called.

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

Monster monsterChoosen = new Monster(monsterListPrefab.Find(m => m.Id == randId));
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You could improve this answer by explaining why this change fixes the problem: Your old code didn't change the newly created monster, it changed the prefab in your list of monster prefabs. So any changes you made to monsterChosen would also apply to any monsters chosen in the future. So the solution is to first create a copy of the monster prefab and then do your modifications on the copy. \$\endgroup\$
    – Philipp
    Feb 26 at 13:02
  • \$\begingroup\$ Thanks i will improve my answer \$\endgroup\$ Feb 26 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.