Jump to content
  • Advertisement
Sign in to follow this  
3dmodelerguy

need a solution to decides logic at runtime

This topic is 3281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
A scripting language might be best, but the strategy pattern does not increase coupling. You perhaps misunderstood it if you think that it does.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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). =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!