Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Too many parameters! HELP!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 crovea   Members   -  Reputation: 124

Like
0Likes
Like

Posted 07 June 2012 - 11:46 AM

Hey! My first post here so tell me if i'm doing something wrong :P

I've started developing a game in C++. To begin with i just did some proof of concept with lots of (bad?) global variables.
After cleaning up and attempting to exhibit better programming practises i have been moving my variables into my main function and passing them to parameters. This is from my understanding the way to go since you have control of what functions can modify what variables.

The problem is that i'm passing way too many variables to functions that shouldnt need them.
An example is an objects move() function, this move function checks for collisions, if there is a collision it calls a damage function which might cause the player to die and then call a GameOver() function which resets the game.

The game over function needs to reset alot of variables which i as a (good?) c++ developer is passing to it as parameters, but those parameters come from the damage function which gets them from the move function. So when i call the move function in the main function, it basically needs pointers to all the variables in the game which seems really really silly!

I can't possibly believe this is the way to go, but neither is global variables.

So my question is if i should put these variables as static data members of a static class as a way to avoid global variables. But i've heard that this is just as bad as global variables as they suffer from the same problems (Who modified what?)

I've tried to find some high quality open source games on here, but every post with that purpose just tells the OP to go and get his hands dirty himself.

Any advice is appriciated! thanks!

EDIT:
tl;dr Should i use static members for game global variables, or if not, then what?

Edited by crovea, 07 June 2012 - 11:47 AM.


Sponsor:

#2 larspensjo   Members   -  Reputation: 1561

Like
3Likes
Like

Posted 07 June 2012 - 11:57 AM

Consider defining classes and group the parameters into these classes. Try to keep parameters that belong together in the same class. Now you only need to pass around objects for the classes as arguments.

As a next step, consider making the functions you mentioned as member functions to the classes.

The design trick now is to define a good set of classes. It will take some practise to get right. Even experienced programmers have to redesign the class architecture now and then.

If you do this wrong, you put all global parameters into one class and make all functions member functions of this class. Then you are back to the original problem, but in an object oriented way.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

#3 Dawoodoz   Members   -  Reputation: 331

Like
0Likes
Like

Posted 07 June 2012 - 12:06 PM

It is good to use references for all side effects so that all input and output is explicit and the code can be reused in another context. If you have too many small variables, group them together using structs. If you refer to too many things that you don't need, only give references to a subset of the data.

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#4 crovea   Members   -  Reputation: 124

Like
0Likes
Like

Posted 07 June 2012 - 12:11 PM

Thanks alot, this seems to be what i need!
On the subject of references, i don't really see a good reason to use them. (I prefer pointers, const pointers etc)
But from what i've been able to read, thats a personal preference and i'm fairly experienced in C so. (C# also, so references aren't mysterious to me)
Sometimes you NEED to use pointers, other times you CAN use references, so i'd rather make my code more uniform than gain a little safety and 'easy on the eyes notation'.
Unless im missing something about references?

Edited by crovea, 07 June 2012 - 12:12 PM.


#5 larspensjo   Members   -  Reputation: 1561

Like
0Likes
Like

Posted 07 June 2012 - 01:00 PM

I agree with you about references. You can define arguments as references and then mutate the parameters in ways that are unexpected. References is usually only a syntactic sugaring.

There is one way I use references now and then: When I find optimization is needed and arguments are objects of a certain size. For example, sending a 4x4 transformation matrix as argument. In this case, declaring the argument as "const matrix4x4 &transform" will give the speed of a pointer with the syntax of a normal object. This can be done with pointers also (const matrix4x4 *transform), but then you have to change all calls to the function. And it gets messy if you want to use overloaded functions defined for the type (like projectionMatrix * viewMatrix * modelMatrix * vertex).
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

#6 crovea   Members   -  Reputation: 124

Like
0Likes
Like

Posted 07 June 2012 - 01:11 PM

Noted thanks :)

#7 yckx   Prime Members   -  Reputation: 1291

Like
0Likes
Like

Posted 07 June 2012 - 01:20 PM

In many cases pointers and references can be interchanged, but not all. A pointer implies that the value may be null; a reference makes it explicit that null is an invalid value for the variable. Getter functions for classes are typically better implemented as returning a reference to const rather than returning by value. Pointers don't make sense in that context. And operator overloading requires the use of references. If you need to be able to change which object your variable dereferences to, you cannot use a reference, so a pointer is necessary.

References tend to result in cleaner-looking, easier to read code, which is important since coders tend to read code more than they write it. That benefit, small as it may seem, should not be ignored. I concede that in many cases it is more of a stylistic choice, but I encourage the approach of using references when possible and pointers only when one must.

#8 Ravyne   GDNet+   -  Reputation: 8188

Like
2Likes
Like

Posted 07 June 2012 - 02:02 PM

I agree with you about references. You can define arguments as references and then mutate the parameters in ways that are unexpected. References is usually only a syntactic sugaring.


That's completely incorrect. The use of references, and even more importantly 'const', states a contract between the function and the caller that defines if and how the function may modify those parameters. Yes, it's often possible in C++ to wiggle out of those contracts by abusing casts, but just because no contract can force a party to act in good faith does not mean that the contract itself is not important. In C++ the contract is important because the compiler will help you to inadvertently violate it (violating the contract usually requires tricky syntactic gymnastics), and also helps the compiler to understand the code more-fully by precluding certain possibilities, the result of which is that the compiler can often make more-aggressive optimizations.

There is one way I use references now and then: When I find optimization is needed and arguments are objects of a certain size. For example, sending a 4x4 transformation matrix as argument. In this case, declaring the argument as "const matrix4x4 &transform" will give the speed of a pointer with the syntax of a normal object. This can be done with pointers also (const matrix4x4 *transform), but then you have to change all calls to the function. And it gets messy if you want to use overloaded functions defined for the type (like projectionMatrix * viewMatrix * modelMatrix * vertex).


References should always be preferred over pointers, except where pointers are necessary (dynamically-allocated memory or other late-binding, null values, pointer arithmetic, interacting with legacy interfaces that use pointers). When you do need a pointer, the standard library provides a number of smart-pointers that should be preferred over home-grown pointer classes (I refuse to call them "smart" pointers, as home-grown pointer classes are almost universally broken in ways both obvious and not) or raw pointers. With C++ 11, just about every need you'd have for a pointer, aside from ones to deal with legacy interfaces, is met by one of the pointers in the standard library.

'const' should be applied appropriately regardless of whether the parameter is passed by value, pointer, or reference.

Edited by Ravyne, 07 June 2012 - 02:09 PM.


#9 Washu   Senior Moderators   -  Reputation: 5639

Like
0Likes
Like

Posted 07 June 2012 - 02:31 PM

<br />When you do need a pointer, the standard library provides a number of smart-pointers that should be preferred over home-grown pointer classes (I refuse to call them "smart" pointers, as home-grown pointer classes are almost universally broken in ways both obvious and not) or raw pointers. With C++ 11, just about every need you'd have for a pointer, aside from ones to deal with legacy interfaces, is met by one of the pointers in the standard library.<br />


Additionally, even if C++11 isn't available, boost almost always is. Its smart pointers library is well tested and proven, and is almost completely compatible with the C++11 variants (seeing as how the C++11 variants are BASED on the boost ones).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#10 larspensjo   Members   -  Reputation: 1561

Like
0Likes
Like

Posted 07 June 2012 - 04:23 PM


...You can define arguments as references and then mutate the parameters in ways that are unexpected. References is usually only a syntactic sugaring.

That's completely incorrect.

I think there is a misunderstanding, I didn't state the use of "const" in this example.

References should always be preferred over pointers, except where pointers are necessary (dynamically-allocated memory or other late-binding, null values, pointer arithmetic, interacting with legacy interfaces that use pointers).

One advantage is that it is difficult to make a reference that is a null pointer. That means the source code will become more resilient to errors.

The exception, where pointers can have null values, is quite a big exception. It is a common practice to return a 0 pointer to indicate either a failure or that there is no object associated with the pointer. I am not sure if smart pointers help with this?

'const' should be applied appropriately regardless of whether the parameter is passed by value, pointer, or reference.

Agreed.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

#11 rip-off   Moderators   -  Reputation: 8765

Like
1Likes
Like

Posted 08 June 2012 - 04:22 AM

An example is an objects move() function, this move function checks for collisions, if there is a collision it calls a damage function which might cause the player to die and then call a GameOver() function which resets the game.

The game over function needs to reset alot of variables which i as a (good?) c++ developer is passing to it as parameters, but those parameters come from the damage function which gets them from the move function. So when i call the move function in the main function, it basically needs pointers to all the variables in the game which seems really really silly!

I can't possibly believe this is the way to go, but neither is global variables.

The player does not directly tell the game to end. This unnecessarily couples the player class to the game.

One way to decouple your scenario is to use callbacks. The game hooks into the player's death event, and when the player notifies any listeners that it has died, the game can use this to end itself.

Alternatively, when the game is removing dead objects, it can notice that the object that has died is the player, and react appropriately.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS