Page 1 of 1

setting a value in a vector.

Posted: Sun Feb 01, 2009 12:58 pm
by ismetteren
I am having a problem with the part of my game that loads a level from a text file. By lots of cout's i have tracked down the line that is causing the program to shut down(stop responding/answering - not sure what it is called in the eng. version of windows).

It looks like this:

Code: Select all

Tile t;
t.setX(k*32);
map.at(i).at(j) = t; //problem here!!
map is a vector of vectors of tiles. The only thing that has been done to the vector before this, is the creation in the class definition:

Code: Select all

vector<vector<Tile> > map;
I cant see why i should be able to do this, but if this is illeagel, what can i do instead?

Re: setting a value in a vector.

Posted: Sun Feb 01, 2009 1:29 pm
by Ginto8
:shock: Why are you doing a vector of vectors? Are you doing an accumulated vector that includes every single level? 'cuz if it's just one level, all you need is a vector of tiles. Different vectors for different layers (if you're doing layers).

Re: setting a value in a vector.

Posted: Sun Feb 01, 2009 2:14 pm
by BOMBERMAN
Is "tile" a class or a structure or what?

If it's a class you should do like this: std::vector<tile*> Tiles;
And then do like this to create a new tile inside the vector: Tiles.push_back(new tile(constructor arguments...));

Re: setting a value in a vector.

Posted: Sun Feb 01, 2009 2:22 pm
by sparda
loads a level from a text file. By lots of cout's i have tracked down the line that is causing the program to shut down
You know, you guys should get in the habit of using a little debugging tool that will help you immensely. Every time you load a file, allocate data, or anything else that might cause a system crash (seg fault?), USE YOUR BRAINS!!! hahaha, okay, okay, I'm just kidding.

But seriously though, aside from normal exception handling (try, catch, or even a simple test if-statements), use the __LINE__ constant defined in <iostream> or <stdio.h> (for C).

That way, all you need to do is refer to the line where the program messed up, instead of going through a bunch of couts, or printfs.

Code: Select all

#include <stdio.h>
int main (int argc, char * const argv[]) {
	
	if( !load_this_shit("tiledata.dat") ) printf("Fucked up at line: %d", __LINE__);
    return 0;
	   
}

Re: setting a value in a vector.

Posted: Sun Feb 01, 2009 5:30 pm
by MarauderIIC
If your vectors haven't been allocated, "map.at(i).at(j) = t; //problem here!!" then i and j won't exist and this will throw an error. You need to push_back() or resize() (on both vectors).

So you might do this during level load:
innervector.push_back(every tile in a row)
outervector.push_back(innervector)

Re: setting a value in a vector.

Posted: Mon Feb 02, 2009 9:36 am
by ismetteren
Ginto8 wrote::shock: Why are you doing a vector of vectors? Are you doing an accumulated vector that includes every single level? 'cuz if it's just one level, all you need is a vector of tiles. Different vectors for different layers (if you're doing layers).
I dont know if it is a good way to do it in, but the position in my simulated 2D array, dicides where the tiles is located on the map. The x and y coordinate in my tile class just shows where on the tilesheet they are.

And thanks marauder. :D

Re: setting a value in a vector.

Posted: Mon Feb 02, 2009 1:57 pm
by Ginto8
ismetteren wrote:
Ginto8 wrote::shock: Why are you doing a vector of vectors? Are you doing an accumulated vector that includes every single level? 'cuz if it's just one level, all you need is a vector of tiles. Different vectors for different layers (if you're doing layers).
I dont know if it is a good way to do it in, but the position in my simulated 2D array, dicides where the tiles is located on the map. The x and y coordinate in my tile class just shows where on the tilesheet they are.

And thanks marauder. :D
Here's an idea: make the x and y of the tile the x and y in TILES. When drawing them, just multiply the x and y by the width/height of the tiles. It'll make them easier to manage, methinks. ;)

Re: setting a value in a vector.

Posted: Mon Feb 02, 2009 4:16 pm
by MarauderIIC
If you have an upper bound on your map size, arrays are easier to deal with, not to mention faster (at least in comparison to using .at() instead of [][]) and with less overhead. Define a couple integer constants to use as upper bounds.