Question

(I apologise if something's wrong, it's my first question)

I'm trying to create a grid of rooms for my game (similarly to Binding of Isaac).

I have a commanding class called 'Station' in which i want to have a 2D array of rooms, which I defined in my class 'Room'. In my main.cpp I'm using a Room object called currentroom, that I want to pass by reference to a function loadRoom in my Station class. That function would generate all Rooms in the array and pass the middle one back to main.cpp by reference. I've been tring to make it work with vectors, arrays, arrays of pointers and even new classes designed solely for the purpose of storing Rooms, but nothing seems to have worked. Please help me, these are my 2 classes mentioned before: Station.h:

#pragma once
#include "The Station.h"

class Station
{
public:
    void pause();
    void resume();
    int h,w;
    Station();
    sf::RenderWindow App;
    sf::Image guii;
    sf::Texture player, bgt,   guit, fltilt;
    sf::Font arial;


    void loadSection(Room& currentroom);
};

Room.h:

#pragma once
#include "The Station.h"

#define n 16
#define m 6
class Station;
class enemy;
class projectile;
class Room
{
public:
    tile floortile[n][m];
    sf::Texture floort,enemyt, dleftt,sht, drightt,dtopt,dbottomt, chestt;
    int chest_max=15;
    int chestcount;
    bool dleft, dright,dtop,dbottom,isempty;
    Room();
    void loadroom(int w, int h);
    std::vector <enemy> enem;
    std::vector <projectile> proj;
    void spawn_enemy(int x,int y);
};

I would like the first one to have the array of the latter. Thank you.

EDIT: Ok, here's what I got: When I simply try to make a 2d array like this:

Room sect[10][10];

and I put it in a class before the void loadSection, it crashes a second after running. When I make an object of class Section called 'lvl' in the same place, it only crashes when I try to access a member of a Room, like this:

this->lvl->Row[5].Column[5].loadroom(this->w,this->h);

However, when I create a vector of vectors, like this:

std::vector< vector< Room > > station;

it crashes when I try to resize it, no matter if I do it with an initializer, push_back, or vector::resize.

The error is always the same, Application.exe stopped working.

Any ideas?

Was it helpful?

Solution

I fixed the problem. I removed the loadRoom function and put its code into the default constructor. The other problem I had with it, was that the sf::Texture operator= overload was causing a crash, so I loaded the textures from files again instead of copying them. I know its wasteful and slow, but I'm going to write it from scratch in Unity in a few months, so I don't care if its optimized or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top