For Loop Theory for Tile Map Editing Program

I decided not to post the whole back story to this program, but I could use some help with it :P (more for flexibility reasons). So basically I have an tile class with the following variables: x, y, texture, and solid. I have created an array called tile MAP[map_tiles_x][map_tiles_y]. The object of the following snip-it of code is to loop through all of the tiles in the map placing their default values in each. For some reason this code does not work as planned, but in theory shouldn't it? I cannot see why it doesn't work. Thanks in advanced for help :P (BTW I do plan on open sourcing this map editor :D)

1
2
3
4
5
6
7
8
9
10
void init_map(){

for (int tile_y; tile_y < map_tiles_y; tile_y++){
for (int tile_x; tile_x < map_tiles_x; tile_x++){
MAP[tile_x][tile_y].x = tile_x * tile_size;
MAP[tile_x][tile_y].y = tile_y * tile_size;
MAP[tile_x][tile_y].solid = false;
std::cout << MAP[tile_x][tile_y].x << ", " << MAP[tile_x][tile_y].y;
}
}


I should also add the effect of the code. Basically it only sets the default coordinates for the first object :/.
Last edited on
closed account (N36fSL3A)
Why limit yourself with an array? Use a vector.
Ehh it's just a prototype of the code, once I can figure out why this doesn't work I will probably change it :P.

Plus I think an array is all that's necessary right now :P.
Last edited on
closed account (N36fSL3A)
I don't like working with 2d arrays.

What I did in my game is simply make a map class, and my stuff looked like this:

1
2
3
4
5
6
7
8
9
10
11
12
class Map
{
    public:
        int Init();
        void UpdateTiles();
        void Render();
        void Cleanup();
    private:
        int Width;
        int Height;
        std::vector<Tile*> tiles;
};


This way it's all in one vector/array. It makes everything easier.

How I initialized it was this

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
	// A for loop to go through the tiles one by one
	for(int t = 0; t < MaxTiles; t++)
	{
		// Make a offset for the tile types
		int tiletype = -1;

		map >> tiletype;
		// If the map fails to get the tile id
		if(map.fail())
		{
			// Display this message
			std::cout << "Map failed to load.\n";

			// Close the map file, then return an error value.
			map.close();
			return 1;
		}
		// If the tile type is a valid number, go on to work with them
		if((tiletype >= 0 ) && ( tiletype < TILETYPES))
		{
			// Add a tile to the vector at the coordinates of x, and y, then with the tiletype
			// loaded from the map.
			Tiles.push_back(new Tile(x, y, tiletype));
		}

		// If it isn't a valid number close the map immediately and return an error
		else
		{
			map.close();
			return 1;
		}

		// Add the value of x by 1 tile pixel width each time a tile is loaded
		x += TILE_W;
		// If the x value passes the level width we set x to 0 and
		// make the y value move on tile height down.
		if(x > MapW)
		{
			x = 0;
			y += TILE_H;
		}
	}


It's easily expandable to make it take the "solid" value.
For some reason this code does not work as planned, but in theory shouldn't it? I cannot see why it doesn't work.

Initialize your variables.

1
2
for (int tile_y = 0; tile_y < map_tiles_y; tile_y++){
    for (int tile_x = 0; tile_x < map_tiles_x; tile_x++){


Pay attention to the warnings generated by your compiler.
Topic archived. No new replies allowed.