1 year ago
#366921
Nick Román
C++ make_unique() changing member attributes' values
I am developing a small game in SDL2 and am in the process of implementing a Quadtree for collision detection in the ECS I have written. The structure of my Quadtree class is similar to this one and the full implementation can be found below.
Quadtree.hpp
#pragma once
#include <vector>
#include <algorithm>
#include "Box.hpp"
namespace quadtree {
class Node {
public:
Node(int p_level, quadtree::Box<float> p_bounds)
:level(p_level), bounds(p_bounds)
{
parentWorld = NULL;
for (int i = 0; i < 4; i++) {
childNodes[i] = nullptr;
}
}
// NOTE: mandatory upon Quadtree initialization
void setParentWorld(World* p_world_ptr) {
parentWorld = p_world_ptr;
}
void clear() {
;
}
void split() {
;
}
int getIndex(Entity* p_ptr_entity) {
;
}
void insertObject(Entity* p_ptr_entity) {
;
}
std::vector<Entity*> retrieve(Entity* p_ptr_entity, std::vector<Entity*> returnObjects) {
;
}
World* getParentWorld() {
return parentWorld;
}
private:
int MAX_OBJECTS = 10;
int MAX_DEPTH = 5;
World* parentWorld; // used to unpack entities
int level; // depth of the node
quadtree::Box<float> bounds; // boundary of nodes in the game's map
std::vector<Entity*> objects; // list of objects contained in the node: pointers to Entitites in the game
Node* childNodes[4];
quadtree::Box<float> getQuadrantBounds(quadtree::Box<float> p_parentBounds, int p_quadrant_id) { ;
}
};
}
The Quadtree is then passed to a CollisionDetection system class:
DetectCollisions in allSystems.cpp
class DetectCollisions : public System {
public:
DetectCollisions() {
signature.addComponent<Transform>();
signature.addComponent<Hitbox>();
quadtree::Box<float> mapBoundaries = quadtree::Box<float>(0.0f, 0.0f, 1280.0, 736.0);
quadtree::Node quadtree = quadtree::Node(0, mapBoundaries);
collisionQuadtree = &quadtree;
}
quadtree::Node* getTree() {
return collisionQuadtree;
}
void update(int dt) {
;
}
private:
quadtree::Node *collisionQuadtree;
};
So far there's no problem really: when I check the values of the collisionQuadtree after creating the System all the values are the ones used during initialization of said system. The problem arises when adding this system to the ECS' world using unique pointers:
Main.cpp
#include "allSystems.cpp"
#include "world.hpp"
using namespace arcade;
int main() {
auto entityManager = std::make_unique<EntityManager>();
auto world = std::make_unique<World>(std::move(entityManager));
std::unique_ptr<System> keyboard_player_status = std::make_unique<KeyboardPlayerStatus>();
std::unique_ptr<System> movement = std::make_unique<Movement>();
std::unique_ptr<System> render = std::make_unique<Render>();
std::unique_ptr<System> detect_collisions = std::make_unique<DetectCollisions>(); // some attributes of the Quadtree have changed
DetectCollisions test = DetectCollisions();
quadtree::Node* test2 = test.getTree(); // attributes are as expected
world->addSystem(std::move(detect_collisions));
world->addSystem(std::move(keyboard_player_status));
world->addSystem(std::move(movement));
world->addSystem(std::move(render));
}
All other systems are working well. However, as I've pointed out in the code, creating a unique_ptr of this particular system changes the size of some of the parameters, in contrast with simply creating an instance of the DetectCollision() system. Here's an example using the Debugger in VS:
detect_collision instance (unique pointer, changed values)
test2 instance (expected values)
Is it reasonable, then, to deduce that it is std::make_unique() what's causing the variables to change their values? If so, what is a possible workaround? And if not, what am I missing?
c++
pointers
unique-ptr
entity-component-system
0 Answers
Your Answer