• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Crovea

Too many parameters! HELP!

10 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='larspensjo' timestamp='1339095647' post='4947131']
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.[/quote]

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.

[quote]
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).
[/quote]

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
2

Share this post


Link to post
Share on other sites
[quote name='Ravyne' timestamp='1339099336' post='4947169']<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 />[/quote]

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).
0

Share this post


Link to post
Share on other sites
[quote name='Ravyne' timestamp='1339099336' post='4947169']
[quote name='larspensjo' timestamp='1339095647' post='4947131']
...You can define arguments as references and then mutate the parameters in ways that are unexpected. References is usually only a syntactic sugaring.
[/quote]That's completely incorrect.[/quote]
I think there is a misunderstanding, I didn't state the use of "const" in this example.
[quote]
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).
[/quote]
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?

[quote]
'const' should be applied appropriately regardless of whether the parameter is passed by value, pointer, or reference.
[/quote]
Agreed.
0

Share this post


Link to post
Share on other sites
[quote]
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.
[/quote]
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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0