This one should be an easy fix, or so I thought.
Error
Segmentation error while initializing my world.
A function is called to move a Vehicle, this checks if the location it would move to is valid, and there I need to reach back to the World to see its boundaries. This pointer to the World is not initialised correctly. However, in the debugging window, the world pointer is shown to be valid.
Note: The world pointer in the debugger shows an initialised world (so it's not randomly constructing an empty one).
! Look at this image: Debugger shows valid pointer, however, it still errors. I am very confused.
Relevant (?) Code
Function called:
void World::vehicleMoverTimerPing()
{ // Function every time short timer timeouts
// move player
if (player->speed != 0) {
player->move(game->TIMER_SHORT_DURATION);
}
// add to age (in seconds)
age += (float)game->TIMER_SHORT_DURATION/1000;
}
Class Player:
class Player : public Vehicle
{
private:
World * world;
public:
Player(World*);
};
Player::Player(World * w) : Vehicle(w)
{
// add to world & game
world = w;
world->player = this;
}
Class Vehicle:
class Vehicle : public QObject, public QGraphicsPixmapItem {
private:
World * world;
// PLAYER CANNOT ONLY ACCESS THESE VARIABLES THROUGH FUNCTIONS OF VEHICLE
// driving variables
float orientation_degree;
bool location_is_valid(QPointF); // IN GRID COORDS
QPointF direction(); // orientation
public:
// type & constructor
Vehicle_type * type;
QPointF location;
float speed;
Vehicle(World*, QPointF, Vehicle_type*, QObject *parent=nullptr);
Vehicle(World*);
World* getWorld() {return world;}
... unrelated
}
Vehicle::Vehicle(World* w, QPointF spawn_position, Vehicle_type* t, QObject *parent) : QObject(parent)
{
world = w;
speed = 0;
type = t;
// spawn at center of the tile at position spawn
location = spawn_position+QPointF(0.5 - t->dimensions.x()/2, 0.5 - t->dimensions.y()/2);
orientation_degree = 0;
turn(orientation_degree);
}
Vehicle::Vehicle(World * w)
{
Vehicle(w, w->viewportCenter, w->getGame()->vehicle_types[0]);
}