I'm a newbie trying to build a primitive component based engine for a simple 2D game and I'm wondering if everything from a "human" to a "crate" should extend one of the two. I can see advantages to having the Update and Draw called, but it seems like a lot of extra baggage for every single entity to carry around. Any thoughts would be appreciated.
On a different note, I can also see that a manager-type class, such as a particle engine should certainly extend DrawableGameComponent.
Personally, I don't use either of the game component classes. Game engines are highly specialised and you can't possibly have a single component-based design that will work for them all.
How difficult would it be to write your own base class that has Update & Draw methods? Not very... there's really nothing magic about XNA GameComponents. If they fit your purpose, fine, if they don't, it's really not the end of the world!
Related
I am just starting to create the models for my newest game, which will be my first game in full 3D.
I have read in a couple unreliable places that it is far better to create all 3D objects as just one mesh and apply different materials to the mesh with weight painting, etc, rather than creating multiple meshes and parenting them to the same armature for animations.
According to these sources, this is because of UV mapping.
Is this true?
At the stage that I am at in creating my models, it would be far easier for me to make each individual part (arms, legs, knees) out of individual meshes and link them all to the same armature. If I do this (not merging vertices together, simply leaving each piece separate and overlapping, while linking them to the same armature), 2 questions:
Will the animations work in a Game Engine, moving all pieces and keeping them all attached where they should be?
If so, will it slow my game's performance to a significant (noticeable) degree because the characters are made of 7 or 8 separate meshes?
NOTE: I am, at this point, at least, planning on using the OpenGL game engine to run my game.
What you can do is create your models from separate meshes, UV unwrap each one, texture it, apply it's material and then when you're done and only then, merge the meshes into one and link that one mesh to your armature.
I think for blender it's Ctrl-J for joining.
I know this doesn't necessarily answer your question but it's just what I would do.
What you should do depends on what you need your model to do with the model. If your character needs to have its equipment or appearance be customizable in some way, or if you need to do special effects involving the change in appearance of the in-game character, keeping the body parts separate may be the more ideal solution. Team Fortress 2 does this with its customization system (or else you wouldn't be able to do things such as replacing the Heavy's gloves with something else or give them bird heads), while the Super Smash Brothers games takes heavy advantage of them in order to make the in-character animations seem good.
And then there are cases where a character's inherent design would've made animations regarding them be very difficult to manage should their joints be all within a single model. Examples of such characters include the Goombas from Mario and Kirby from the Kirby games.
And then there's the artistic choice of it. For example, there's no reason to make Pikachu's ears separate from the rest of the model, but the one used within Pokémon Gen VI onwards (including Pokémon Go) does so anyway. If you look real closely, you can even see the seam where the ear model ends and the head part begins.
To answer your questions as to the technical details, note that as you're using a low level API such as OpenGL for your game, whether or not you'd be able to do this depends entirely on your skills. As to the performance costs, it only depends on the amount of vertices that you have as well as the way you do the shaders. All that OpenGL ever sees is a group of vertices which has "faces" defined to them. It passes said information to the whatever shaders you had coded (to which it then processes the data you sent to it).
Granted, I'm no expert on the API, but even those starting out knowing the modern forms of OpenGL know this.
I'm still new to OOP, and the way I initially perceived it was to throw alot of procedural looking code inside of objects, and think I'd done my job. But as I've spent the last few weeks doing alot of thinking, reading, and coding (and looking at good code, which is a hugely under-rated resource), I believe I'm starting to grasp the different outlook. It's really just a matter of clarity, simplicity, and organization once you get down to it.
But now I'm starting to look at things as objects that are not as black and white a slamdunk case for being an object. For example, I have a parser, and usually the parser returns some strings that I have to deal with. But it has one specialized case where it has to return an array, and what goes in that array and how it's formatted has specialized rules. This only amounts to two lines plus one method of code, but this code sticks out to me as not being cleanly fitting in the Parser class, and I want to turn it into its own "ActionArray" object.
But is it going to far? Has OOP become a hammer that is making me look at everything like a nail? Is it possible to go too far with turning things into objects?
It's your call, but you should think of objects as real life objects.
Take for example a car. You could describe a car with different objects:
Engine
Wheels
Chassis
Or you could describe a car with just one object:
Engine
You can keep it simple and stupid or you can spread the dependency to different objects.
As a general guideline, I think Sesame Street says it best: you need an new object when "one of these things is not like the others".
Listen to your code. If it is telling you that your objects are becoming polluted with non-essential state and behavior (and thus violating the "Single Responsibility Principle"), or that one part of your object has a rate of change that is different from the rest, and so on, it is telling you that you are missing an object.
Do the simplest thing that could possibly work. When that no longer works, do the next simplest thing. And so on. In general, this means that a system tends to move from fewer, larger objects to more, smaller objects; but not always.
There are a number of great resources for OO design. In addition to the ones already mentioned, I highly recommend Smalltalk Best Practice Patterns and Implementation Patterns by Kent Beck. They use Smalltalk and Java examples, respectively, but I find the principles translate quite well to other OO languages.
Design patterns are your friend. A class rarely exists in a vacuum. It interacts with other classes, and the mechanisms by which your classes are coupled together is going to directly affect your ability to modify your code in the future. With poor class design, a change that you make in one class may ripple down and force changes in other classes, which cause you to have to change other classes, etc.
Design patterns force you to think about how classes relate to each other. For example, your Parser class might choose to implement the Strategy design pattern to abstract out the mechanism for parsing. You might decide to create your Parser as a Template design pattern, and then have each actual instance of the Parser complete the template.
The original book on Design Patters (Design Patterns: Elements of Reusable Object-Oriented Software is excellent, but can be dense and intimidating reading if you are new to OOP. A more accessible book (and specific to Ruby) might be Design Patterns in Ruby, which has a nice introduction to design patterns, and talks about the Ruby way of implementing those patterns.
Object oriented programming is a pretty tricky tool. Many people today are getting into the same conflict, by forgetting the fundamental OOP purpose, which is improving code maintainability.
You can always brainstorm about your future OO code reusability and maintainability, and decide yourself if it's the best way to go. Take look at this interesting study:
Potok, Thomas; Mladen Vouk, Andy Rindos (1999). "Productivity Analysis of Object-Oriented Software Developed in a Commercial Environment"
I'm programming a web based game and as you can imagine, it is pretty vital that i write some good code in order to be able to preserve the game and not have too many problems in the future.
As you can guess, a game has some values and functions that do not really fit to a model (or at least fitting it would make the model too fat i think). For instance, think of a case where you want to calculate the experience formula for a given monster or when you want to calculate the health of a monster, user etc based on their hp points.
I guess that all these could still fit to a user or monster model, but in my opinion, it would make it a hell to make changes in all shorts of places around different models. Moreover, a monster has hp, a user has hp, it would be weird to have the same functions in 2 or more different models (thus, not DRY).
So, i'm thinking of using a module like "game_engine/formulas" and extend it with more things if something comes up.
But i wanted some expert opinion about it. Would you do it this way ? Or is there something better ?
If you're going to share code that applies to multiple models, a module is a good way to do so. Then you can simply put Include MyModule in each model. But a module should be somewhat abstract, and shouldn't have any knowledge of which model its code is currently operating in, i.e. it shouldn't try and "detect" what model its in, but it is perfectly okay for it to get information from the containing model by calling the model's methods or accessing its attributes.
I am used to XNA like structures:
LoadContent
Update
Draw
Now based on these 3 main methods my game objects inherit from them.
In XNA I as non-professional and non-game-programmer was badly forced to think in these blocks.
Well add a Sprite baseClass, inherit from DrawableGameComponent.
Now create the WarriorSprite class for the gameobject Warrior.
etc. etc.
I totally get lost in this structures, but I simply want to program a game, not an engine.
I do not want to deal with extra ordinary rendering code each time I want to add an entity to my game.
In addition to that, I never got used to in any tutorial how to let 2 objects to interact....
My focus should lie on porting the game mechanism from game design to code.
Now I found Ogre(3D), and I hoped this engine offers this functionality.
The wiki could not answer my question really.
regards,
If you're getting lost with game-object classes based off other classes, I wouldn't recommend moving to OGRE. It's very object-oriented, by design. It does simplify some of the low level stuff, like working with DirectX or OpenGL, but you still have a lot of classes that inherit from quite a few others and have plenty of methods. If you're confused with that, you may want to read up on classes and see if you can get the hang of those.
If you're looking for an engine that you can just make a game in, though, there are a few different ways you can go.
One of the more common starting placing is modifying existing games, especially creating "total conversions." You have a complete engine, which you already have a copy of, and plenty of game resources, so that can be a good place to start.
You can go looking for an engine, but finding an easy-to-use, cheap and powerful engine isn't terribly easy. There are a lot of varied engines, all at different levels of completeness and different prices. Some that come to mind are Irrlicht, CrystalSpace, Quest3D, DarkBASIC (if you want to sacrifice your sanity and use BASIC ;)). There are plenty of others, though.
You may try checking out these for engines (I know DevMaster has a nice, detailed search):
http://www.devmaster.net/engines/
http://en.wikipedia.org/wiki/List_of_game_engines
And here for general tips:
http://www.gamedev.net/
Yes, Ogre is a complete game engine that will let you build a game, while with XNA you need to create much of the engine-type functionality.
Ogre supports both OpenGL and Direct3D, and is coded in C++. If you want to code in C# you'll need to look elsewhere (or see if anyone has done some sort of C# wrapper for Ogre). If you want to deploy to the Xbox you'll have to use XNA unless you have a contract with Microsoft.
After years of coding Delphi programs as untestable code in forms and datamodules, including global variables, and the only classes are the forms themselves, containing all the code I need for the form UI itself.
How would I convert the code to a set of classes that do the actual work? would I need to stop using the datasources/datasets and do everything in classes? do I need an ORM?
There's usually zero need for reuse of the code in the forms, so does it make sense to convert the logic to classes?
If I encounter a form (or other class) with too much responsibility, I usualy follow the pattern below:
Define a new class for the logic.
Create a member variable of the new class in the form.
Create the class in the onCreate and free it in the onDestroy of the form.
Move a single piece of logic (for example a variable) to the new class.
Move or create all methods to the new class.
Compile and test.
Continue until all logic is put in the new class.
Try to decouple the logic class from the form class. (You can even work with interfaces if you like).
There are situations where a single class is not enough, so it is no problem to create more classes. And these classes can have other classes to.
With these steps, you can tackle most of these problems.
To start with I can highly recommend reading the book Refactoring by Martin Fowler.
This will give you a real understanding about how best to sensibly approach introducing changes to the existing (non OO) code to improve maintainability.
I would not look at an ORM until you have a clear understanding about what benefits (if any) one would bring to your application.
I have encoured problem like this with one application, I start doing the following:
Define main classes for most general logic in the code.
In each form, move the code that process the business logic inside the events as function / procedures in that form.
Then Move these functions/procedures to those classes as static methods.
Finally make only the needed code inside forms like validation UI, and calls to the classes.
For the global variables try to omit as much as you can, and just pass the values as parameters to the methods.
I used static methods, because it's easier for you to remove the code from events and just call them without requiring to Create/Free object for each operation. The original design was not designed to separate the forms from business logic code.
The final application was not full OO, but it least it was easier to test the methods without requiring interacting with the forms and events like before.
Sometimes you feel if you redesign the application from scratch it will be easier than to made changes to make it real OO design.
Another book I can highly, highly recommend - in my personal opinion even better suited than the "generic" refactoring book by Fowler - is "Working Effectively with Legacy Code" by Michael Feathers. It truly showcases the major bumps you will hit while doing that kind of work. Oh, and: Refactoring legacy code can be quite hard on your psyche. I hope you can handle frustration... I like this quote (don't remember where I got it from): "God was able to create the world in 6 days, just because there wasn't any legacy code". Good luck. ;)
Importing into Modelmaker is my first action when confronted with an existing Delphi project. Modelmaker will assist you in refactoring your code because:
It graphically represents all the classes, methods, variables, etc.
It is very tightly integrated in the Delphi IDE (main menu, popup menu,
separate Modelmaker explorer,
toolbar, keyboard shortcuts). This
integration allows you to quickly
perform the necessary actions without
leaving the IDE
It has a dedicated "refactoring" module allowing you to quickly create, move
and rename classes and variables without
having to worry about changing the
underlying code. Modelmaker will
automagically change names and
references in all units.
The basic functionality of Modelmaker is easy to learn. Modelmaker is like any other good productivity tool - The more you put into it, the more you get out of it.
Modelmaker is not free but easily pays for itself in increased productivity.
I have not found a better tool for refactoring legacy Delphi code. They offer a free trial and some decent tutorial movies.
Give Modelmaker a try and good luck...
After understand what you need to refactory your code, and if you want an OPF/ORM, I suggest Jazz SDK