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.

Helper class

Postby hallsofvallhalla » Mon Jan 22, 2018 4:58 pm

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: 11933
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 156 times
Blog: View Blog (3)

Re: Helper class

Postby hallsofvallhalla » Mon Jan 22, 2018 5:17 pm

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
hallsofvallhalla
Site Admin
 
Posts: 11933
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 156 times
Blog: View Blog (3)

Re: Helper class

Postby vitinho444 » Mon Jan 22, 2018 5:51 pm

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
vitinho444
 
Posts: 2825
Joined: Mon Mar 21, 2011 11:54 am
Location: Portugal
Has thanked: 64 times
Been thanked: 17 times

Re: Helper class

Postby hallsofvallhalla » Mon Jan 22, 2018 9:29 pm

I think I can make it work with polymorphism and interfaces.
User avatar
hallsofvallhalla
Site Admin
 
Posts: 11933
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 156 times
Blog: View Blog (3)

Re: Helper class

Postby hallsofvallhalla » Tue Jan 23, 2018 5:49 pm

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: 11933
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 156 times
Blog: View Blog (3)

Re: Helper class

Postby hallsofvallhalla » Tue Jan 23, 2018 6:30 pm

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
hallsofvallhalla
Site Admin
 
Posts: 11933
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 156 times
Blog: View Blog (3)

Re: Helper class

Postby vitinho444 » Wed Jan 24, 2018 8:04 am

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

Skype: vpegas1234
User avatar
vitinho444
 
Posts: 2825
Joined: Mon Mar 21, 2011 11:54 am
Location: Portugal
Has thanked: 64 times
Been thanked: 17 times

Re: Helper class

Postby Jackolantern » Wed Jan 24, 2018 8:46 pm

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
Jackolantern
 
Posts: 10888
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: Helper class

Postby hallsofvallhalla » Thu Jan 25, 2018 11:31 am

Oh wow thanks for the link! Reading now.
User avatar
hallsofvallhalla
Site Admin
 
Posts: 11933
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 156 times
Blog: View Blog (3)


Return to Advanced Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

x