Public Group

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

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

## 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:

 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(); } } 

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:

So input anyone? Is this a good implementation?

##### 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 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.

Please tell me more about deserialization from string to argument types 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 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.

Please tell me more about deserialization from string to argument types 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[])

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, Microsoft's Guidelines are a good starting point for standardized C#.

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 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 " boilerplate coding " I have seen it in several topics but haven't figured out what it is yet.

##### 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.

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.

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.

Also what is " boilerplate coding " 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 Code Smell that you haven't abstracted the problem properly.

##### 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.

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.

Also what is " boilerplate coding " 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 Code Smell 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
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

I'll do my best to understand it!

##### Share on other sites

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.

It might be of value looking into The Mediator design pattern 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 Command Pattern 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 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.

It might be of value looking into The Mediator design pattern 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 Command Pattern 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

##### 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.

It might be of value looking into The Mediator design pattern 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 Command Pattern 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
[/quote]

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

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
18

• 11
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999990
×