Array not updating right

Hi, I am writing a "building game" with sfml and C++. I managed to make it work in one class, so I figured out I had to do it with classes. I have currently made a level array where the problem is. Whenever I call draw the array all the numbers go to -842150451, so it wont draw something. I don't know how I can fix the problem with it.
Here is my relevant header file:
1
2
3
4
	const static int tileMapHeight = 16;
	const static int tileMapWidth = tileMapHeight * 2;

	int level[tileMapHeight*tileMapWidth];


Here is the cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      void mainGame::Initialize(sf::RenderWindow* window){
	std::cout << "Init\n";
	int level[] = {
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
	};


	if (!map.load("tileset.png", sf::Vector2u(32, 32), level, 32, 16))
		std::cout << "Error Updating level\n";
      }

      void mainGame::updateLevel(){
	if (!map.load("tileset.png", sf::Vector2u(32, 32), level, 32, 16))
		std::cout << "Error Updating level\n";
	for (int i = 0; i < 512; i++){
		std::cout << level[i] << std::endl;
	}
      }

      void mainGame::Update(sf::RenderWindow* window){
	//Process input
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
		quitGame = true;
	if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && mouseDown){
		int mousePosX = sf::Mouse::getPosition(*window).x;
		int mousePosY = sf::Mouse::getPosition(*window).y;
		int tileX = mousePosX / 32;
		int tileY = mousePosY / 32;
		int tilePressed = tileY*tileMapWidth + tileX;
		//std::cout << "Tile pressed: "  << tilePressed << "\n";
		if (money > 0){
			int tileTexture = level[tilePressed];
			if (tileTexture > 64)
				tileTexture = -1;
			level[tilePressed] = tileTexture + 1;
			mouseDown = false;
			money -= 10;
			ErrorString = "";
			updateLevel();
		}
		else{
			ErrorString = "To little money";
		}
	}
	if (sf::Event::MouseButtonReleased){
		mouseDown = true;
	}
      }


What is the problem?

In mainGame::Initialize you define and initialize a local-to-the-function variable level which stops existing when the function returns.

In mainGame::updateLevel and mainGame::Update the level you are accessing is not the same variable.
How can I fix it, should I make a constructor?
Quick fix, assuming that the two arrays are actually the same size/type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <algorithm>
#include <iterator>
//...

      void mainGame::Initialize(sf::RenderWindow* window){
	std::cout << "Init\n";
	int level[] = {
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 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2, 3, 4, 5, 6, 7, 1, 3,
		0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 2, 2, 3, 6, 6, 1, 1, 1, 3, 3, 3, 7, 7, 7, 7, 1,
		8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 1, 3,
	};

	std::copy(std::begin(level), std::end(level), this->level);

	if (!map.load("tileset.png", sf::Vector2u(32, 32), level, 32, 16))
		std::cout << "Error Updating level\n";
      }
should I make a constructor?

I think it's a better idea since a constructor is automatically called and you don't need to call the initialize function.
Most important is that your level variable becomes a member variable.
Cire it worked with the std::copy, but it stops after I have pushed it some times. Also whould the level be a xml file? I read it some other place, that tilemaps often is a xml file
Found out It stopped because I did not have more money :/
Topic archived. No new replies allowed.