Unity How do you organize your classes and their respective implementation files?

This topic is 2345 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey Everyone,

Anyways, I am constantly debating with myself about how I should organize and write my code (e.g. where I should put whitespaces, etc.), and once again I am at another dilemma. I want to make my classes look more professionally organized, but because I am mostly a self-taught programmer, I don't get a lot of insight on good ways of organizing my code. So I am asking the GameDev community: How do you guys organize your classes and class implementations? For example, do you structure members first by private and public members, or by declarations and functions? Do you organize your source files in order of where members appear in the header, or by some other method (i.e. alphabetical order)? I was originally going to make this into a poll, but I found my choices were getting way too complicated.

Share on other sites
I feel your pain. Suggest finding a style that you like and then stick to it. Some style guides (I use bitsquid in my personal projects and a style that shall remain unmentioned - for your own safety - at work):
http://bitsquid.blog...ding-style.html

And some awesome tools that you can set up to automatically reformat your code:
http://uncrustify.sourceforge.net/
http://universalinde...ourceforge.net/

Writing with a consistent style is great and IMO helps readability. So make sure that when you are editing other peoples' code that you use their conventions!

Share on other sites
"Suggest finding a style that you like and then stick to it."

I would argue against the 'stick to it' bit, obviously it is wise to use a consistant style throughout any project but you should allways be aware that their probably a better style out there.

Share on other sites

I feel your pain. Suggest finding a style that you like and then stick to it. Some style guides (I use bitsquid in my personal projects and a style that shall remain unmentioned - for your own safety - at work):
http://bitsquid.blog...ding-style.html

And some awesome tools that you can set up to automatically reformat your code:
http://uncrustify.sourceforge.net/
http://universalinde...ourceforge.net/

Writing with a consistent style is great and IMO helps readability. So make sure that when you are editing other peoples' code that you use their conventions!

Thanks for the links! I understand that it is good to keep a consistent style at least across a project, but related to what Jim1664 said, I created this topic post in order to gather different styles from the GameDev community and pick elements from each that I like. So to add onto what I originally posted:

Share a particular styling rule that you think is great in making code better organized and/or easier to read. This can include many things such as white spaces, number of spaces before starting a new line, etc. If you can, provide reason(s) as to why you think it works well. Let's get a discussion happening!

It is difficult to say what is the best style, but I would love to hear things that coders have discovered really work for them.

Share on other sites
This is always going to be a subjective conversation. There is no objectivity to be had.

Some people like spaces. I like tabs. Some people like K&R bracing. I like the bracing style that isn't fuck-ugly.

We can argue back and forth about this all month, and neither side will ever really be convinced.

Pick something that looks nice to you, and stick with it within a given codebase. If you decide later that something else looks nicer, feel free to switch. It's 2011, we should all be using pretty-printers on our code anyways, so it should never actually matter.

Share on other sites

This is always going to be a subjective conversation. There is no objectivity to be had.

Some people like spaces. I like tabs. Some people like K&R bracing. I like the bracing style that isn't fuck-ugly.

We can argue back and forth about this all month, and neither side will ever really be convinced.

Pick something that looks nice to you, and stick with it within a given codebase. If you decide later that something else looks nicer, feel free to switch. It's 2011, we should all be using pretty-printers on our code anyways, so it should never actually matter.

Well I know this kind of stuff is mostly subjective, I just wanted to see what styles other programmers use in case I never thought of a good method or a good reason for that method. There would be no need for people to point fingers and tell the other person that their style seems absurd. That's not what I want at all, so I guess it was wrong of me to try to promote a "discussion". Rather, this is more of "show-and-tell".

By the way, what do you mean by K&R bracing?

Share on other sites
[font="Consolas"]"By the way, what do you mean by K&R bracing?[/font]"

Its the style used in the 'C Programming Language', by Kernighan and (someone else whos name begins with an R)

Which is;

 void function( ... ) { doSumfink(); for( int a = 10; --a; ) { doSummitElse(); } } 

By comparison, I use.

 void Function( ... ) { DoSumfink(); for( int a = 10; --a; ) { DoSummitElse(); } } 

Edit: That guys name is Ritchie, not knowing annoyed me so I had to google it, or bing actualy but 'bing it' isn't quite the same.

Share on other sites

Well I know this kind of stuff is mostly subjective, I just wanted to see what styles other programmers use in case I never thought of a good method or a good reason for that method. There would be no need for people to point fingers and tell the other person that their style seems absurd. That's not want I want at all, so I guess it was wrong of me to try to promote a "discussion". Rather, this is more of "show-and-tell".

Fair enough

You can see examples of my formatting style at http://epoch-language.googlecode.com/ and http://scribblings-by-apoch.googlecode.com/

Share on other sites

[font="Consolas"]"By the way, what do you mean by K&R bracing?[/font]"

Its the style used in the 'C Programming Language', by Kernighan and (someone else whos name begins with an R)

Which is;

 void function( ... ) { doSumfink(); for( int a = 10; --a; ) { doSummitElse(); } } 

By comparison, I use.

 void Function( ... ) { DoSumfink(); for( int a = 10; --a; ) { DoSummitElse(); } } 

Edit: That guys name is Ritchie, not knowing annoyed me so I had to google it, or bing actualy but 'bing it' isn't quite the same.

I see. So from what I can tell, K&R bracing is where they leave the first brace on the same line as the function call? I much rather have my braces have their own line. I feel like I can lose track of where I am enclosing the scope of a particular chunk of code. I too like capitalizing functions names so that I can tell them apart from my variables - it's just something I found appealing when I was learning C#.

[quote name='Robot Ninja' timestamp='1316341910' post='4863002']
Well I know this kind of stuff is mostly subjective, I just wanted to see what styles other programmers use in case I never thought of a good method or a good reason for that method. There would be no need for people to point fingers and tell the other person that their style seems absurd. That's not want I want at all, so I guess it was wrong of me to try to promote a "discussion". Rather, this is more of "show-and-tell".

Fair enough

You can see examples of my formatting style at http://epoch-language.googlecode.com/ and http://scribblings-b...googlecode.com/
[/quote]

Very cool. I clearly have a lot to learn in C++, because a lot of the source code is still out of the scope of my understanding.

Share on other sites
Here are a few ideas to consider.

L. Spiro

Share on other sites

Here are a few ideas to consider.

L. Spiro

Wow, a very comprehensive guide targeted directly towards people like me. Awesome. I've been contemplating using Hungarian notation, but I never really decided on a priority tree for writing the letters. Your guide certainly gave me a better idea, thanks. It also seems to make sense that public class members may be better displayed at the top of the class for accessibility reasons. I was never sure what other people did for that, especially when class members are private by default. My primary reason for making this post was to find out how other people organized their classes, which you also answered in your guide - thanks again!

I didn't anything mentioning how you organize your source file. Do you just order the code by how they appear in your header?

Share on other sites
Indeed I forget to mention that, so I will update my article.
Functions in the .CPP files appear in exactly the same order as those in the .H files. Same with inlined functions at the bottom of .H files.

L. Spiro

• 9
• 10
• 12
• 10
• 10
• Similar Content

• By khawk
Watch the latest from Unity.

• By GytisDev
Hello,
without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
• By Ovicior
Hey,
So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).

Here's a couple unique weapon ideas I have:
Explosion sword: Create explosion in attack direction.
Cold sword: Chance to freeze enemies when they are hit.
Electric sword: On attack, electricity chains damage to nearby enemies.

I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?

I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.

• Hi fellow game devs,
First, I would like to apologize for the wall of text.
As you may notice I have been digging in vehicle simulation for some times now through my clutch question posts. And thanks to the generous help of you guys, especially @CombatWombat I have finished my clutch model (Really CombatWombat you deserve much more than a post upvote, I would buy you a drink if I could ha ha).
Now the final piece in my vehicle physic model is the differential. For now I have an open-differential model working quite well by just outputting torque 50-50 to left and right wheel. Now I would like to implement a Limited Slip Differential. I have very limited knowledge about LSD, and what I know about LSD is through readings on racer.nl documentation, watching Youtube videos, and playing around with games like Assetto Corsa and Project Cars. So this is what I understand so far:
- The LSD acts like an open-diff when there is no torque from engine applied to the input shaft of the diff. However, in clutch-type LSD there is still an amount of binding between the left and right wheel due to preload spring.
- When there is torque to the input shaft (on power and off power in 2 ways LSD), in ramp LSD, the ramp will push the clutch patch together, creating binding force. The amount of binding force depends on the amount of clutch patch and ramp angle, so the diff will not completely locked up and there is still difference in wheel speed between left and right wheel, but when the locking force is enough the diff will lock.
- There also something I'm not sure is the amount of torque ratio based on road resistance torque (rolling resistance I guess)., but since I cannot extract rolling resistance from the tire model I'm using (Unity wheelCollider), I think I would not use this approach. Instead I'm going to use the speed difference in left and right wheel, similar to torsen diff. Below is my rough model with the clutch type LSD:
speedDiff = leftWheelSpeed - rightWheelSpeed; //torque to differential input shaft. //first treat the diff as an open diff with equal torque to both wheels inputTorque = gearBoxTorque * 0.5f; //then modify torque to each wheel based on wheel speed difference //the difference in torque depends on speed difference, throttleInput (on/off power) //amount of locking force wanted at different amount of speed difference, //and preload force //torque to left wheel leftWheelTorque = inputTorque - (speedDiff * preLoadForce + lockingForce * throttleInput); //torque to right wheel rightWheelTorque = inputTorque + (speedDiff * preLoadForce + lockingForce * throttleInput); I'm putting throttle input in because from what I've read the amount of locking also depends on the amount of throttle input (harder throttle -> higher  torque input -> stronger locking). The model is nowhere near good, so please jump in and correct me.
Also I have a few questions:
- In torsen/geared LSD, is it correct that the diff actually never lock but only split torque based on bias ratio, which also based on speed difference between wheels? And does the bias only happen when the speed difference reaches the ratio (say 2:1 or 3:1) and below that it will act like an open diff, which basically like an open diff with an if statement to switch state?
- Is it correct that the amount of locking force in clutch LSD depends on amount of input torque? If so, what is the threshold of the input torque to "activate" the diff (start splitting torque)? How can I get the amount of torque bias ratio (in wheelTorque = inputTorque * biasRatio) based on the speed difference or rolling resistance at wheel?
- Is the speed at the input shaft of the diff always equals to the average speed of 2 wheels ie (left + right) / 2?