The code design of a game (C#)

Started by
10 comments, last by Flimflam 11 years, 8 months ago
I think im relatively good at C#.
What I think im not relativey good at is designing games properly.
I have a few (probably basic) questions below, answering even one will be appriciated.

1. How much should I include inheritance? i.e. should I make a base Character.cs class that represents any character in the game, whether it be a player or a NPC, then have the Player.cs class inherit from the character class and NPC.cs inherit from the character class aswell? or am I just trying to simplify to much and instead just have two seperate files non-related?

2. Im trying to learn somewhat off terraria's source but its really messy and doesnt use things such as inheritance. Please recommend any well designed games that has its source available to learn off (C# would be nice).

3. I get lost in the logic of any largish code base quickly (even if its my own). What can I do to improve this problem?

Replies appriciated thanks.
Advertisement

2. Im trying to learn somewhat off terraria's source but its really messy and doesnt use things such as inheritance. Please recommend any well designed games that has its source available to learn off (C# would be nice).


If you are using XNA, I would look through some of these: http://create.msdn.com/en-US/education/starterkits/, and poke around for other code examples on that site. There are many examples, they have been designed by some smart people, and they even include comments wink.png

1. How much should I include inheritance? i.e. should I make a base Character.cs class that represents any character in the game, whether it be a player or a NPC, then have the Player.cs class inherit from the character class and NPC.cs inherit from the character class aswell? or am I just trying to simplify to much and instead just have two seperate files non-related?


I am not experienced here, since I am a new game developer, but from what I have researched an entity/screen system seems to be best. This has a lot of inheritance, but not in the way that MainCharacter->Character->BaseCharacter->Thing->Object or something really complicated. If it is a noun, it is its own entity. FlatRedBall is an engine/framework that I am using which has taught me this. They have a lot of documentation that I find pretty useful.


2. Im trying to learn somewhat off terraria's source but its really messy and doesnt use things such as inheritance. Please recommend any well designed games that has its source available to learn off (C# would be nice).
[/quote]

I don't have specific examples, but any small example is good to get started with. You can find examples just by searching "C# game programming examples" or something like that in Google. Sorry I can't help more there.


3. I get lost in the logic of any largish code base quickly (even if its my own). What can I do to improve this problem?
[/quote]

This is something you learn from experience. The one thing that you can "learn" from other people is MAKE COMMENTS and lots of them. I always thought them tedious and wastes of time, but the larger projects get, the more I realized they are necessary. I have blocks of comments that can be several lines long, but as long as it describes what is going on, then it's worth it. Later, even just a simple line could confuse you if you aren't careful.

What I'm trying to say is (1) make loads of comments that make sense to whomever might end up reading them--most importantly, you and (2) read moar code! The more you read code, the more you will learn to sort things in your brain without making a conscious effort to.

Hope this helps!

-Ingshtrom.
Question 2:
Learning from the terraria source is a VERY bad idea. If you copy and paste even 1 line you are liable to copyright infringement charges. Using a dedicated XNA tutorial is a far better idea. riemers.net has good XNA tutorials and the microsoft code samples are well commented to tell you what they are doing and why, most of them are for the older xna 3.1 version.

Question 1:
As for the player and NPC thing. Defining a class character (which can probably deal with collision rules and rendering etc) and then inheriting from it to create NPC's and players would be absolutely fine.

Question 3:
I assume seeming as your using XNA your using visual studio (or one of the express editions).
Lets assume we're working on the NPC/player/character system from above.
Under the solution manager there will be 2 projects ([name] and [name]content most likely, or something similar anyway).
Right click your actual project (not the content project, although this style can be applied to your content project anyway)
Click add > New Folder then call it something like "Characters".
Then we'll click Project (near the top) > Add new item and we'll add a class called Character.cs
It will appear in the solution manager, click and drag it into your folder, the compiler will still be able to find it no issues.
Then do the same with 2 new classes Npc and Player and add them into the folder.
These 2 classes will both inherit from Character. The compiler will be able to find character no matter which .cs file you've put it in.

Just keeping 1 little folder for those 3 classes just keeps it out of the way of your main code so you don't end up with a 50 billion line file. Any other related classes you can do the same, give 1 folder for them and give each one its own source file. It might not be worth giving the class its own file if its only 5 or 6 lines or something though, thats just not needed, if it inherits from another file add it to the end of that instead (lets say zombie spearman inherits from zombie but is only 15 lines, stick that class on the end of the zombie class instead, or if there are multiple zombie variants all around 15 lines perhaps have a zombie.cs and zombieVariant.cs that contains all the classes inheriting zombie)
It is possible to go overboard on this technique making too many folders but assuming you don't do that it really help clean up your main source file and aids editing of classes a bit by not having to view tonnes of un-needed code all the time.

Another tip, next to where it says something like "protected override void LoadContent()" in the code editor theres a little "-" square, click it, the "-" will be replaced with a "+" and that code block will be hidden from view (apart from the first line). Load content is a block you might change frequently so that might not be the best one to minimise but some other code block that you don't tend to alter much might be worth minimising so you don't have to scroll through regularly.
Thank you for the replies, I'm just mainly getting confused in the logic about networking. I am actually programming two seperate programs and one library. One program is the client, the other program is the server, the library has data such as the verification code for connecting, later on i may add to the library the list of abilities in the game and the stats of each ability (how much damage does it do blah blah...). Managing TWO applications just seems really hard, my client (game) program has 34 classes, my server application has 12 classes, my library has 2 classes.

Ive actually read the whole 2D section of the book Learning XNA 4.0 but sadly (and not surprisingly) is does not go into how to struture a large game well, and thats really what I would like to know before I go attempting to make a real game.

About terrarias source: theres no way id copy anything haha. its not the real source anyway.

Best thing for me to do now is look at them linked real code examples, thanks for the replies! [size=1](again).
Are there any classes that can be shared between both the client and the server, move them into the library and have the client and server both inheriting from/using that where needed. THe character class would probably be useful to have both client and server side for example
Yeah i thought about that, the only problem is how would you split up the drawing code? For example the Player.cs class could have many fields (including a texture field that saves the texture of the player in memory), if the server were to run the same code this would mean memory is being wasted. I am trying to develop a high-priority single-threaded server [size=2](may distibute the CPU core use though TPL though later on though) that uses as low memory as possible which will hopefully allow many clients 300+ to connect at once.

Is there a easier way that can enable me to just write once which wont produce a waste in server resources?
If your inheritance tree is rather small, you could use inheritance to create a client and a server version of your entities and provide your world class, or whatever class is responsible for managing the entities, a factory object that knows what specialization to use.

Or, to make it more generic, you could try a more component based design where you have almost no inheritance at all. Instead, you can create different types of entities by attaching different sets of components to them. On the server, you could then simply leave out whatever component is responsible for managing graphics resources, while the client may not need to have a component for handling AI.

It's hard to recommend a specific design without knowing more about your game's world.

current project: Roa

lwm, your second paragraph is probably most simple and managable, and it was similar to what I was doing already, ty :)
First off, forget about the code and its specifics entirely, think of the game's design (at concept level), try to think of it as a non programmer, what are its parts? what parts have stuff in common and what? if you have an orc and a human, are they the same think logically speaking and simply have different content and values?

Yes, a generic Character or Actor class is usually a good start, in complex games it may even bee too specific already, Entity would be the most basic object, with just the minimum data to put a static thing up there in your world and have it go through all your systems properly (render, collision, audio, rtc.)

The best code design (IMHO) is the one that provides clear processses and easy ways of adding new stuff, take for instance the character, you start your development and want your character to be able to move and to have a melee punch hability, so you make the character class and implement those habilities into it.
A month later into development yo decide the character needs a secondary ranged attack you didn't plan for originally, spaguetti code enters!
Instead if at design time you acknowledge characters will have an undetermined number of skills, and that different characters will have completely different skills, you'll realize that what your character actually needs from the code is to have a dynamical skill system which you can add from script or some other simpler source like config files.
Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


This topic is closed to new replies.

Advertisement