Advertisement Jump to content
Sign in to follow this  
Solid_Spy

Is using static variables for input engine evil?

This topic is 1873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


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

Share this post


Link to post
Share on other sites

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

Edited by Solid_Spy

Share this post


Link to post
Share on other sites

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;

};

Edited by Solid_Spy

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!