Sign in to follow this  
Luke Welburn

I'm having problems with global variables

Recommended Posts

Luke Welburn    100
I've read that you should keep global variables to a minimum with programming. But unfortunately I've seem to have gotten myself into a real bad habit of keep declaring global variables when programming my small platformer. I want to be able to correct this and find a bit way of doing things.

What I've currently done is create a header.h file which contains the variables that I need. Then I call these variables from the different classes(.h) and .cpp files. I know I can declare these variables in my .h files, but I become stuck when I declare the functions as static functions to call other functions from other classes. They only let use the global variables. It seems there should be a better way to do this. Ill show you an example:

[code]void camera::move()
if (camera_reset) reset();

camera_x = camera_x - x_vel;
camera_y = camera_y - y_vel;

//add background
misc::apply_surface(camera_x + 2000,camera_y,background,screen);

//moves level with camera


Because I'm Using the weather::camera_movement() function from a different class, I have to create this function has static, therefore only being able to use global functions from my header file. [img][/img]

void weather::camera_movement(int camera_x)

int i;

if (frame == 0)

//sets amount of snow on screen
for (i = 0; i < amount_of_snow; i++)
//sets random co ords for x and y snow.
snow_x[i] = (rand() % 800);
snow_y[i] = (rand() % 800);

//stop loop so it only randomizes once per turn
if (i == amount_of_snow - 1)
frame = 1;

And if i didnt put my variables in the header.h file, I would just get the following error message:


1>c:\users\luke\documents\visual studio 2010\projects\sdl platformer\sdl platformer\weather.cpp(57): error C2597: illegal reference to non-static member 'weather::frame'
1>c:\users\luke\documents\visual studio 2010\projects\sdl platformer\sdl platformer\weather.cpp(57): error C3867: 'weather::frame': function call missing argument list; use '&weather::frame' to create a pointer to member
1>c:\users\luke\documents\visual studio 2010\projects\sdl platformer\sdl platformer\weather.cpp(70): error C2597: illegal reference to non-static member 'weather::frame'

I also have to put the variables into my main.cpp file at the top as well, and make the pointer = NULL.

I hope this makes sense.


Share this post

Link to post
Share on other sites
SimonForsman    7642
First of all, use member variables in your classes rather than global variables, (Your camera class should only modify variables that are a part of the camera class).
Secondly, Create one instance of the camera class for each camera you need. (for most games you only need one camera).

Thus your camera class should be something like:

#ifndef CAMERA_H_
#define CAMERA_H_
class Camera {
int x,y; //could be floats or doubles aswell if you use those datatypes for positioning the camera.
//other variables your camera needs to track (since i assume its 2D you probably don't need that much more)
Camera(int x, int y) : x(x), y(y);
void move(int dx, int dy);
int getX();
int getY(); //ok, it might make sense to simply make x and y public in this example but for a more advanced camera class you might want the move method to restrict movement (to not allow the camera to pan outside the game area for example thus i made them private anyway)

#include "Camera.h"
Camera::Camera() {
void Camera::move(int dx, int dy) {
int Camera::getX() {
return x;
int Camera::getY() {
return y;

//---- cpp file with the main function ---
#include "Camera.h"
#include "Level.h"
void main() {
Camera myCamera(0,0); //create a new camera at position 0,0
Level someLevel("level1.txt"); //create a new level based on the contents of level1.txt

myCamera.move(10,0); //moves the camera 10 pixels to the right.

someLevel.printMaze(myCamera.getX(),myCamera.getY()); // print a portion of the level based on the cameras position

//i could create multiple cameras now aswell:

Camera myOtherCamera(50,30);
someLevel.printMaze(myOtherCamera.getX(),myOtherCamera.getY()); //prints a portion of the level based on the second cameras position

//The printMaze method could also be changed to take a reference to a Camera object rather than an X and Y coordinate to simply make the call to printMaze look like this:
someLevel.printMaze(myCamera); //nice and short

As you can see i'm not using any global variables at all here, everything is local.

Static methods should only be used when the method has zero interaction with the object which means that in C++ you normally don't want to use them at all (Those functions are almost always better placed in a namespace than in a class)

I'm using CamelCase for classes and lowerCameCase for variables (there are other naming conventions aswell, its a good idea to pick one and stick with it for the project though as it makes reading the code significantly easier)

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