Sign in to follow this  

[C#][Textbased] Command System, input needed.

This topic is 2346 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

Hello all! I'm currently working on a small RPG-style text adventure game. Hold on now! It's not an epic long game of 60+ gameplay hours.
It's a very very small ( still complex though ) text based RPG that has 3 classes which each offers around 20 minutes of gameplay.

Anyways, the game features a very very basic command-system that has the identifier of '/'.
Some examples are:

/pickup, /talk, /climb, /attack, /open ect ect.

I'm sketching on a small prototype of it using delegate's and .Invoke.
This is what i have so far:

[code]
public class Command
{
Delegate Invoker;
public Command(Delegate function, cSystem caller, string[] args)
{
Invoker = function;
Invoker.Method.Invoke(caller, new object[] {args});
}
}
public class cSystem
{
Command a;
delegate void Method(string[] args);
private void Test(string[] args)
{

Console.WriteLine("Hello World, {0}");
}
public cSystem(string[] args)
{
a = new Command(new Method(Test) , this, args);
}
}

class Program
{


static void Main(string[] args)
{
string[] StringArray = new string[1];
StringArray[0] = "By Victor";
cSystem b = new cSystem(StringArray);
Console.ReadLine();

}
}


[/code]

This is really sweet since Command's can access even the private functions of the cSystem. The idea is that cSystem is the main-game class, it contains everything, character, command-lists, location-lists ect ect also all of the functions the commands are going to use when triggered.

As you can see the above example isn't really developed THAT much but it works and you can feed another class functions from the calling class ( cSystem ).
My idea is to have ( ONE ) instance of each command-type in a dictionary, this way when a location or anything else needs access to the command i don't need to create a new instance of it, i can add it to a
List<Command> CommandList = new List<Command>(); that holds refrences to Command-objects like so:
CommandList.Add(caller.FetchCommand(Commands.Open));


So input anyone? Is this a good implementation?

Share this post


Link to post
Share on other sites
It's good enough.

I'd expect validating the number of parameters to be done here, and perhaps deserialization from string to argument types. But those are just gravy. If it does what you need it to then it's a good implementation; don't spend too long analyzing.

Share this post


Link to post
Share on other sites
[quote name='Telastyn' timestamp='1310399312' post='4833782']
It's good enough.

I'd expect validating the number of parameters to be done here, and perhaps deserialization from string to argument types. But those are just gravy. If it does what you need it to then it's a good implementation; don't spend too long analyzing.
[/quote]
Please tell me more about deserialization from string to argument types :o what is that?

PS. I find it hard not to analyze my code more than i really need to, i hate rewriting code :( ( Though that is almost inevidible, right? )
I wan't to kinda keep to some kind of code-standarisation ( A few rules to keep in mind while coding ( like private vars, properties ect ect )
and i really hate ugly looking code xD That is mostly the stuff that holds me back :(
A few tip to keep me focusing on what is important?

Share this post


Link to post
Share on other sites
[quote name='Moonkis' timestamp='1310402414' post='4833801']
[quote name='Telastyn' timestamp='1310399312' post='4833782']
It's good enough.

I'd expect validating the number of parameters to be done here, and perhaps deserialization from string to argument types. But those are just gravy. If it does what you need it to then it's a good implementation; don't spend too long analyzing.
[/quote]
Please tell me more about deserialization from string to argument types :o what is that?
[/quote]

Right now, you pass a series of strings to the various commands. Deserialization is the process of converting from some standard encoding (string, XML, binary formatting, etc) into the actual objects you want to use (ints, coordinates, users, orcs, whatever). It saves you a lot of boilerplate coding to adapt whatever method you actually want to call to fit into void(string[])

[quote]
PS. I find it hard not to analyze my code more than i really need to, i hate rewriting code :( ( Though that is almost inevidible, right? )
I wan't to kinda keep to some kind of code-standarisation ( A few rules to keep in mind while coding ( like private vars, properties ect ect )
[/quote]

Inevitable. Standardization. Want. Etc.

How are you going to standardize in code when you can't conform to standardized English (with the help of spell checkers available in any modern web browser)?

That said, [url="http://msdn.microsoft.com/en-us/library/ms229042.aspx"]Microsoft's Guidelines[/url] are a good starting point for standardized C#.

[quote]
A few tip to keep me focusing on what is important?
[/quote]

Each coding session, start with 'what do I want to get done in (however long you plan on working in that session)?'. Then do only what is necessary to achieve that goal. Having automated tests that evaluate success is super, but not entirely required.

Share this post


Link to post
Share on other sites
Regarding my spelling, English is not my native language nor is it easy for me to spell/use advance words.
I'll try to download a spell-checker if you think that It's really necessary.

As for the serialization of objects ( also deserialization ofcourse! ) I will be researching the topic, preferably I'll use Binary-files because I do not like XML.
Now i have done the mature step of taking and analyze your feedback, I suggest you do the same now.

Their is no reason to act as an elitist by pointing out flawed spelling, I'm not saying you shouldn't, I'm saying that you could try to sound a lot nicer about it, frankly those few sentences gave me the impression of you being a dick, which i don't think you are nor was aiming for.

As of your input, It has been really helpful and I really appreciate it!
Thank you very much!


As for everyone else reading this topic! Please give me more input!
Also what is " [b]boilerplate coding [/b]" I have seen it in several topics but haven't figured out what it is yet.

Share this post


Link to post
Share on other sites
[quote name='Moonkis' timestamp='1310407789' post='4833881']
Regarding my spelling, English is not my native language nor is it easy for me to spell/use advance words.
I'll try to download a spell-checker if you think that It's really necessary.

Their is no reason to act as an elitist by pointing out flawed spelling, I'm not saying you shouldn't, I'm saying that you could try to sound a lot nicer about it, frankly those few sentences gave me the impression of you being a dick, which i don't think you are nor was aiming for.
[/quote]

I was aiming for being just enough of a dick that you'd realize I was being serious, but not the full-blown dick that many people will be about such things.

I do think that a spell-checker would help you make better posts which means more talk about design and less talk about English. Even the simple 'Sorry, but English is not my native language' warning to lead a post goes a long way.

[quote]
Also what is " [b]boilerplate coding [/b]" I have seen it in several topics but haven't figured out what it is yet.
[/quote]

Boilerplate coding is when you do the same thing over and over again with slight variations. It is often a [url="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBUQFjAA&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCode_smell&rct=j&q=wiki%20code%20smell&ei=fUIbTrOiGqPv0gHE68yWBQ&usg=AFQjCNHHFdg7O5TU-9vZ16AbY5waQTMEBg&cad=rja"]Code Smell[/url] that you haven't abstracted the problem properly.

Share this post


Link to post
Share on other sites
[quote name='Telastyn' timestamp='1310409390' post='4833896']
[quote name='Moonkis' timestamp='1310407789' post='4833881']
Regarding my spelling, English is not my native language nor is it easy for me to spell/use advance words.
I'll try to download a spell-checker if you think that It's really necessary.

Their is no reason to act as an elitist by pointing out flawed spelling, I'm not saying you shouldn't, I'm saying that you could try to sound a lot nicer about it, frankly those few sentences gave me the impression of you being a dick, which i don't think you are nor was aiming for.
[/quote]

I was aiming for being just enough of a dick that you'd realize I was being serious, but not the full-blown dick that many people will be about such things.

I do think that a spell-checker would help you make better posts which means more talk about design and less talk about English. Even the simple 'Sorry, but English is not my native language' warning to lead a post goes a long way.

[quote]
Also what is " [b]boilerplate coding [/b]" I have seen it in several topics but haven't figured out what it is yet.
[/quote]

Boilerplate coding is when you do the same thing over and over again with slight variations. It is often a [url="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBUQFjAA&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCode_smell&rct=j&q=wiki%20code%20smell&ei=fUIbTrOiGqPv0gHE68yWBQ&usg=AFQjCNHHFdg7O5TU-9vZ16AbY5waQTMEBg&cad=rja"]Code Smell[/url] that you haven't abstracted the problem properly.
[/quote]

I read a bit about boilerplate coding and I came across an problem I have been trying to solve but haven't been able to do so.
I have a hard time trying to figure out how to communicate between classes in a smooth way without passing references of the class all the time, because someone told me that is bad practice :P
That is why i implemented cSystem that act's like a bridge between classes and their functions and allows them to communicate without having references to each other.

cSystem contains all the ("High-Level") functions that will utilize the classes ( cSystem contains all the diffrent "Managers") functions.
I don't know if this is a good way to do it or if I'm doing the mistake of making it a "God"-object :)
It is a problem that has been itching my brain for a long time! And I did hope maybe you could clarify it for me? But try not to use too many complicated words or terms ( I'll try to look them up as fast as possible ).
It would be really really helpful if you could do this for me :D

I'll do my best to understand it!

Share this post


Link to post
Share on other sites
[quote name='Moonkis' timestamp='1310407789' post='4833881']
That is why i implemented cSystem that act's like a bridge between classes and their functions and allows them to communicate without having references to each other.
[/quote]

It might be of value looking into [url="http://www.dofactory.com/Patterns/PatternMediator.aspx"]The Mediator design pattern[/url] which de-couples ( removes direct connections ) between two objects. To be honest, the scheme you have devices seems to be overkill, unless you plan to make things more complicated in the future ( for example, Plug-In support ). The [url="http://www.dofactory.com/Patterns/PatternCommand.aspx"]Command Pattern[/url] is obviously ( by it's name ), another good fit for implementing commands. Both ( and all the other design patterns on that site! ) are worth looking into.



Hopefully the language on that site is easy to follow with ESL.

Share this post


Link to post
Share on other sites
[quote name='Serapth' timestamp='1310414389' post='4833951']
[quote name='Moonkis' timestamp='1310407789' post='4833881']
That is why i implemented cSystem that act's like a bridge between classes and their functions and allows them to communicate without having references to each other.
[/quote]

It might be of value looking into [url="http://www.dofactory.com/Patterns/PatternMediator.aspx"]The Mediator design pattern[/url] which de-couples ( removes direct connections ) between two objects. To be honest, the scheme you have devices seems to be overkill, unless you plan to make things more complicated in the future ( for example, Plug-In support ). The [url="http://www.dofactory.com/Patterns/PatternCommand.aspx"]Command Pattern[/url] is obviously ( by it's name ), another good fit for implementing commands. Both ( and all the other design patterns on that site! ) are worth looking into.



Hopefully the language on that site is easy to follow with ESL.
[/quote]

Thanks I'll be reading the topics throughly.
One more question!

Serialization seams like a good way to read "Monster" ( Or other entity data ) from a file, but how do you serialize and create each unique aspect of the monster, like AI or dialogs for NPC's for example how do you actually store that kind of "huge" data :o

Share this post


Link to post
Share on other sites
[quote name='Moonkis' timestamp='1310416624' post='4833977']
[quote name='Serapth' timestamp='1310414389' post='4833951']
[quote name='Moonkis' timestamp='1310407789' post='4833881']
That is why i implemented cSystem that act's like a bridge between classes and their functions and allows them to communicate without having references to each other.
[/quote]

It might be of value looking into [url="http://www.dofactory.com/Patterns/PatternMediator.aspx"]The Mediator design pattern[/url] which de-couples ( removes direct connections ) between two objects. To be honest, the scheme you have devices seems to be overkill, unless you plan to make things more complicated in the future ( for example, Plug-In support ). The [url="http://www.dofactory.com/Patterns/PatternCommand.aspx"]Command Pattern[/url] is obviously ( by it's name ), another good fit for implementing commands. Both ( and all the other design patterns on that site! ) are worth looking into.



Hopefully the language on that site is easy to follow with ESL.
[/quote]

Thanks I'll be reading the topics throughly.
One more question!

Serialization seams like a good way to read "Monster" ( Or other entity data ) from a file, but how do you serialize and create each unique aspect of the monster, like AI or dialogs for NPC's for example how do you actually store that kind of "huge" data :o
[/quote]

Generally you would create some kind of tool for level/creature/world design, sharing the same code as your actual game.

Share this post


Link to post
Share on other sites
Yeah, I get that I need some kind of "Editor" ( Monster Editor, Item Editor ect ect ) to create the serialized files ( It needs to have the same .cs files as the main game ).
Then I can simply add them all in the main-game, de-serialize them and use them ingame!

But how do you parse ( that is the name for it right? ) Dialogs , AI's and such? Or do you make a Dialog.cs and AI.cs that contains the Dialog and Logic for the Dialog? add it to the NPC and serialize it all together?
Is that how it commonly works? :) ( Of course all that is done with the "Editor" )

Share this post


Link to post
Share on other sites
3dbuzz got a free video tutorial on how to make an engine for text based adventure games in c#. I know its not exactly what you are looking for but it got som very good tips on handling input.
Here is a link: [url="http://www.3dbuzz.com/vbforum/sv_videonav.php?fid=e4da4800d407d5304c121d86f9f2a97d"]http://www.3dbuzz.com/vbforum/sv_videonav.php?fid=e4da4800d407d5304c121d86f9f2a97d[/url]

Share this post


Link to post
Share on other sites
[quote name='Basse85' timestamp='1310418759' post='4833988']
3dbuzz got a free video tutorial on how to make an engine for text based adventure games in c#. I know its not exactly what you are looking for but it got som very good tips on handling input.
Here is a link: [url="http://www.3dbuzz.com/vbforum/sv_videonav.php?fid=e4da4800d407d5304c121d86f9f2a97d"]http://www.3dbuzz.co...c121d86f9f2a97d[/url]
[/quote]
I have actually seen that one ( Not watched all the videos though ).
And It's a great reference source!

But all in all, my biggest question ( right now ) is how to implement larger entities without hard-coding them all.
By the information I have gathered, reading from a ( database ) file and parsing the strings into objects or serialize/de-serialize files directly into objects.

How do you create a big entity and serialize it, like for example an NPC-entity? With a dialog's that have logic ( and or else if ), call functions that changes different status and the likes, or hold a huge text-dialog.
Monsters with different AI's, Items that differs from normal ones, that calls a function each time it is used in battle and so on.

I know Scripting comes to many of you readers minds, but then i would have too add Lua ( or Python w/e ), and miss the knowledge of how to do these stuff by natively-coding them!
I'm a beginner but I want to learn! :D

Hope you GameDev-foxes can give me quick run through about the theory behind it all!

Share this post


Link to post
Share on other sites

This topic is 2346 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this