Text based game - Need Help

Started by
7 comments, last by evolutional 19 years, 4 months ago
Hello, I am writing a text based game in C# and i am trying to figure out how to structure the code. How would i impliment objects such as a chair or a torch? Any information would help! thanks!
Advertisement
You mean something you can interact with? You could have a .txt file with a list of objects and the text response. The objects could be denoted by a special symbol like ##, used nowhere else. Then, whenever something is inputted you could search the file for the exact string typed. I don't program in C# (yet) though so I can't give language specifics.
Hmmm... sounds like classes to me!
I program in c++, so I don't know what you have in c#...
.:<<-v0d[KA]->>:.
Thanks for the information!

Basicly, im trying to figure out what the best way to create things and be able to access and modify them.

For example:

Lets say I have a torch, a chair and an apple and i want to set atributes to each of them.

is it better to set each item as a class and create a new object for each time one is invoked?
Quote:Original post by navilon
Thanks for the information!

Basicly, im trying to figure out what the best way to create things and be able to access and modify them.

For example:

Lets say I have a torch, a chair and an apple and i want to set atributes to each of them.

is it better to set each item as a class and create a new object for each time one is invoked?


This would really be a lot like a MUD wouldnt it? So you might want to search for some tutorials on how MUD's interact with the environment, might give you some idea's.
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Hey,

If you are thinking about how to structure your code, I would recommend that you have a look at the text game creation tool called Inform. Inform uses a very sensible approach to handling ingame things in an object oriented way. It might be a good source of inspiration even if you don't want to use the actual Inform language. Nothing wrong with writing in Inform, but sounded like you want to work in C#.
Alexander "Siker" LjungbergNorwinter Studios
we actually have to write a text adventure for an assignment. we are given a small api, and its pretty extendable. you can take a look at how things are structured here, seems like a pretty good way for a basic engine.

http://courses.cs.vt.edu/~cs1705/api/

and go to cs1705.adventure
---------Coming soon! Microcosm II.
If I were you, I'd have a single class for all items in your game. Each item can be an object of that class and the class would store all the information you need about the object such as the name, description, whether it can be taken and stuff like that.
I'd also have a single class GameObject which posesses a dictionary for the properties. Naturally, you'd have a standard propertyset (Name, Description, etc) that would be part of the main class, but any other attributes that are likely to differ across the different objects would be well stored in a custom Dictionary (just wrap the DictionaryObject and add GetValue/SetValue code). This will allow you to refer to the extended properties by name:

GameObject chair = new GameObject("Wooden Chair");

chair.Properties.SetValue("weight", 100);

Console.WriteLine("The " + chair.Name + " has a weight of " + chair.Properties.GetValue("weight") );


With this method, you could quite easily serialise the object instances to a media set for loading/saving/exchange as it's a simple case of iterating the propertyset and reading/writing into it.

You can also create some helper functions to set up an object, for example if you know you'll be creating 100 chairs, it makes sesne to write a "CreateChair" method to set up all the default property values for the object.

This topic is closed to new replies.

Advertisement