10-26-2004, 07:32 PM | #1 |
New Member
Join Date: Apr 2002
Posts: 21
|
So I'm messing around with writing a mud. It'll never get done, but its a mechanism for me to get better at a few skills.
Anyway. One of the things I need at a basic level is a routine that matches up the command input by the player with a method call in the engine. I suppose the most basic way of doing this is to use a massive switch statement, but I was wondering if anyone has a more elegant way of doing it? Thanks, Dub |
10-26-2004, 07:58 PM | #2 |
Senior Member
|
Of course there's a better way to do it =). Despite being a horrendous design decision, gigantic lists of switch and if statements don't cut it because they can't do partial matching of a command name.
You most likely want to store command objects in a dynamically extendable table. Of course it varies from mud to mud, but in the command object I keep an access control (so that the 'smite' command is not made available to normal players), the name of the command, a list of target specifications, and a function pointer or pointer to member function or delegate or code object or whatever so you can call the code you need when necessary. The target specification is also very important and very overlooked. Many commands such as 'tell', 'kill', 'give', etc., are targetted at an element in the game. It should be the command dispatcher's job to locate those targets specified in the command string and pass them as an array when the actual command code is called into. I refer you to the section on in my Aetas Concepts and Architecture document for some examples of how I define commands. |
10-26-2004, 08:59 PM | #3 |
New Member
Join Date: Apr 2002
Posts: 21
|
Yes, I thought of a pointer to function, but I am using C# and don't know how (if it is possible without unsafe code) to do that. I'll think about it for a bit.
|
10-26-2004, 09:51 PM | #4 |
Senior Member
|
Not only is it possible with C#, it's much, much easier because you can use either delegates or reflection to get at what you need. I would suggest reflection since you can't retarget (change the 'this' target) of a non-static delegate, and static delegates always have a null 'this' target. It's a terrible limitation I know, but they don't have me designing these languages so I'm content to merely complain about them on mud forums...
The reflection method is very easy. Say you've got an object called 'Player' which can execute the 'CmdLook' method when the player types 'look' [code] using System.Reflection; ... Type playerType = typeof(Player); MethodInfo lookMethod = playerType.GetMethod("CmdLook");[/quote] Now say you want to invoke the method with a played named 'actor' as 'this', a target player named 'target', and a string of miscellaneous text named 'args': [code] lookMethod.Invoke(actor, new object[] {target, args});[/quote] Which will jump into code like this on the Player object: [code] class Player... public void CmdLook (Player target, string args) { ... }[/quote] It's extremely simple. Look into the System.Type.GetMethod method. |
10-26-2004, 11:01 PM | #5 |
New Member
Join Date: Apr 2002
Posts: 21
|
Thanks much, I'll look into it.
|
command processor - Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Features of a MUD command parser. | Drealoth | Advanced MUD Concepts | 15 | 04-27-2006 02:08 PM |
Jog Command | Jaegar | MUD Coding | 4 | 04-09-2004 04:41 AM |
Switch command | Verboden Faction | MUD Coding | 8 | 05-15-2003 09:31 PM |
Command trigger for progs | Jaegar | MUD Coding | 1 | 04-03-2003 06:41 PM |
Chown Command | Verboden Faction | MUD Coding | 9 | 03-23-2003 07:12 PM |
|
|