Tile Editor Problem

closed account (N36fSL3A)
I didn't have to resort to this, because I'm sure this is a simple problem. However, I've been dealing with this for quite a while now. For some reason it keeps saying my vector is out of range.

I've been setting breakpoints and such, reading the values of my variables.

(the map area + 1 thing was a debugging thing)

So I explained the problem, here's my function.

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
Map* NewMap(int w, int h)
{
	Map* map;

	int x = 0;
	int y = 0;

	int mapTileArea = w * h; // Make a interger that holds the area of map tiles

	map = new Map;

	// Set variable values
	map->setMapH(h);
	map->setMapW(w);

	map->setMaxTiles(mapTileArea);

	// Pre-allocate memory for speed
	map->Tiles .reserve(mapTileArea + 1);
	map->Tiles2.reserve(mapTileArea + 1);
	map->Tiles3.reserve(mapTileArea + 1);
	map->Tiles4.reserve(mapTileArea + 1);
	map->Tiles5.reserve(mapTileArea + 1);
	map->Tiles6.reserve(mapTileArea + 1);

	for(int t = 0; t < mapTileArea; t++)
	{
		// Add a new tile to the map's vector
		map->Tiles .push_back(new Tile(x, y, 2)); // Fill the bottom layer with "void" tiles
		map->Tiles2.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles3.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles4.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles5.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles6.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles

		x += TILE_W;

		if(t == mapTileArea)
		{
			std::cout<< "hai";
		}

		// if x goes too far...
		if(x < map->getMapW())
		{
			x  = 0;
			y += TILE_H;
		}
	}
	return map;
}


It's somewhere in the loop, but I don't know why it's saying this.
I don't know what your constructor looks like for Map, but try changing line 10 to:

map = new Map();
Last edited on
the code is not that neat, but it shouldn't cause any problem.

where does tha 'out of range' problem occur?
closed account (N36fSL3A)
I used breakpoints to narrow it down to there. Occurs some where in the loop, like the variable is going too high for some reason.

I know the code isn't neat, it was a quick thing to do.

I don't worry about making neat code at first, I see results, then optimize the code, and try to make my program more efficient.

The problem occurs here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	for(int t = 0; t < mapTileArea; t++)
	{
		// Add a new tile to the map's vector
		map->Tiles .push_back(new Tile(x, y, 2)); // Fill the bottom layer with "void" tiles
		map->Tiles2.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles3.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles4.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles5.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles
		map->Tiles6.push_back(new Tile(x, y, 2)); // Fill the layer with transparent tiles

		x += TILE_W;

		if(t == mapTileArea)
		{
			std::cout<< "hai"; // Set a breakpoint here.
		}

		// if x goes too far...
		if(x < map->getMapW())
		{
			x  = 0;
			y += TILE_H;
		}
	}
Last edited on by Fredbill30
well, this code does not cause problems. except mapTileArea is an astronomical number.

line 15 cannot be executed.

the conclusion is that you did something wrong (out of bounds?) before this.
closed account (N36fSL3A)
Thanks guys. Turns out it was somewhere completely different then here. It was in a function that determined where my cursor went.
Last edited on by Fredbill30
Topic archived. No new replies allowed.