Helper class

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Helper class

Post 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?
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Helper class

Post 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.
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: Helper class

Post 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:
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Helper class

Post by hallsofvallhalla »

I think I can make it work with polymorphism and interfaces.
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Helper class

Post 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;
        }
    }
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Helper class

Post 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;
        }
    }
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: Helper class

Post by vitinho444 »

Glad it's working with Interfaces :D
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Helper class

Post 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)?
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Helper class

Post by hallsofvallhalla »

Oh wow thanks for the link! Reading now.
Post Reply

Return to “Advanced Help and Support”