0

I have made a enum type (MonsterTypes) with 4 types (Monster1, Monster2 , Monster3, Monster4) which should be possible to extend with more monsters. I want to chose a random type, and create an instances of a random monster, and rather than having if states stating if (System.Random.Next(4) = 1) Monster1.CreateMonster(Image)} which works. In order to automatically extend the random from 4-5 when a new enum is added.
var squadrons = MonsterTypes.GetNames( typeof( MonsterType)).Length; in order to get the number of enums.
I encounter a problem when i try to initiate CreateMonster() without an if statement. If i use: ((MonsterTypes) System.Random.Next(4)).CreateMonster(Image) i get the error: "'MonstersTypes' does not contain a definition for 'CreateMonster' and no accessible extension method 'CreateMonster' accepting a first argument of type 'MonsterTypes' could be found (are you missing a using directive or assembly refrence?)

public static  class CreateNextMonster {
    public static IMonster ActiveMonster {get;private set;}
    private static System.Random rand = new System.Random();
    public static void CreateNewMonster (Img MonsterPicture) {
        var monstersTotal = MonsterType.GetNames(typeof(MonsterType)).Length;
        int nextMonster= rand.Next(monstersTotal);
        ((MonsterType) nextMonster).CreateMonster();
    }
}
  • What is the definition / signature of the `CreateMonster()` method? – Pac0 Mar 31 '21 at 22:23
  • Also what is the type `Monsters` in the last line of your code? Isn't that supposed to be the enum `MonsterTypes` ? – Pac0 Mar 31 '21 at 22:24
  • [How do I select a random value from an enumeration?](https://stackoverflow.com/a/3132151/3791245) – Sean Skelly Mar 31 '21 at 22:26
  • By the way, concerning the target duplicate q&a, I especially favors [this answer](https://stackoverflow.com/a/60847339/479251) – Pac0 Mar 31 '21 at 22:30
  • 1
    I think you are missing the argument to CreateMonster. Should be `CreateMonster(MonsterPicture)` – Garr Godfrey Mar 31 '21 at 22:30
  • but what we really need to see is the extension methods for CreateMonster – Garr Godfrey Mar 31 '21 at 22:31
  • @GarrGodfrey nice catch, I think your comment is right on point, since OP mentioned 'Monster1.CreateMonster(Image) which works' – Pac0 Mar 31 '21 at 22:32
  • `if (System.Random.Next(4) = 1) Monster1.CreateMonster(Image);` is using an assignment, not a comparison, for the condition. Isn't that a bug? – Rufus L Apr 01 '21 at 00:02
  • @Pac0 The signature is void CreateNewMonster(Img MonsterPicture) and yes, you are correct, it is MonsterType, i had them in a folder called Monsters, i managed to delete the type rather than the folder, sorry about that mistake – FsharpTopp Apr 01 '21 at 07:17

0 Answers0