Help learning how to structure code efficient and flexible

Started by
9 comments, last by MrAndersDk 11 years, 1 month ago

Hello, I'm new to game programming and new to this forum.

I try to look through the forum, the books and the articles, but the content is so vast that I could not find what I was looking for. My background is PhD in physics and all my programming skills are through my education. I'm not a great programmer and for this reason i started playing around with game make studio, because it seemed simple. I've watched a lot of tutorials on youtube and read some articles and I think I've got the basics down. I want to make games in my past time and not professional and I think I will do it as a one man thing, at least in the beginning.

Now to my problem. Whenever I try to start making a game I loose the overview of the game. I simply don't know how to make efficient structure of the game. That is what to put where, should i use the build in physics engine, should i write something my self. What code should be put in the objects what should be put in scripts, and the list goes on.

All tutorials I have found are very basic, so if a tutorial says it will show you how to make a platformer, it will show you how to make a solid object and a player you can control. I can do that, however my code is very messy and not very flexible. So if I suddenly want to integrate for example a level up system for the player, it will be very difficult. Another example, I can make a player that can shoot and kill enemies, but if i want to add the possibility to change weapons, again it gets very messy.

I hope someone can help me to learn writing efficient, flexible and smart code. In my head it would be very nice if i could write single modules, and then combining them to a game. And also latter easily add a new module if i get a new idea, without having to go through all single objects implementing this new module. Don't know if this is possible. Hope my problem is clear, or else write and I will elaborate.

So to summarize I don't need help learning to program specific code (I learn on the fly, using google and the help file), but help to build the complete structure and combining the individual code efficiently.

Ant type of help would be appreciated: books, webpages, youtube, direct guidance ...

Thanks in advance.

Advertisement
"Game Coding Complete" seems perfect for you.

Other than that, designing software and structuring the code so it stays manageable for a long time are very difficult skills that are acquired over the years, primarily through making mistakes. Try to be a bit thoughtful, but don't let it block you: You are going to be doing it wrong for a long time, but it's an important part of the learning process, so don't worry about it too much.

You should also try to collaborate with other programmers in long-term projects. That's the best way to learn to organize software, because they will call you on your messes, you'll suffer through theirs and you'll be much more aware that you need to write code that is easy to read.

Seem like an interesting read.

You'r right I will make a lot of mistakes, and I know it is a difficult task. The problem is that because I don't know where to start learning the skill, I don't come very far with my projects because they get so unorganized.

It would be perfect to work with others, especially if I could get small clearly defined tasks/modules, that needs to work together with a larger things. However, because I'm such a newbie I don't think I would be of that much help, and I don't think that people would be that interesting in letting me help. It would be just as much tutoring from their side, as me helping them.

But thanks for your answer, I will try not to be that worried about the problem happy.png .

Any other suggestions is still very much appreciated.

You most definitely make mistakes. There are just things that are looks so easy when you see it but when you do it, it will take time to master. If you think your code is not flexible, post it up and let us see if it follows good object oriented principle.

You can expect your game to broken up to multiple classes like this:

Game

Grass

Tile

Player

FireBall

Sprite

GameComponent

Grass

Rock

[...] If you think your code is not flexible, post it up and let us see if it follows good object oriented principle.

From comments like that one could get the impression that object-oriented programming is the only way to make your code flexible. This is not the case. It might not even be the best way to achieve flexibility. Exposing the right parts of the program to a scripting language, for instance, can make the code very flexible. Or using callbacks ("signal/slot" is a more modern version of this). Or writing little processes that can be plugged in all sorts of ways (OK, maybe not very useful for games, but that's how Unix achieved a lot of flexibility).

Objects are just one tool of the programmer. Not everything is a nail.

It's OK if things are unstructured when you start. It's part of the learning process and as you write more code you'll get a feel for what works and what doesn't.

That being said, the book "Code Complete" is a tome but immensely useful (this is different than "Game Coding Complete" and places more of an emphasis on general programming best practices). Reading up on design patterns would be helpful as well. It's not a silver bullet but it will give you an idea of how some common problems are solved and how the solutions are structured.

Finally, I'm not sure what you do for a living, and this is probably absurdly impractical advice but here it goes: One of the fastest ways that I improved my software engineering was by working at a software company with a lot of smart engineers. Things like peer code reviews are extremely helpful, as is working in a large codebase and getting a chance to see how other people do things. Again, I realize that you're not gonna switch careers just to improve a hobby skill (assuming that this is just a hobby), but the point is that if you find opportunities to read a lot of code and discuss code with other people, it will help a lot. Maybe looking at some open source projects would be helpful, as long as you somehow know in advance that the code in the project isn't crap to begin with :]

Only that which doesn't change can ever be perfect.

Make mistakes. Learn from those mistakes. Make new mistakes and learn some more. I get caught up in the idea of perfectly beautiful code a lot too... granted I'm slightly ocd. Like learning another (speaking) language you're far more likely to learn threw use of that language.

Beyond the vague. A blank piece of paper and pen do wonders for me in visualizing and keeping track of my original idea. I have one of those 2 inch thick notebooks for each of my projects. And a filing cabnet full of them. Before I code I tend to let my brain puke all over it. I find it the fastest and easiest way to get my ideas out. Granted I tend to close it and tbrow it behind me right afterwards then code. But I find Ok write less hackish code...

Its fun to scan over some of my old ones just to see how I thought about programming years past. I had a lot of bad techniques that make me cringe now a days ;)

Keeping things clean is usually a result of me going back over code and actually cleaning it up. Getting the code done and working is more important to me though.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

well said, and we could add a little famous quote : "perfection is achieved not when there is nothing left to add but when there is nothing left to take away".

Also read about Entity Component based Systems. There are 2 beautiful introduction articles on the webside of Ash framework.

Last thing I want to say, is : refactoring will help you. go for "whatever works" first, then you'll have the perspective to see what needs to be improved. it is much easier to improve on something existing that creating something perfect from the first attempt.

Hello, I'm new to game programming and new to this forum.

I try to look through the forum, the books and the articles, but the content is so vast that I could not find what I was looking for. My background is PhD in physics and all my programming skills are through my education. I'm not a great programmer and for this reason i started playing around with game make studio, because it seemed simple. I've watched a lot of tutorials on youtube and read some articles and I think I've got the basics down. I want to make games in my past time and not professional and I think I will do it as a one man thing, at least in the beginning.

Now to my problem. Whenever I try to start making a game I loose the overview of the game. I simply don't know how to make efficient structure of the game. That is what to put where, should i use the build in physics engine, should i write something my self. What code should be put in the objects what should be put in scripts, and the list goes on.

All tutorials I have found are very basic, so if a tutorial says it will show you how to make a platformer, it will show you how to make a solid object and a player you can control. I can do that, however my code is very messy and not very flexible. So if I suddenly want to integrate for example a level up system for the player, it will be very difficult. Another example, I can make a player that can shoot and kill enemies, but if i want to add the possibility to change weapons, again it gets very messy.

I hope someone can help me to learn writing efficient, flexible and smart code. In my head it would be very nice if i could write single modules, and then combining them to a game. And also latter easily add a new module if i get a new idea, without having to go through all single objects implementing this new module. Don't know if this is possible. Hope my problem is clear, or else write and I will elaborate.

So to summarize I don't need help learning to program specific code (I learn on the fly, using google and the help file), but help to build the complete structure and combining the individual code efficiently.

Ant type of help would be appreciated: books, webpages, youtube, direct guidance ...

Thanks in advance.

I have the same problem. You need to study everything as a building block. You have a phd in physics so that shouldn't be hard. :P

Pretend you are playing with lego's. The entire structure can be complicated as a whole, but broken down, it's pretty simple. In fact,

once broken down to the individual blocks, the mechanics behind the build are pretty intuitive. Unfortunately the building blocks of

programming languages are not as simple as lego's, but once you start learning to keep looking at the basic blocks, you start to

intuitively get a better grasp of whats efficient and what isn't. It's easier said than done, but this IS the way to view it.

Start by learning the basic types and the general do's/do not's on how to use them. Then use those blocks to build a small, tightly

packed (i.e. efficient) larger lego structure. Now that you have that efficient block put together, you can stop looking at the

underlying specifics behind it every time you need that function, and use it to build and even larger lego structure made up of other

tightly packed, pre-structured code. Now again, easier said than done. I have learned that you will eventually hit parts of code

where it seems so abstract and non-straight forward, like a dot product can be, that it's hard to find an efficient way to string

together blocks into an efficient struct. It's the same in physics. It's easy to calculate gravitational effects on a baseball thrown

in an arc on earth. It's not so simple to calculate the effects on a baseball thrown into a binary-blackhole system. :P Sometimes

you'll just run into problems that seem so abstract and removed from a certain paradigm you are use to working on, that it will just

be a mother*******. :P But remember:

KISS.

Keep. It. Simple. Stupid.

I constantly have to remind myself of that. I'm extremely tired, and still, I have to retrain myself to break things down like this

no matter how tired or discouraged I am. Once you train to the point that you start noticing that programming certain types

of problems efficiently becomes intuitive, you'll be more motivated and actually possibly come to enjoy sifting through some

forum posts, which were seemingly inane previously, on how to improve this algorithm or that. Patience is a big factor. I am

no where near expert, but I have noticed the more time I invest in the basics instead of trying to skip ahead and make

something, the more I just naturally understand. Having a high level degree, you have already experienced the kind of

pace you need to think at when programming. It's long term, methodical work. And as usual, it's best to not try to cram

overnight before the big exam. No one writes a Far Cry 3/Battlefield 3 overnight, especially not as a loner, fly-by-night

programmer. The more methodical and slow your approach at programming in the beginning, the faster you will actually

begin writing better code. Also, don't forget to do back-research. You should have some understanding of different os

platforms since each handle windowed apps differently (unless you use scripted languages which are often able to handle

much of the thinking there for you). You also need knowledge of your individual compiler, and os file system/path

requirements.This is currently annoying the hell out of me. I have been fooling around with python seriously for the past

year, and I just started earnestly trying to learn C++. C++ requires all kinds of weird linked in file types and organization.

The IDE/compiler alone feels more complex than any of the C++ programming concepts themselves. blink.png tongue.png

But again, that's because I naturally slip back into viewing the IDE as a whole rather than it's simple pieces. KISS. Lordy KISS,

KISS, KISS. angry.pngbiggrin.png

In short:

- Learn the general overview of the problems you need to solve. Then find out the basic starting points you need to

start solving the smallest problems. You can't solve 3d rotation without trig, and you can't figure out trig without

understanding basic arithmetic.

- Remember to break the whole problem into manageable pieces that you easily, and intuitively can solve. Those then

combine with the other smaller pieces you have already solved.

- Keep reminding yourself to not throw yourself headlong into a brickwall trying to solve the problems immediately.

It causes undue stress, making the problems even harder to solve. If you are apt to doing this out of your very nature

as I am, I feel sorry for you because I know what you are in for. :P But again, keep working at it, while just taking up

the additional task of purposefully, and intently, retraining yourself to break this habit and remind yourself of the

mindset needed to solve the paradigm at hand.

Have fun. Once I get the initial roadblocks out of the way, I know I will be doing so. happy.png

Only that which doesn't change can ever be perfect.

Make mistakes. Learn from those mistakes. Make new mistakes and learn some more. I get caught up in the idea of perfectly beautiful code a lot too... granted I'm slightly ocd. Like learning another (speaking) language you're far more likely to learn threw use of that language.

Beyond the vague. A blank piece of paper and pen do wonders for me in visualizing and keeping track of my original idea. I have one of those 2 inch thick notebooks for each of my projects. And a filing cabnet full of them. Before I code I tend to let my brain puke all over it. I find it the fastest and easiest way to get my ideas out. Granted I tend to close it and tbrow it behind me right afterwards then code. But I find Ok write less hackish code...

Its fun to scan over some of my old ones just to see how I thought about programming years past. I had a lot of bad techniques that make me cringe now a days ;)

Keeping things clean is usually a result of me going back over code and actually cleaning it up. Getting the code done and working is more important to me though.

I do the same thing with pen and paper. tongue.png

There is something different about HOLDING a set of physical objects and solving the problem with them, that seems to make problems less complex.

Makes sense considering how the brain works I guess.

This topic is closed to new replies.

Advertisement