Is using static variables for input engine evil?

Started by
12 comments, last by Satharis 10 years, 5 months ago

Hello, I am currently writing a game engine, and I am thinking of switching to static variables for my input engine. At first, I would pass the input engine over to objects that need it, for example the Player. Every time I create a player, i would pass the input engine over to it, like this:

player1->inputEngine = inputEngine;

However, I found this to be very tedious, and have decided to instead make all the variables in the Input engine class static, so that i wont have to copy the reference over. Would you consider that to be a good idea? I have a feeling that this might be the same as using global variables, which I know is evil, but what other alternative do I have?

View my game dev blog here!

Advertisement

1 line of code is tedious? What if you have 2 players on one machine using different input methods? What if you want ghost players e.g. in a racing game which playback previous stored input?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

1 line of code is tedious? What if you have 2 players on one machine using different input methods? What if you want ghost players e.g. in a racing game which playback previous stored input?

I meant in the long run, what if i have a graphics engine, sound engine, and a hundred objects that all use different engines

View my game dev blog here!

I don't really know what you mean by engine in this context. Maybe the engine should have a container of all the objects it does stuff with instead?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I don't really know what you mean by engine in this context. Maybe the engine should have a container of all the objects it does stuff with instead?

I want the game loop to first check the input, then loop through all the game objects and call their update functions. the update functions will use the input data to determine what to do.

I'l give example:

class InputEngine

{

public:

InputEngine();

static bool leftPressed;

};

View my game dev blog here!

Yes it is! You're Hitler to me now.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

I want the game loop to first check the input, then loop through all the game objects and call their update functions. the update functions will use the input data to determine what to do.
I'l give example:

class InputEngine
{
public:
InputEngine();
static bool leftPressed;
};


There's nothing evil about this, but there's no need to make that static. Static means that all instances will of InputEngine will all use the same variable. In another post you said each player gets a reference to the input engine so this doesn't make sense and isn't needed.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

I want the game loop to first check the input, then loop through all the game objects and call their update functions. the update functions will use the input data to determine what to do.
I'l give example:

class InputEngine
{
public:
InputEngine();
static bool leftPressed;
};


There's nothing evil about this, but there's no need to make that static. Static means that all instances will of InputEngine will all use the same variable. In another post you said each player gets a reference to the input engine so this doesn't make sense and isn't needed.

no, I meant that if I don't pass it by reference I use static variables.

View my game dev blog here!

Global state is a bad idea in general, and I am sure it's easy to find descriptions of why with a web search. I would much rather pass references to the InputEngine everywhere (I don't love the name: How about InputState?), and thus make the dependence explicit.

Here's an alternative that I find elegant: Use signals and slots, so the part of the code that manages the input issues a signal whenever left is pressed, and the player's character object has a slot that moves it to the left. There is some code that instantiates all the objects and hooks the signals and the slots, and that's the only part of the code that knows about the connexion between the input and the player's character. That way the input handler doesn't know anything about the character and the character doesn't know anything about the input handler.

One of the strong points of an architecture like the one I just described is that it allows you to test the individual objects, by hooking the signals and the slots to dummy versions of the objects that should be on the other side. If you have a lot of global state, you can't possibly test any class in your code without instantiating all the global objects that it might make use of.

I want the game loop to first check the input, then loop through all the game objects and call their update functions. the update functions will use the input data to determine what to do.
I'l give example:
class InputEngine
{
public:
InputEngine();
static bool leftPressed;
};


There's nothing evil about this, but there's no need to make that static. Static means that all instances will of InputEngine will all use the same variable. In another post you said each player gets a reference to the input engine so this doesn't make sense and isn't needed.
no, I meant that if I don't pass it by reference I use static variables.

Sorry, I didn't really explain well. I accidently hit the post button (I'm on my phone) and then my wife made me go out before I could really fix it.

Statics and globals seem like an easy fix early, but often they get in the way later. Then you have statics and globals, too many things have access to the variables. This can cause problems later if you want to change your implementation. Only use globals if it "needs" to be accessed by everything which is very rare. In your case, only the player needs to access that data so it would be better to just pass it by reference to what needs it.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

This topic is closed to new replies.

Advertisement