PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

scope of Global variables not so Global?

jeemigdepeemig · 08 Nov 2005, 22:53 UTC
Hey all (i also posted this in 'general devolper' but it seems more appropriate here),

I use pocketfrog to devolp a game but I have a small problem. I want to acces some variables from my Game object with a function from a Sprite Class. I thought the variables in main are globals, so that you can use them in all classes.

Still if I do this in my Sprite class:
Code:
Movepoints = game.map[Xpos-10/50][Ypos-10/50].movement;


I get a 'game' : undeclared identifier.

This is my WinMain
Code:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int) {
HekkusSample game;
_Module.Init(0, hInstance);
game.Run();
return 0;

}


Can someone spell thuis out for me? I'm pretty new to all this so please explain in detail if you can (not like: "you need a pointer)". :-)
jeemigdepeemig wrote: I thought the variables in main are globals, so that you can use them in all classes.


Variables in main are not global.
I use loads of globals in Space Treker III which is a PF game. If you can show me a code snippet of which variables you want to use I can probably tell you what to do.

ps: you probably just want to lose the 'private' in your main .h file or wherever you are defining stuff.
jeemigdepeemig · 09 Nov 2005, 09:40 UTC
I want to access the game variable 'map' from within another class (the Sprite Class).

This is the .f file of my Game class (called HekkusSample):

Code:
#ifndef POCKETFROG_SAMPLE_Hekkus_H
#define POCKETFROG_SAMPLE_Hekkus_H
#include "sprite.h"
#include "Map.h"
#include "STRING.H"
#include <PocketFrog/PocketFrog.h>
using namespace Frog;


//////////////////////////////////////////////////////////////////////////////
//
// HekkusSample
//
//////////////////////////////////////////////////////////////////////////////

class HekkusSample : public Game {

public:
void StylusDown( Point stylus );

// Construction
HekkusSample();
// Framework callbacks
virtual bool GameInit();
virtual void GameEnd();
virtual void GameLoop();
void DrawTile(Surface *image, int x, int y, Display * display);
int MAP_X; //map size(height)
int MAP_Y; //map size(width)
Map map[5][4];

private:
Sprite mario;
Surface* m_images[3]; // Moving images
Surface* m_number[3];
bool stop; //stop sprite bewegen
bool grid; //showgrid
Rect but_grid;
};


This is the main where the game object is created:

Code:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int) {

_Module.Init(0, hInstance);
HekkusSample game;
game.Run();
return 0;

}


This is a Sprite function from within i wish to access the game variable:
Code:
Movepoints = game.map[Xpos-10/50][Ypos-10/50].movement;


So I think somehow I should make the HekkusSample game global but I don't know how to do that within Main. I have no main.h file or something. When i look at game.h from pocketfrog i see this:

Code:
public:

Game();
virtual ~Game();

// Start running the game
bool Run();


protected:

// Access to devices
Display* GetDisplay();

// Game callbacks


So you would think a Game object should be public right?

If this is all a bit unclear, if you can show how to access a Game object variable from the SpriteClass (without passing it through a function) from the Caveman example from Conan's site it would be great to.
you should be able to get at map without the 'game.' in front??
You can zip up the project if you like as I am off today and could solve it in no time if I could run it.
jeemigdepeemig · 09 Nov 2005, 10:39 UTC
That's cool of you Conan. Unfortunetly I'm at work till 17:00 (GMT).

I'm pretty sure I tried the map instead of game.map syntax but I'm starting to doubt it now :? .

I can't wait to try it out when I get home, if i can't make it work I'll get back to you.

thanks,
jeemig
ok Jeemig, I will wait to hear from you. Don't worry, even when you have years of experience there are still problems which someone else has to help with. I learned to write games in the days before the web and the best you could hope for was a listing in a computer mag and about a week for an answer to a letter :)
jeemigdepeemig,

I think you're just misunderstanding the scope here (or I'm misunderstanding your question which is equally possible :wink:). As Andy said earlier, variables declared in main (or WinMain) are NOT global and cannot be accessed outside the function they are declared in.

To declare game as a global variable, you need to declare it outside any of your functions. I usually do that at the beginning of the source file.

So, simply change this:

Code:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int) {

_Module.Init(0, hInstance);
HekkusSample game;
game.Run();
return 0;

}


to this:

Code:

// I moved the declaration of game OUTSIDE of the WinMain function
// but left the rest of the code unchanged
HekkusSample game;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int) {

_Module.Init(0, hInstance);
game.Run();
return 0;

}


If your Sprite code is in a different file than the WinMain function, just add the following to the Sprite .cpp file,

Code:

extern HekkusSample game;


and things should work as you are expecting.

I hope this helps. If not, it sounds like Conan may be able to do more.

-Brad
jeemigdepeemig · 10 Nov 2005, 11:21 UTC
Hey Brad,

I tried what you suggested but I get a: "syntax error : missing ';' before identifier 'game'" in the extern HekkusSample game; line.

Even so, is this is a good way (maybe the only way) to access a variable from an outside file? From what i've understand the compiler will not allocate memory for the variable. Which sounds not so good performance wise (since the variable is a huge game object).

Thanks for looking at my problem. I send Conan my zipped project and he's looking at it right now. It seems like a great rescourse for me this forum :) .

Greets,
jeemig
For completeness here's what I suggested via email:-
pass the map variable in to the sprite's move function when the sprite move function is called.
This seems the correct way to do this and I now have the code working and a little guy walking on the screen of my X51v.
jeemigdepeemig · 10 Nov 2005, 14:23 UTC
Thanks!

I can continue finally, I have already made a functional movementpoints function. From here on things will be easy :wink:.

Gr,
J
Great! Glad you got it working!

-Brad