programming

Overriding and methods

Today I had a little challenge. I had a Class, I call it Class A. It has a method, i call it “doSomething”. Class B extends Class A, and overrides this method with a totally new behavior. Class C who extends from Class B wants to have the original behavior from Class A.

Here is a picture:

The Problem

Now, the simplest but most stupid way to solve this is using copy / paste. Yes, you simply copy the contents of the method of Class A and paste it into the method of Class C and you’re all set right?

Wrong!

It might work for a while, but one of the first things you *should* be feel itchy about, is code duplication! (along with its problems you get when you want to maintain your code).

So what now?

Solution : Use method for shared behavior

Solution : Use method for shared behavior

Well, when you took the easy road and duplicated your code, you probably wanted to get rid of that duplicate code immidiatly… And how do you do that? .. Yes,  you create a new method which is put in Class A , and accessible from Class C and Bam! Code Duplication gone, and you got what you want…

Another way is to re-think your class hierarchy. You might want to consider to do this:

 

Solution : Change in hierarchy, extend C from A, B from C

Solution : Change in hierarchy, extend C from A, B from C

 

 

Yes, you’ve seen it right. Perhaps you can swap Class B and C.  So C extends now from A, and B from C.

But, if you do that, be careful. You need to know exactly what kind of behavior you wanted in Class B. Most likely you have changed that now by extending from C. Take a good look at what methods B was originally calling from Class A, and if it now calls an overridden method by Class C.

These 2 solutions came up today. For my particular problem I’ve used the first. (no, not copy paste smart ass)

I think the first solution is the easiest, I would not recommend anyone to do the second solution unless you really know what you are doing. If you encounter more of these problems like above, swapping might be better for you.  

Do you got another (better?) solution? Let me know!

Standard
Game Development, Games

Game Programming (Experience)

Recently I saw at Youtube someone telling about his view on game programming. His targetted audience was for those who want to create games, but have no clue how to start. He began with something along the line of “I’ve been programming games for 5 years, so I guess I could say I’m experienced“.

That got me thinking. How long have I been programming (for) games?

I’ve been working on Arrakis since I was around 14/15 years old. I’ve finished it when I was 18. Arrakis is written in Basic (using Quick Basic 4.5). I realized in order to get further I had to learn a new programming language. One that did not had the limitations as Basic. Thats when I had decided to learn C. My first attempt was writing a new Dune II clone, this time really using the Dune II graphics.

After a short period of learning C I began playing around with the Half-Life SDK; I’ve been toying around with a bot framework (Botman’s bot) and before I knew it, RealBot was born. RealBot has been developed for around 4 years.

And, to complete the circle; while RealBot was fading away I picked up again my Dune mania and recreated Dune II – The Maker from scratch. 

Today, I am 26 years old. I haven’t worked on Dune II – The Maker seriously since 2006. This would mean I would have rougly around 7 to 8 years “experience”.  So considering that, I might also say that I have experience in game programming.

When you look at my LinkedIn, you see none of those projects counted as experience. Sure, they are mentioned
under “websites”. But I would prefer to put them in a more suited spot. Perhaps somebody has a suggestion for that.

Although ~ 8 years might sound like a lot. I do think it has to taken with a grain of salt.

First, I did not work 8 hours a day, 5 days a week.
Secondly, I did not have any sparring partners,  so quality wise I did not write superb code.

However, there are concepts that I’ve learned; and you only learn those when you write games. Concepts like:

  • sprite/bitmap management
  • drawing maps / tile engine
  • A*
  • parsing files (from original game)
  • little AI, 
  • state machines, 
  • double buffering
  • blending techniques
  • team colouring (in RTS)
  • using colorkey’s
  • transparancy
  • palette manipulation

And probably a few other things I can’t recall at this moment.

So yeah, perhaps I could say I do have experience in Game programming…

Standard