Keeping Track Of Your Data
Keeping Track Of Your Data
What's the best way to keep track of your data?
Say you have a structure or a class that holds initializations of other classes and structures along with the Irrlicht device and things of that nature.
What is the best way to keep track of this data and have the contents available to some if not all of the classes which are initialized inside of it?
I often find it easy to get backed into a corner when you have many different things passing around data so I would like to get everyone's strategies and input on this :)
thank you
Say you have a structure or a class that holds initializations of other classes and structures along with the Irrlicht device and things of that nature.
What is the best way to keep track of this data and have the contents available to some if not all of the classes which are initialized inside of it?
I often find it easy to get backed into a corner when you have many different things passing around data so I would like to get everyone's strategies and input on this :)
thank you
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: Keeping Track Of Your Data
There is no miracle structure that solves all the problems
You have to clearly identify what is the problem and then carefully create a design. Often, your problem has already been solved years ago by other developers. Design solutions to these predefined problems are called Design Patterns.
If you cannot use any of these patterns to solve your problem, you have to design your solution alone according to your problem definition. You will develop the ability to design your own solutions with a lot of practice, and by taking advice from other developers.
No one will be able to bring a "strategy" to you. Everything depends on your problem and on what it implies. There are hundreds (thousands?) of books describing how to design good architectures, and there are new books about new design processes every year.
However, if you are precise enough (which means extremely precise
) in your problem definition, you might find developers who encountered the problem before you and already have a good solution. Then, they will surely be glad to share it with you 
You have to clearly identify what is the problem and then carefully create a design. Often, your problem has already been solved years ago by other developers. Design solutions to these predefined problems are called Design Patterns.
If you cannot use any of these patterns to solve your problem, you have to design your solution alone according to your problem definition. You will develop the ability to design your own solutions with a lot of practice, and by taking advice from other developers.
No one will be able to bring a "strategy" to you. Everything depends on your problem and on what it implies. There are hundreds (thousands?) of books describing how to design good architectures, and there are new books about new design processes every year.
However, if you are precise enough (which means extremely precise
Re: Keeping Track Of Your Data
I realize my initial post was a bit broad, I'm more looking for people to post different suggestions, pros and cons of doing different things, if you could go back in time to when you first started programming and give yourself a bit of advice on this subject what would it be? :P
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: Keeping Track Of Your Data
Read other people's code. For example supertuxkart is opensource and using Irrlicht.
General hints are hard. For settings (if you mean those when you say "initializations of other classes") I have usually one or several settings classes. Irrlicht device is usually in my main-application class and then passed on to the classes which need access to it (like for example my game class).
General hints are hard. For settings (if you mean those when you say "initializations of other classes") I have usually one or several settings classes. Irrlicht device is usually in my main-application class and then passed on to the classes which need access to it (like for example my game class).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Keeping Track Of Your Data
What do you mean when you say "passed on"? Can you provide an example? :)CuteAlien wrote:Irrlicht device is usually in my main-application class and then passed on to the classes which need access to it (like for example my game class).
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: Keeping Track Of Your Data
That depends on the situation. Sometimes only one function of another class needs it, then it's a parameter to that function. Sometimes the class can't work with it - then it's passed as parameter in the constructor and saved as a membervariable. Or sometimes I might pass my app-class itself which has then a getDevice() function.
When I wrote a racer a few years ago I was even more lazy and used one global variable for my application class (called APP). So I could call something like APP.getIrrlichtDevice() from any place. Which works well in simply non-network games, but once a game gets really large I think it's better to avoid globals completely (well, I still use a global for the logging class even then).
And about settings, for example my class for the steering settings in that racer looked like this:
When I wrote a racer a few years ago I was even more lazy and used one global variable for my application class (called APP). So I could call something like APP.getIrrlichtDevice() from any place. Which works well in simply non-network games, but once a game gets really large I think it's better to avoid globals completely (well, I still use a global for the logging class even then).
And about settings, for example my class for the steering settings in that racer looked like this:
Code: Select all
struct SettingsSteering
{
SettingsSteering();
void ReadFromXml(TiXmlElement * settings_);
void WriteToXml(TiXmlElement * settings_);
float mRotationSpeed;
float mScaleMinSpeed;
float mScaleRotationBySpeed;
float mForceFactor;
float mBrakeFactor;
float mScaleRolling; // how fast does hover roll from controller
float mScaleRollingBack;
float mMaxRolling; // don't roll more than that
float mScalePitchFlying; // how fast is pitch changed when in air
float mScaleAlignment; // how fast is hover aligned to floor
int mRelocateNoColl; // ms to disable collisions after hover got teleported back to track
int mRelocateFreeze; // ms to avoid moving after hover got teleported back to track
int mResumeFreeze;
};
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Keeping Track Of Your Data
My application has many classes such as, MenuManager, WorldManager, PlayerManager, NetworkManager, ultimately what my scenario calls for is the need for some sort of global class to hold the Irrlicht device along with some other Irrlicht related parameters and the initializations of these previous classes, then the ability to have access to this global class within all the initialized classes, that last part is where my code turns into spaghetti and I start getting stuck in places with no way to get out.
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: Keeping Track Of Your Data
wow. You used the word global in your post. I look forward to reading this over the next few days 
Re: Keeping Track Of Your Data
I understand you would not want it to be a "global variable/class" I only used it as a descriptive word to convey my thoughts and ideas :P
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.
Re: Keeping Track Of Your Data
You have those options:
- Each class keeps a pointer to Irrlicht device. Which is passed either in the constructor (if the class won't work without it) or with a setDevice function (when it can change at runtime). That is mostly the best solution. Now anyone reading your code - especially yourself in 5 months - can see immediately that this class needs the Irrlicht device to work.
- Have just one class with the Irrlicht device (let's call it AppClass for now). AppClass needs a function getDevice() to return the Irrlicht device. Pass a pointer to AppClass to the classes which need the Irrlicht device. Advantage is that you don't have Irrlicht device pointers all over the place. Disadvantage is that it's harder to see that this class does need an IrrlichtDevice.
- Again use AppClass as above. Create a global object for it which can be accessed from anywhere directly. Advantage - less typing. Disadvantage, like before it's no longer easy to see who uses the device. Another disadvantage - every class can suddenly use every public function in AppClass. So every function in there is global - and every class you can access in there is also global. So basically you have given up on OO design. Last disadvantage - when starting like this it often leads to code that is not re-usable at all. Because it's so easy to just access the global class everywhere. And because global classes are project specific your code can't be used in any other project (unless it has the same application class). All that said - in small projects having to type less can still be worth it and even lead to more readable code - but generally you will regret it in the long run. So don't use this.
The best code is to find the minimum number of other classes which a class absolutely needs. And only pass pointers (or even better references when possible) to those. If the class needs a pointer constantly then keep that pointer as member. If only 1-2 functions in your class need a pointer then consider as well passing it as function parameter. That is even nicer (but can be slower or leading to too many function parameters).
About your case: Do really all your classes need the IrrlichtDevice? Or maybe some only need the IVideoDriver and others maybe only the ISceneManager? Or some could work with a second Irrlicht device which is using the NULL driver (if you only need file functions).
- Each class keeps a pointer to Irrlicht device. Which is passed either in the constructor (if the class won't work without it) or with a setDevice function (when it can change at runtime). That is mostly the best solution. Now anyone reading your code - especially yourself in 5 months - can see immediately that this class needs the Irrlicht device to work.
- Have just one class with the Irrlicht device (let's call it AppClass for now). AppClass needs a function getDevice() to return the Irrlicht device. Pass a pointer to AppClass to the classes which need the Irrlicht device. Advantage is that you don't have Irrlicht device pointers all over the place. Disadvantage is that it's harder to see that this class does need an IrrlichtDevice.
- Again use AppClass as above. Create a global object for it which can be accessed from anywhere directly. Advantage - less typing. Disadvantage, like before it's no longer easy to see who uses the device. Another disadvantage - every class can suddenly use every public function in AppClass. So every function in there is global - and every class you can access in there is also global. So basically you have given up on OO design. Last disadvantage - when starting like this it often leads to code that is not re-usable at all. Because it's so easy to just access the global class everywhere. And because global classes are project specific your code can't be used in any other project (unless it has the same application class). All that said - in small projects having to type less can still be worth it and even lead to more readable code - but generally you will regret it in the long run. So don't use this.
The best code is to find the minimum number of other classes which a class absolutely needs. And only pass pointers (or even better references when possible) to those. If the class needs a pointer constantly then keep that pointer as member. If only 1-2 functions in your class need a pointer then consider as well passing it as function parameter. That is even nicer (but can be slower or leading to too many function parameters).
About your case: Do really all your classes need the IrrlichtDevice? Or maybe some only need the IVideoDriver and others maybe only the ISceneManager? Or some could work with a second Irrlicht device which is using the NULL driver (if you only need file functions).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Keeping Track Of Your Data
Thank you everyone I found a good mix of a few things, I understand it was a broad question :P
Dream Big Or Go Home.
Help Me Help You.
Help Me Help You.