Where to store constants?

Started by
4 comments, last by ChurchSkiz 12 years, 9 months ago
Hi,

Another question.
I want to store a gravity constant in my game that affects many things in different place so I want a constant variable that is accessible from many places. I was about to make a header file called constants that literally just has const float GRAVITY = n;
I thought this looked a bit stupid and out of place.

How do people normally do this? Or is this fine and I'm just having a sly bout of o.c.d.?

Cheers

"To know the road ahead, ask those coming back."

Advertisement
Do you have a core "Game" object? If so, I'd plug it in there as a property something similar to Game.GloablGravity.

I was about to make a header file called constants that literally just has const float GRAVITY = n;


That seems like an acceptable solution. As long as you're using some named constant, that's what really matters with regards to the impact on your design.
Why are many parts of your code concerned with the value of gravity? Other than the part of your simulation that computes the forces acting on each object, I can't imagine where else it might appear.
Mmmmmmmm, yeah maybe your right alvaro, I'm now going to par-take in some serious re-design.

Cheers

"To know the road ahead, ask those coming back."

Alvaro hit it on the head, the only thing that should "know" the gravity is the physics system that applies it. Pass the object you want into your physics simulator and have it do the calculation you want.

ie

Player P;
PhysicsSystem physics;
physics.calculateGravityEffect(P);

As far as storing constants, a good way to do it is to store the values in a text file with other constants. That way you can make simple design/testing changes without having to recompile.

This topic is closed to new replies.

Advertisement