• Advertisement
Sign in to follow this  

Code organisation?

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

Alright, I'm planning to make an RPG game with some friends. But i don't know HOW to organize my code. I want it to be easy to understant for me and the other people that are going to work with me on this. The game will be made in C++. Don't get me wrong I'm not a newb in programmation so i can do a lot of stuff, but that will be the biggest project I'v made so far. So what I'm searching for is maybe some game with the source to take a look at the code organisation, or some tutorials for RPG game that talk about code organisation ( if such tutorials do exist ). Thank in advance.

Share this post


Link to post
Share on other sites
Advertisement
Sit down with the group and break the project into its componets.

Maps, creatures, players, network/graphics/sound interfaces, spells, items ...
Then flush out the interfaces on these parts. This means determining how
you want to update your map, creatures and players. What actions do you want
each to take. Then once everone knows that they can do something like:

a_creature->Update ( );
if ( a_creature->HasTarget() )
t = a_creature->GetTarget();
...

then you can assign people from the group to work on each of the parts, knowing
that the interfaces can't change. They can work inside each function (ie
GetTarget) and provide whatever implemention they want. That way you dont have
to figure out that the map will be a 2d array implemented as vector<vector<int>>.
You can just say that whoever is implementing your map design has to give a GetTile (X,Y) on the map class.

Then the code is already organized into classes, and all you have to do at the end
is make a game loop arround your classes calling update and such. Since you are working
in a group, this should help with the problems of everyone HAVING to read everone else's
code. Each person only has to look outside their code to see the class interface
that others have set up.

Share this post


Link to post
Share on other sites
Thank about the info, but my biggest problem would be how do i setup my class object so they can be call from where i need em? without having different object of a same class to avoid Data to be lost/non-accessable

Share this post


Link to post
Share on other sites
Work from the top down, something like:
Map
-Creatures
--Items
--SpellBook
---Spells
-Items
--stat_changes
-Tiles

Then you know that the map holds all the objects.
Then a map update can pass each Creature a pointer to the map.
This pointer would let each creature ( unable to see other creatures directly )
query the map for the next closest creature(or player) to act on.
(Prolly kinda bad style, but can't think of better.)

All your data is stored in the map, and Each object is it's own entity that asks
it's parrent to inform it about other entities.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Design all your major modules with uml (or on paper at least). Work out all the dependancies and interconnections and then draw up interfaces to those modules.

Once this framework is in place, individual programmers can tackle particular modules with little or no changes to the basic framework. If framework changes are required, then everyone should be involved in the decision.

When it comes to coding, use namespaces and make as much of your classes private as possible. Make use of accessors and add sanity checks to these accessors.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathwearer
Thank about the info, but my biggest problem would be how do i setup my class object so they can be call from where i need em? without having different object of a same class to avoid Data to be lost/non-accessable

Well, that's software design and engineering. We can't give exact advice on how exactly you should go about coding it (since that would require us to lay everything out for you), but we can offer little tidbits and pieces of advice on how to go about designing it.

My advice is this: prototype prototype prototype. Don't start making the big project all at once. Make lots of smaller apps and build up a base code. Once you've got a nice codebase, starting larger projects will make things much easier, and you'll have more experience designing it because you'll have coded all those other apps.

Good luck ;)

Share this post


Link to post
Share on other sites
You're probably going to want to use a polymorphic container of some sort.

You might need a hierarchy for storing many objects in different zones etc.

Anyway - my advice is, make it using the simplest polymorphic object container system that you can (say a STL vector of BaseObject *).

Then once you find that that's no longer practical (maybe if there are 10,000 monsters in your world, you don't want to process them all every loop tick), you could split it up into a different system.

Consider how you're going to handle persistence early on, because a RPG will definitely need a load/save feature.

Mark

Share this post


Link to post
Share on other sites
Keep your code organised into sections.
E.G.:
-Core engine code
--Core input
--Core output
--Core networking

-Game code
--GUI code
--Player skills code
--Items code
--Game specific network code

Also, another great tip is to keep all references to files in a seperate file, and then use that file as a reference to the file. This avoids you having to look through all of your code to find a couple of hard-coded filenames that you may have changed.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathwearer
Alright, I'm planning to make an RPG game with some friends. But i don't know HOW to organize my code. I want it to be easy to understant for me and the other people that are going to work with me on this. The game will be made in C++. Don't get me wrong I'm not a newb in programmation so i can do a lot of stuff, but that will be the biggest project I'v made so far.

So what I'm searching for is maybe some game with the source to take a look at the code organisation, or some tutorials for RPG game that talk about code organisation ( if such tutorials do exist ).

Thank in advance.


Your questions goes to the heart of software engineering & software architecture design. Congratulations :) But seriously, at your level, I would just recommend taking out some paper and a pencil, and drawing lots of boxes with names. Conceptually, what are all the objects in your game? (Player, Alien, Bullet, Screen, etc...). Then, think about all the functions those objects should be able to perform (Player:move, Player:shoot, Screen:redraw, Game:processAI, Alien:move).

If you do a good job, you'll start to notice interesting patterns. For example, in a shooter, you'll notice that the player character (eg: a spaceship) does a lot of the same things as the aliens (shoot, move, get drawn to the screen, etc...). That's when the word "inheritance" should enter your mind. You can put common functionality into a parent class, and then have objects implement/override functions for their specific needs:

GameObject->Player
GameObject->Alien

Eventually, you'll want to make use of design patterns. "Design patterns" are tried-and-true ways of structuring code and classes to solve particular problems. A lot of games use the "Model View Controller (MVC) Pattern". I won't get into it here, but if you Google it, you'll find lots of info. Note that the MVC pattern (and patterns in general) doesn't apply to just games or desktop software. MVC is also quite popular in web applications. (The Java Struts framework is designed to make it easier to implement a MVC design for a web site).


Share this post


Link to post
Share on other sites
Thank for all the reply everyone. I was curently desingning the contain of the game a bit. I have some question tho.

I will follow the way that game told me that everything was stored in my Map class. Easier to keep track of my class if everything is stored here.

Since it's an RPG Sometime when you load the object you have to make are different. How do handle the object creation at the loading? Any tutorials that talk about it ( I will go visit the Software engineering section right now )

Share this post


Link to post
Share on other sites
Didn't found anything that could help me in the software engineering section. What would be the best is a similar project (smaller doens't matter) where i could watch and understand how the code is organized.


When it come to drawing, i do a drawing class? then when I'm about to draw something like a char or an object on the map, i call my drawing scene and draw the object passing the name of it? ( and it have been init in the constructor at the map load i guess ? ) But how i know how much Surface i need to define in my header files? using vector? but how do i reconize my surface now? :S it's bugging me. Never made such a big project.

Share this post


Link to post
Share on other sites
First, sry no samples...
Second, there are 2 stratagies you can use:
1) Everything mangages itself.
You have your map loop through every unit and unit->Update() on everything.
Then you unit->Draw() everything.
The code to display something on the screen is stored inside that thing. When it becomes handy, you make a graphics class that contains helper functions, like
IsXYvisible(x,y), and "bmppointer LoadBMP()" and "DisplayBMPatXY(bmppointer,x,y)"
but every object decides how to call each of those functions, and stores all the graphics information inside itself.

2) Something manages everything else
You pass the map into a drawing class. The drawing class extracts the infomation
it needs and processes each object. You can store drawing infomation in a resource map, so that the graphics are not in the units but in a list that maps unit types to graphics. That list is only visible from the graphics system.

Im sure someone can give Pros/Cons to each way. In your group project the second may be better.
If you flush out the design, then the graphics person will know that a player will allways contain "my_sprite_pointer" and so the graphics class can just loop through the players and "display(unit->my_sprite_pointer)" and you can be sure that since the graphics rutines are all in one place you dont have to worry about each person comming up with a different way of drawing each object when they make the code for their objects.

Share this post


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

  • Advertisement