Page 1 of 1

Helper class

Posted: Mon Jan 22, 2018 9:58 pm
by hallsofvallhalla
So I am deciding how to make all commands in my game easily accessible as well as adding them in the future. So basically its easy to add new commands and much of the base code wont have to be touched. For instance when a player puts
attack@Goblin
search@chest

the word before the @ sign is the command and I have parsers that get that. I am building a c# class for each command but I want a easy way for said command to activate that class.

My thoughts are
1. create a helper class that stores instances of all commands in a dictionary and the key is the command word. for instance helperClass.getCommand("attack", message); would look for the instance of attack in the dictionary then pass the message


hmm now that I think this through I think this will work best. Any other ideas?

Re: Helper class

Posted: Mon Jan 22, 2018 10:17 pm
by hallsofvallhalla
On second thought this wont work since all commands are a different type of class and will have different methods. Hmm wonder if I make them have to follow a path like an interface(which I still dont fully understand interfaces). There has to be a way to create an array or dictionary or simply cycle through instances of classes and look for the right one.

Re: Helper class

Posted: Mon Jan 22, 2018 10:51 pm
by vitinho444
Don't know much about interfaces and polymorphism in c#, but I do in Java and I think it's the same. Interfaces are just blueprints for other classes to build onto. You just define the return type, method (function) names and parameters and then child classes do the body of it. You can create a list of Interfaces to have all in the same place and then cast to the class you want.

Hope it helps. :roll:

Re: Helper class

Posted: Tue Jan 23, 2018 2:29 am
by hallsofvallhalla
I think I can make it work with polymorphism and interfaces.

Re: Helper class

Posted: Tue Jan 23, 2018 10:49 pm
by hallsofvallhalla
Figured it out

I made a interface and then each command is a class with that interface. Wahoo!! I finally understand interfaces!

So now to make a mode you make a new class and simply add a reference to that class in the commandHelper class. When a user types @ and your command it automatically runs that command.

Code: Select all

public class commandHelper
    {
        private Dictionary<string, ICommand> commands = new Dictionary<string, ICommand>();
        public void startCommands()
        {
            login loginCommand = new login();
            register registerCommand = new register();
            commands.Add("login", loginCommand);
            commands.Add("register", registerCommand);
        }
        public string runCommand(string command)
        {
            string returnMessage = "";
            if (command == "") { return returnMessage; }
            commands[command].Startup();
            return returnMessage;
        }
    }

Re: Helper class

Posted: Tue Jan 23, 2018 11:30 pm
by hallsofvallhalla
Final code for running and sending a message to a command then that command can return a message back to user, for instance "@attack Goblin" will send to the attack command goblin, then the attack command can return to the user or all users what ever it wants like "Halls attacks Goblin and hits for 4 damage!"

Code: Select all

public class commandHelper
    {
        private Dictionary<string, ICommand> commands = new Dictionary<string, ICommand>();
      
        public commandHelper()
        {
            login loginCommand = new login();
            register registerCommand = new register();
            commands.Add("login", loginCommand);
            commands.Add("register", registerCommand);
        }
        public string runCommand(string command, string[] message)
        {
            string returnMessage = "";
            if (command == "") { return returnMessage; }
            returnMessage = commands[command].Startup(message);
            return returnMessage;
        }
    }

Re: Helper class

Posted: Wed Jan 24, 2018 1:04 pm
by vitinho444
Glad it's working with Interfaces :D

Re: Helper class

Posted: Thu Jan 25, 2018 1:46 am
by Jackolantern
Not sure if this is exactly what would help, but this is kind of circling around a usecase for the "Command Pattern". It is a nice way to be able to parameterize an entire command. It can make it easy to string different parts of commands together, log them, undo them, etc. Perhaps a combination of a command pattern that be configured to call a specific interface to carry out the action (which is in a class that inherits from the command target interface)?

Re: Helper class

Posted: Thu Jan 25, 2018 4:31 pm
by hallsofvallhalla
Oh wow thanks for the link! Reading now.