Phoxeh
Phoxeh

Reputation: 15

Dynamically deciding what object to interact with by string contents

I am working on a console application in C# .Net 6.0 that uses WMPLib that I am trying to turn into a form of MUD game.

I am struggling to allow the player to control what it can attack and 'validate' it is an actual object with a matching correct name.

I have two relevant custom class objects

Monster tytbric = new();

Player player = new();

Followed by a method that interprets a string

globals.Command = Console.ReadLine();

public int ReadCommand(string comm)
        {
            if (comm.StartsWith("go"))
            {
                return 1;
            }
            if (comm.StartsWith("talk"))
            {
                return 2;
            }
            if (comm.StartsWith("attack"))
                return 3;
            }
        }

I then define what is being attacked by taking the second half of globals.Command

(The expected contents of the string would be in the format of "attack nameofmonster")

if (command.ReadCommand(globals.Command) == 3) //attack command 
   {
       string attackingwhat = globals.Command.Split(' ')[1];
       if (attackingwhat == tytbric.name)
       {
           if (player.zoneNum == tytbric.zoneNum && player.coordinate == tytbric.coordinate)
           {
               if (tytbric.Dead == 0)
               {
                   globals.ReturnedCombatStats = statcombatcalcs.MeleeToMeleeCalc(player.maxhp, player.CurrHP, player.strength, player.dexterity, player.level, tytbric.maxhp,
                       tytbric.CurrHP, tytbric.strength, tytbric.dexterity, tytbric.level, tytbric.DisplayName);
                   //player.CurrHP = globals.ReturnedCombatStats[0];
                   tytbric.Dead = globals.ReturnedCombatStats[3];
                   player.Dead = globals.ReturnedCombatStats[4];
                   player.strength += 0.1;
               }
           }
        }
}

Although this works with the correct casing, I wanted it to look for an object instance that was named the same as the string value rather than specifying it or by using the property tytbric.Name ? Is that possible or should I implement this / look at this a different way? I'm quite new to development and this is just a hobby for now.

In the end I hope to have lots of objects with the type of Monster - so allowing a form of string search or similar feels important.

[EDIT] I should add that I have a unique property of monsterid

E.g. If(attackingwhat == ObjectOfType(Monster).Property(Name)) ?

Upvotes: 0

Views: 66

Answers (1)

Andy Wynn
Andy Wynn

Reputation: 1256

You probably don't want the answer you are asking for. It's very bad practice to start looking for in-code objects by name (though you can do this with reflection)

In your case, I'd suggest making a static List<Monster> monsters = new(); somewhere high level, then .Add and .Remove when monsters spawn/die. Add a string name field to the Monsters class, and find the monster by iterating though the list.

It's probably a bit advanced, but you can do this with Linq:

monsters.where(x => x.name == userString).FirstOrDefault()

You can then attack this object or anything else the object exposes.

Edit: couple of other notes:

Look up enums, they are far better than returning/checking for hard coded ints

Also look up the switch statement and patterns that can give.

You may also be interested in string.split, as you can split your string into an array of strings (in your case by the space character) to get command/target information seperately.

Happy coding!

Upvotes: 1

Related Questions