• 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
Agreon

Whats cleaner? More global variables or getting Values thorugh functions?

7 posts in this topic

Hey,

in my previous post i asked about my Input Manager. But then something came in my mind.

Instead of calling the "isKeyDown" function everytime, couldn´t i just set a global variable bool key[] and only change and check them?

Or when i want to get my mouse coordinates, do i call "getMousePositionX" or do i just create global variables "mouseX" and "mouseY"?

Whats cleaner?

0

Share this post


Link to post
Share on other sites

Global state is generally to be discouraged for the reasons ApochPiQ mentioned, but you should also aim to encapsulate systems and their state using functions to provide a stable interface. It is not a big stretch to imagine that, at some point, you might have multiple threads and then you are reading and writing the key & mouse states from different threads; if that state was global you might quickly run into subtle concurrency bugs and race-conditions whereas if you had encapsulated it behind functions then you can implement a suitable locking strategy without affecting how the rest of the system interacts with the input-manager.

2

Share this post


Link to post
Share on other sites

Also be aware that that this isn't a two-party system -- the options are not Globals on the left and Functions on the right. A perfectly-acceptable middle-ground might be to poll the input once per frame, hold it in a variable that's defined in a suitably-small scope, and then access that variable wherever keystate is needed. You might access that variable through its name alone, or as part of a class -- whichever you do, you might get ahold of it simply by being in a nested scope, or because it was passed into a function, or because a reference to it was passed into a class that contains that functionality. If you make reasonable choices, and organize your project well, such an approach need not introduce the same kinds of problems that honest-to-goodness global state does.

 

In general, you should prefer to pass information to functions or classes explicitly, rather than simply making it available implicitly. In this way you state which code is dependent upon which information (or which other code), which makes things easier to keep track off. Furthermore, if you later decide that a class or function really ought to depend on some other information (could just be a different source, it doesn't have to be a different kind of information) then you only have to update those places where the information (or functionality, if a class) is passed in, rather than every place where it's used.

 

This makes your code far easier to understand, reason about, and change. The code becomes less brittle and more robust.

1

Share this post


Link to post
Share on other sites

Hey,

in my previous post i asked about my Input Manager. But then something came in my mind.

Instead of calling the "isKeyDown" function everytime, couldn´t i just set a global variable bool key[] and only change and check them?

Or when i want to get my mouse coordinates, do i call "getMousePositionX" or do i just create global variables "mouseX" and "mouseY"?

Whats cleaner?

 

You could have a function in your Input Manager that gives you an array of bools as its result, or copies out an entire struct with that array, plus the mouse buttons and whatever else you need, if that's what you prefer to interact with.

0

Share this post


Link to post
Share on other sites


Also be aware that that this isn't a two-party system -- the options are not Globals on the left and Functions on the right. A perfectly-acceptable middle-ground might be to poll the input once per frame, hold it in a variable that's defined in a suitably-small scope, and then access that variable wherever keystate is needed. You might access that variable through its name alone, or as part of a class -- whichever you do, you might get ahold of it simply by being in a nested scope, or because it was passed into a function, or because a reference to it was passed into a class that contains that functionality. If you make reasonable choices, and organize your project well, such an approach need not introduce the same kinds of problems that honest-to-goodness global state does.

 

i used to do something like this, get all key states all at once per frame (can't remember the windows call), stuff it into an array[256] of byte, then just check the values in the array as needed. the array was global in the low level keyboard input library. process_input and getstring were about the only things that used it.

 

now i just do:

 

if (Zkeypressed(VK_somecode)) blah_blah_blah();

 

where Zkeypressed calls the windows routine that uses VK codes: GetAsyncKeyState()

 

it really makes life easier. except for an input mapper, the keyboard input module is reduced to:

 

// returns 1 if vkey is pressed, else returns 0
int Zkeypressed(int vkey)
{
SHORT a;
a=GetAsyncKeyState(vkey);
if (a & 0x8000) return(1);
return(0);
}
0

Share this post


Link to post
Share on other sites

editor is screwing up.....

 

continued from last post

 

 

then you just call it as needed, where needed, when needed. No having to get and store key states. No scope or data hiding issues. windows is already doing those things. no hassles. nice, sweet, simple, and clean. just like you want writing code to be.  ; )

0

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