I am making game in C++. I'm using SFML. I have game.h and game.cpp files which are connected and share/use variables.
I want to use some of those variables in other files. I tried example:
extern int variable;
And it is working fine. Problem is when i try to make extern this:
sf::Texture hoverTexture;
std::vector<sf::Sprite> Hover_Tiles;
sf::Sprite hover;
And use those sprites/textures in other cpp file. Just trying to break my code into few files to make it easier to read. So my question would be is it possible to make those mentioned sprites/textures global to use them in other files correctly. What is the best way to do it. If it isn't possible what should I do and how?
How to use those sprites/textures in other files. How to connect them? At least point me to somewhere. Thanks.
Edit:
After all includes on top of game.h
extern std::vector<sf::Sprite> Tiles;
extern sf::Sprite tile;
extern sf::Texture seaTexture;
extern sf::Texture grassTexture;
extern sf::Texture rockTexture;
extern sf::Texture forestTexture;
extern sf::Texture hoverTexture;
extern std::vector<sf::Sprite> Hover_Tiles;
extern sf::Sprite hover;
extern sf::Texture hoverTexture2;
extern std::vector<sf::Sprite> Hover_Tiles2;
extern sf::Sprite hover2;
extern sf::Texture houseTexture;
extern std::vector<sf::Sprite> House_Tiles;
extern sf::Sprite house;
extern int map[20][10];
In other file map.cpp
#include "game.h"
#include "map.h"
#include "SFML\Graphics.hpp"
#include <vector>
std::vector<sf::Sprite> Tiles;
sf::Sprite tile;
sf::Texture seaTexture;
sf::Texture grassTexture;
sf::Texture rockTexture;
sf::Texture forestTexture;
sf::Texture hoverTexture;
std::vector<sf::Sprite> Hover_Tiles;
sf::Sprite hover;
sf::Texture hoverTexture2;
std::vector<sf::Sprite> Hover_Tiles2;
sf::Sprite hover2;
sf::Texture houseTexture;
std::vector<sf::Sprite> House_Tiles;
sf::Sprite house;
int map[20][10] =
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 }
};
In the same map.cpp I made void function which is first defined in map.h. That function holds some code that works great in game.cpp (if this code I posted up is in game.h). I also have problem with Exception thrown:
Exception thrown at 0x7749B2E3 (ntdll.dll) in App.exe: 0xC0000005:
Access violation writing location 0x00000004.
At map.cpp line:
sf::Texture seaTexture;
I hope this code I posted helps in fixing problem. When I transfer those sprites/textures/variables into other file using extern it won't work but everything in one (game.h and game.cpp) works great.
You need to include lib header, before you declare the extern variable. Otherwise the compiler has no idea what Class is.
otherfile.h
#ifndef OF_H_
#define OF_H_
#include <Texture.hpp>
#include <Sprite.hpp>
#include <vector>
extern sf::Texture hoverTexture;
extern std::vector<sf::Sprite> Hover_Tiles;
extern sf::Sprite hover;
#endif
game.cpp
#include <game.h>
sf::Texture hoverTexture;
std::vector<sf::Sprite> Hover_Tiles;
sf::Sprite hover;
User contributions licensed under CC BY-SA 3.0