Helper class
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Helper class
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?
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?
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Helper class
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.
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: Helper class
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.
Hope it helps.
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Helper class
I think I can make it work with polymorphism and interfaces.
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Helper class
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.
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;
}
}
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Helper class
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;
}
}
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: Helper class
Glad it's working with Interfaces
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Helper class
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
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Helper class
Oh wow thanks for the link! Reading now.