need a solution to decides logic at runtime

Started by
23 comments, last by _moagstar_ 14 years, 9 months ago
Ok, hear is the story. I am going to be building a game console/chat system for a game engine and will be using C# as the language for this game console/chat system. The person using this game console/chat system will be able to type in commands by starting them with a forward slash (/) so "/playtime" would execute code that will display the playtime in the game console window. The person can also just type in normal text without the forward slash which would just internally call the say command and message out what the person typed to the console window. The way I thought I was going to do this is have a class for each command and have each class inherit from iconsole_command interface and then just create the instance of the object during runtime and call the execute method. I was then directed to try to find a different way to do this as it can be quite slow, which I agree. The one thing I want to be able to do it keep the code the manages the execution of the commands as independent form the actual command code, so that when I want to add a command to the system, I will not need to change the code the manages the execution of the commands. Someone suggested I try the strategy pattern and while that solves the issue, it seems if I used that pattern, the code executing the commands would need to know what commands are available which create more coupling than I want. Is there a better design pattern/way of doing this?
Advertisement
I would suggest that you integrate a scripting language (perhaps lua), and just feed the console commands into that. It is a bit more work on the setup end, but the payoff in features and flexibility is (IMO) well worth it.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

A simple solution would be to have a dictionary that maps command strings to command handlers, a command handler either derives from an interface or it's a delegate:

Dictionary<string, CommandHandler> commands;

A scripting language might be best, but the strategy pattern does not increase coupling. You perhaps misunderstood it if you think that it does.
Actually, the game engine I will be building the game console for is using Mono for its scripting engine, so technically, C# is the scripting language for the game engine (even tho I would not call C# a scripting language).

I will take a look at using a dictionary as it seems like a clean and easy possible solution.
Quote:Original post by 3dmodelerguy
Actually, the game engine I will be building the game console for is using Mono for its scripting engine, so technically, C# is the scripting language for the game engine
If you already have a scripting language, that is even better - I had assumed you were starting from scratch [smile]

I used to have a few tutorials on integrating in-game consoles with scripting languages, in my file of links, but I seem to have misplaced them. At any rate, since you already have the scripting language integrated with the engine, it would make very little sense not to wire it up to the console.
Quote:(even tho I would not call C# a scripting language).
C# makes a very good scripting language - unity3d use it to great effect.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by 3dmodelerguy
Actually, the game engine I will be building the game console for is using Mono for its scripting engine, so technically, C# is the scripting language for the game engine (even tho I would not call C# a scripting language).

I will take a look at using a dictionary as it seems like a clean and easy possible solution.


In .Net 4.0, all .Net languages can become scripting languages natively. You can already do so some .Net with certain framework implementations. Embedding IronPython in a c# app took a matter of hours to get down.
Good mercy. Look at the number of people suggesting you use bulldozers to move a teaspoon of sugar into a demitasse cup. This is the sort of thinking that made Vista the model of performance that it is.

If you have a small handful of commands and they mostly result in having some function called, let me suggest you do the equivalent of

if (cmd == "playtime")
do_Playtime();
else if (cmd == "quit")
do_Quit();
...
else
CmdWindowingSystem << "Excuse me?";

Granted, if you have need of deferred command processing, or your commands are all variations on a complex theme, then getting into classes and dictionaries and full blown scripting langauges may have some appeal. But if it's simple verb recognition, save yourself the elaboration of languages, containers and classes, and just write some if-then-else. It's not as cool but it is vastly simpler, and easier to read.


Quote:Original post by ScottMayo
If you have a small handful of commands and they mostly result in having some function called, let me suggest you do the equivalent of

if (cmd == "playtime")
do_Playtime();
else if (cmd == "quit")
do_Quit();
...
else
CmdWindowingSystem << "Excuse me?";
That is a very verbose, painful and inflexible version of the dictionary suggestion. What exactly do you think your if/else approach offer over a map of strings to functions?
Quote:But if it's simple verb recognition, save yourself the elaboration of languages, containers and classes, and just write some if-then-else. It's not as cool but it is vastly simpler, and easier to read.
Sure, a scripting language may be overkill at this stage in the game (though a dictionary certainly isn't), but what happens 6 months down the line when the engine has been expanded many times, and you need to add another 20-odd functions to your console on a weekly basis?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Sure, a scripting language may be overkill at this stage in the game (though a dictionary certainly isn't), but what happens 6 months down the line when the engine has been expanded many times, and you need to add another 20-odd functions to your console on a weekly basis?


... you get the code to a major title I worked on a few years ago, where a single if-else chain had grown so large that it exceeded the limits of the compiler. (GCC for PSP, IIRC). =)

This topic is closed to new replies.

Advertisement