This could probably belong on Stackoverflow but I'm not sure.

I want to change some variables that are in my game loop from different classes, this is probably a simple question with something i'm overlooking but I can't think how to do it easily.

The only way I can think to do is pass a pointer to whatever class I want to change it in, but that seems stupid as if I want to do something else it means passing another pointer.

Any ideas to help me?

  • Can you post some code of what you want? – Pubby Dec 9 '11 at 21:50

You could pass in the pointer, you could make a pointer global that you could access (or make it a singleton), you could set up an event system that changes those variables when those events get called, you could have your main loop query the objects that it owns to see what the variables should be... there are a lot of ways of doing this but without more specifics it seems like a really rudimentary programming question.

I will often create a class specifically made for global data, just because I love having the ease of use, it still keeps my code clean, and namely I have a lot of global data in some projects. I have a large component and entity based system though, so creating an entity and adding components for managing global data is a trivial task. As someone else stated though, it seems like an incomplete question with many varied solutions.

If you're not already using a system that I described though, I recommend just making the pointer global.

EDIT: And you're right, this answer is probably slightly better suited for Stack Overflow, though I didn't mind answering it.

Having a class change external data that is just in a loop is not good program design. if you're using classes, that data should be in its own class. If instances of other classes need access to that data, then you need to associate the 2 objects in some way, usually by passing in a reference, or by having a mediator class that communicates for you, or perhaps by some sort of messaging system. But generally speaking classes shouldn't be changing data outside of that class, they should be calling methods on other classes who change their own data.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.