Hey, C++ noob here. Can someone help me with this?
I'm programming a roguelike, and I want my current level (containing tiles, monsters, items, etc) to be more accessible to other functions. Right now, it's a LevelMap object tucked inside a World object. If I want to access something in a level, I need to type world.levels[world.currentlevel].foobar which feels excessive. After looking at other roguelikes, it seems like they have a single global level. It's loaded in and out of memory and can be accessed with simply level.foobar. Cool. So I tried this:
class LevelMap
{
// LevelMap definitions
}
extern LevelMap currentlevel;
LevelMap currentlevel;
// LevelMap functions
That compiles without any warnings or errors...but it immediately crashes and gives me a segmentation fault before the program even reaches main(). What gives? Can anyone tell me why this approach would not work for my LevelMap?
EDIT:
I think I found the culprit:
Tile(int tileID = 0, int xpos = 0, int ypos = 0)
{
name = TileIDList[tileID].name;
// Setting a bunch of other tile variables
}
When constructing the tiles in my map, there's a segmentation fault when setting the name. TileIDList[tileID].name is a const char* array stored in a struct. If I comment out that line (so all tiles are nameless) the program compiles just fine. I'm dumbfounded, because I'm constructing my Monsters in the same exact way with MonsterIDList[monsterID].name. I hate C++ strings.