   C++ Classes and Structures and "fast" code
  Hi all,I'm going to learn C++ and what better way of learning than making a game :)I'n no totally inexperienced since I made some small textbased games in Ada and visual basic. However since I'm still pretty new to object orientation I would like to learn it good from the start and with good I mean fast code that is accepted in the game industry (ok Im not talking assembler fast but atleast somewhat optimized).I heard that for example classes shouldn't take to many parameters and I'm wondering what the "limit" is and what about structures, are there any limits to those aswell? What about nested structures, are those slower or faster than ordinary structures?For example if I want to make a RPG and want to spawn enemies easy should i make a class like thisclass enemy (int strength, int physics, int intelligence, int endurance, int hitpoints, int ...., .... ,....)and then to spawn it I type likespawn(x_coord, y_cord) enemy(100, 20, 99, 45, 34, 543, 3,4,...,...,...,...)or is it better to use alot of smaller classes that only take a few parameters as input?Yes I know the code ^^ wont work ;) Also if I want to make like a strategy game where you run a small country and fight other countries, would it be a good way to make country a data structure something likestruct country {float money;int attackpoints;int researchpoints;int smallbuildingA;int mediumbuildingA;int largebuildingA;int smallbuildingB;int medbuildingB;int largebuildingB;int smallbuildingC;......};or would it be better to make several structures likestruct building {int small buildingA....int largebuildingC};and then use the structure building inside the structure country....?Could someone (if you are still awake) give me some pointers or general guidelines regarding classes and structures, small and specialised vs big and generialised?Also I guess theres things like lean production in the game industry an perhaps its better to write slower code thats easy to reuse than fast code thats more specific? But Im still somewhat in the amiga500 scene mindset where fast code >>>>> anything else :O   
struct foo {  int a;  double b;  char c;};struct bar {  int a;  double b;};struct baz {  bar x;  char c;};foo and baz are identical in binary layout.  The names of the members are different."unfolding" a struct into a parent struct has no impact on the binary layout of the parent struct.By breaking things up into smaller chunks, however, you can write functions that take only part of the parent structure and do things with it.  This can be highly useful.Big structures are hard to understand.  In a successful program, you will be spending more time debugging and maintaining it than you do writing it.  So write your code to be understood.The same holds for ridiculous lists of arguments.  They are hard to understand.  You are probably better off with a file describing the data.  If you must make it static data, you can write a program that takes the description file and outputs a .cpp file with the creation code.;  44 views 0 replies O.oAny input would be greatly appriciated.Edit: Oh 1 reply while writing this post :) Thank You ^Edit 2: About the foo and baz I got it thanks, about the part about classes with tons of parameters I see the problem when using the class but im very skilled with // usage :O. I tend to write more explanations than code heh. But I get it and usually make functions with few intuitive parameters... however will the speed of the program be slower if i use alot of parameters as input for the classes?   ;Quote:Original post by HybrisI'n no totally inexperienced since I made some small textbased games in Ada and visual basic. However since I'm still pretty new to object orientation I would like to learn it good from the start and with good I mean fast code that is accepted in the game industry (ok Im not talking assembler fast but atleast somewhat optimized).Optimization is best done after profiling - that is, you write code (of course, you should think ahead, but you shouldn't get all too obsessed over it) and then you check if it's fast enough. On modern-day systems, and if you're not writing too complicated things (which, as a beginner, you shouldn't), you'll hardly ever run into trouble (or you're doing something in an obviously inefficient manner). Either way, if performance is a problem, you should determine what causes it, and fix those parts first.Also, different platforms require different mindsets. On a PC, I'd prefer a high-level scripting language over a low-level one for many things. It's trading some performance for development speed and flexibility - a trade-off that is much more easily made now than it was years ago.Quote:I heard that for example classes shouldn't take to many parameters and I'm wondering what the "limit" is and what about structures, are there any limits to those aswell? What about nested structures, are those slower or faster than ordinary structures?I wouldn't worry about speed here, but about clarity, manageability, aspects like that. Object-orientated development wraps things up into small, manageable chunks, and allows you to work on higher levels - instead of working with ints, bytes and the like, you start working with the objects you designed - Player, Building, Country, and so on.Quote:For example if I want to make a RPG and want to spawn enemies easy should i make a class like thisclass enemy (int strength, int physics, int intelligence, int endurance, int hitpoints, int ...., .... ,....)and then to spawn it I type likespawn(x_coord, y_cord) enemy(100, 20, 99, 45, 34, 543, 3,4,...,...,...,...)or is it better to use alot of smaller classes that only take a few parameters as input?Depends on the situation, but yeah, that looks pretty ugly. You'll probably want to store those values in an external file anyway, so it's easier to tweak the gameplay. Waiting for a recompile every time gets boring quick.Quote:Also if I want to make like a strategy game where you run a small country and fight other countries, would it be a good way to make country a data structure something likestruct country {float money;int attackpoints;int researchpoints;int smallbuildingA;int mediumbuildingA;int largebuildingA;int smallbuildingB;int medbuildingB;int largebuildingB;int smallbuildingC;......};Uhm, no. Buildings and things like that are typically not stored as integers. They probably - depending on your game's design - have properties of their own. How about a Building class that takes care of that? That, and small, medium and large buildings probably only differ by size, so you can make it a property of the Building class.Quote:Could someone (if you are still awake) give me some pointers or general guidelines regarding classes and structures, small and specialised vs big and generialised?It's something that comes with experience, but I think you tend to think too low-level.Quote:Also I guess theres things like lean production in the game industry an perhaps its better to write slower code thats easy to reuse than fast code thats more specific? But Im still somewhat in the amiga500 scene mindset where fast code >>>>> anything else :OFast code isn't necessarily cryptic and 'single-structed' - but really, it's not that big of an issue anymore these days. Most optimizations are done at higher levels, by choosing the right algorithms and data structures. Modern compilers have become pretty good at taking care of the lowest levels, too, so you'd have to be pretty good at assembly to beat them.   ;  Edit: Someone linked this somewhere else in Game Programming recently. It's a good read, and relevant here.Quote:Original post by HybrisI'm going to learn C++The power of the dark side compels you. ;)Quote:However since I'm still pretty new to object orientationC++ is not "an OO language". It is not even primarily about OO. It simply offers reasonably good support for OO (much better than C, anyway, where you have to implement polymorphic dispatch yourself). Further, a quick check with Google indicates that OOP is commonly done in both languages you already know. So you are learning two completely separate things here: a new language, and a new design paradigm.Quote:I would like to learn it good from the start and with good I mean fast codeThat's strange, because "fast code" is near the bottom of the list of things that experienced developers generally mean by "learning a language well".Quote:that is accepted in the game industryWhy do you care what is "accepted in the game industry"?Quote:I heard that for example classes shouldn't take to many parametersThat has nothing to do with performance; it has to do with organization.Quote:and I'm wondering what the "limit" is and what about structures, are there any limits to those aswell?You shouldn't let your room get too messy. Is there a "limit" on how many things you can leave lying around?Quote:What about nested structures, are those slower or faster than ordinary structures?Accessing elements of a structure involves adding a known offset to the address of the structure. Nested structures don't cost anything if they're directly embedded, because then you just add another known offset, and the two known offsets get summed ahead of time by the compiler. If you have a pointer to another structure, though, then that pointer will have to be dereferenced (and an offset added again, presumably). One operation is slower than no operations. Similarly, if you stick your hand out the car window, and don't increase the gas supply, the car will slow down a tiny bit.Quote:For example if I want to make a RPG and want to spawn enemies easy should i make a class like thisDo make a class to represent the enemy, yes. How to organize the data contents depends on what the enemy has to do, and is not something that can simply be explained - it must be taught, and refined over years of experience. But fortunately, you already have some experience. You have used some kinds of data structures in your VB and Ada programming, yes? Especially in Ada, it seems impossible to avoid.Quote:Also if I want to make like a strategy game where you run a small country and fight other countries, would it be a good way to make country a data structure something like...or would it be better to make several structures like...and then use the structure building inside the structure country....?1) You don't want to make things like buildingA, buildingB, etc. You have at least heard of arrays, right? Or more generally, of containers (such as a linked list, an associative array, a vector...)?2) Structures represent data types. If you make a Building structure, it does not exist for the purpose of containing buildings. It exists for the purpose of describing what a building is. (an int does not describe a building.) It might include some data, for example, that indicates the size of the building.Then, you make a container of Buildings, and have that container as a data member of the Country.Quote:Could someone (if you are still awake)1) Gamedev.net members live all around the world.2) This is a forum; it is not for real-time communication.Quote:But Im still somewhat in the amiga500 scene mindset where fast code >>>>> anything else :OWait, they use Ada and VB in the "scene"? O_O[Edited by - Zahlman on May 11, 2009 3:14:04 PM];Original post by Captain POriginal post by HybrisQuote:Optimization is best done after profiling - that is, you write code (of course, you should think ahead, but you shouldn't get all too obsessed over it) and then you check if it's fast enough. On modern-day systems, and if you're not writing too complicated things (which, as a beginner, you shouldn't), you'll hardly ever run into trouble (or you're doing something in an obviously inefficient manner). Either way, if performance is a problem, you should determine what causes it, and fix those parts first.Yeah I know I will probably have to go back and fix some parts of the code if nessecary and use better algorithms.. I was more looking for general things to think of when programming not to get caught in bad habits (like i did in basic where i often used sloppier code because it was faster but i knew it would slow down the speed of the program (altough i didn't do anything too complicated that needed top speed back then, but now when im gonna make cryengine 2...) nah its just for sports and to have it with me incase i need to use faster code in the future. since im learning c++ I might aswell learn how to use it efficently from the start since it takes little extra efforet but if i get use to do it in a "slow way" it will take alot of effort to relearn (atleast thats how i work).Quote:Also, different platforms require different mindsets. On a PC, I'd prefer a high-level scripting language over a low-level one for many things. It's trading some performance for development speed and flexibility - a trade-off that is much more easily made now than it was years ago.Yeah, but for me its not about making a game. I want to understand the language, what affect the speed of the code execution and stuff like that. In the future I think i will make smaller fast running applications and perhaps a mix of c++ and assembly. The game part is just a fun way to learn the basics of the language. (((Like so many other ppl my dream is to construct a mmorpg ala wow but I know that wont happen so I will pursue my other dream instead, to make a small robot which eventually will involve programming of motors, sensors and AI. (I made some very simple lowlvl (read 010101) programming for machines a couple of years ago).)))Quote:I wouldn't worry about speed here, but about clarity, manageability, aspects like that. Object-orientated development wraps things up into small, manageable chunks, and allows you to work on higher levels - instead of working with ints, bytes and the like, you start working with the objects you designed - Player, Building, Country, and so on.Aye i get the idea, but first I want to learn how to construct those chunks in a good (and fast running code) way.  Quote:Depends on the situation, but yeah, that looks pretty ugly. You'll probably want to store those values in an external file anyway, so it's easier to tweak the gameplay. Waiting for a recompile every time gets boring quick.Yeah but those values need to go into the class or function or whatever at some point or another (either if i call the functioo/class with them direcly in the main program like Uglyenemy1(10,10,23,111,323.....) or if i hve them in an external file right? With the file i can compile my program and then just notepad the file with the numbers to change the property of the enemy but in the end somehow all those numbers will be put into the class right? I was wondeing if having that much parameters will make the class execution go slower than if i somehow use alot of different classes and split it up for example have a classhead(int facing, int hitpoints){check does he see you;if hitpoints<0 return you/enemy...whatever called this is dead;};^^ this brings up another question.. is there any difference in speed when using different checks like is:if i=5 as fast as writing i<6 ??Quote:;Uhm, no. Buildings and things like that are typically not stored as integers. They probably - depending on your game's design - have properties of their own. How about a Building class that takes care of that? That, and small, medium and large buildings probably only differ by size, so you can make it a property of the Building class.Well it was just an example, i was thinking of numbers of different buildings in the game like:Country sweden have 1 small powerplant, 3 nuclear powerplant, 4 small barracs, 3 large barracks.... Quote:It's something that comes with experience, but I think you tend to think too low-level.yes I do, but I need to understand the low level before I go to a higher level. I understand its not the optimal way for working with huge games or if you are a part of a large team... but its mostly for myself and for sports... when I understand the low level I can make nice fast algoritms wrapped up in easily managebly chunks... Quote:Fast code isn't necessarily cryptic and 'single-structed' - but really, it's not that big of an issue anymore these days. Most optimizations are done at higher levels, by choosing the right algorithms and data structures. Modern compilers have become pretty good at taking care of the lowest levels, too, so you'd have to be pretty good at assembly to beat them.Yeah the compilators are pretty good these days. Maby im worrying too much :OAnyway thank You for taking the time.;Quote:Original post by ZahlmanQuote:The power of the dark side compels you. ;)Father???? O.oQuote:C++ is not "an OO language". It is not even primarily about OO. It simply offers reasonably good support for OO (much better than C, anyway, where you have to implement polymorphic dispatch yourself). Further, a quick check with Google indicates that OOP is commonly done in both languages you already know. So you are learning two completely separate things here: a new language, and a new design paradigm.Yes and yes... im sloppy with words... sometimes you have to read what i mean not what i writeQuote:That's strange, because "fast code" is near the bottom of the list of things that experienced developers generally mean by "learning a language well".Didn't know that, I thought it was important to have fast code. I understand the value of easily manageble code and a good structure so other can follow your code... but fast code doesnt mean you cant have a good structure...Quote:why do you care what is accepted in the game industryYou never know about the future... The game industry is (or atleast was) known for having fast code.[quoteThat has nothing to do with performance; it has to do with organization.]I once heard that alot of parameters could slow down the execution but i might have misunderstood.Quote:You shouldn't let your room get too messy. Is there a "limit" on how many things you can leave lying around?Accessing elements of a structure involves adding a known offset to the address of the structure. Nested structures don't cost anything if they're directly embedded, because then you just add another known offset, and the two known offsets get summed ahead of time by the compiler. If you have a pointer to another structure, though, then that pointer will have to be dereferenced (and an offset added again, presumably). One operation is slower than no operations. Similarly, if you stick your hand out the car window, and don't increase the gas supply, the car will slow down a tiny bit.Not even the darkside would help you in my room O.oQuote:Do make a class to represent the enemy, yes. How to organize the data contents depends on what the enemy has to do, and is not something that can simply be explained - it must be taught, and refined over years of experience. But fortunately, you already have some experience. You have used some kinds of data structures in your VB and Ada programming, yes? Especially in Ada, it seems impossible to avoid.Yes i have used structures before. Quote:1) You don't want to make things like buildingA, buildingB, etc. You have at least heard of arrays, right? Or more generally, of containers (such as a linked list, an associative array, a vector...)?2) Structures represent data types. If you make a Building structure, it does not exist for the purpose of containing buildings. It exists for the purpose of describing what a building is. (an int does not describe a building.) It might include some data, for example, that indicates the size of the building.Then, you make a container of Buildings, and have that container as a data member of the Country. Yeah i replied to the previous post.. I just used it as an example.. I had my mindset more on a textbased game where the structures would be predefined like small powerplants, nuclear powerplant..barracks and so on and the int structure a would just hold a number how many for example small pwerplants that country had.Quote:1) Gamedev.net members live all around the world.2) This is a forum; it is not for real-time communication.O.o I didn't know *rolls eyes*. I thoght if you fell asleep while reading my post...Quote:Wait, they use Ada and VB in the "scene"? O_ONo they used assembly.. move to blitter and stuff like that... I wasn't very into it by then but i had 3 classmates that used the words blitter and mov in atleast 92,3% of the sentences. I just remember how important it was with fast code...back then.;Quote:Original post by HybrisI just remember how important it was with fast code...back then.Yes, exactly, Back then. Now it is different. Computers can do more than 2 operations a second and aren't the size of an average house. The sooner you stop worrying about execution speed, letting the compiler handle that, and getting on with actually designing manageable code, the better.A lot of people frequently debate whether low-level C or high-level OO is better but these people seem to come from very different eras. As programs get bigger and more complicated it is absolutely necessary to think in a modular way where systems (e.g. classes) are designed at a high level and composed of smaller and smaller parts. Rather than having a huge structure for a country which contains everything, have Country class which contains Buildings which in turn contains other objects like Chairs and Tables (although you probably won't need those in your game). Listen to people like Zahlman, they know what they are talking about.   ;  If you want a member variable to represent a count of something, then you should name it in a way that indicates that. Thus, "number_of_power_plants" or something. :)   ;Quote:Original post by HybrisAye i get the idea, but first I want to learn how to construct those chunks in a good (and fast running code) way.You should not worry that your code is slow but that your code is bad in terms of design. Maybe you can get an extra free cycle by doing a lot of scary stuff, but a brute force collision detection algorithm is going to cost you much more. So you should really just concentrate on getting to know the language and OOP.Quote:Yeah but those values need to go into the class or function or whatever at some point or another (either if i call the functioo/class with them direcly in the main program like Uglyenemy1(10,10,23,111,323.....) or if i hve them in an external file right? With the file i can compile my program and then just notepad the file with the numbers to change the property of the enemy but in the end somehow all those numbers will be put into the class right? I was wondeing if having that much parameters will make the class execution go slower than if i somehow use alot of different classes and split it up for example have a class...Premature optimization is the nature of evil!Don't do that.You want to write well designed code. And by the way, your speed of execution will mainly depend on the calculations / iterations you are doing and not on the number of variables a class has.Quote:yes I do, but I need to understand the low level before I go to a higher level. I understand its not the optimal way for working with huge games or if you are a part of a large team... but its mostly for myself and for sports... when I understand the low level I can make nice fast algoritms wrapped up in easily managebly chunks... I think you'll hit a dead end soon. You can worry about low level stuff when you need to, again, read the sentence above if you don't believe me. It's just like optimizing floating point operations with SSE: If you don't do it right it ends up being slower than the easy approach, so why even bother if it's not worth it. For example: Operations with my own vector class are about 2 times slower than with the DirectX vectors, however I don't really care because a nice little octree to cut down the complexity of my collision detection is going to bring me alot more than replacing my code with SSE intrinsics.When you build a car, will you concentrate on every little aspect of the engine before it even got wheels or would you wait until you know it's the engine that needs tweaking?   