Application crashes when calling 'PackedScene::instance()'

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CosmoXD

Hello, I’m trying to create a random map generator but when i’m trying to instantiate the Node2D that has the required nodes using the instance() function the application just freezes and quits.

Note: The ‘PackedScene’ that has the nodes is a std::vector<PackedScene*>

This is my code:

Player.cpp:

void Player::_ready()
{
    ResourceLoader *resLoader = ResourceLoader::get_singleton();

    this->obstacles = std::vector<PackedScene*>();
    this->obstacles.push_back(cast_to<PackedScene>(resLoader->load("res://Prefabs/Obstacles/Obstacle 1.tscn").ptr()));
}

The part of the code that triggers the crash:

int selectedPiece = this->random(0, this->obstacles.size() - 1);
Godot::print(std::to_string(this->obstacles[selectedPiece] == nullptr).c_str());

if(this->obstacles[selectedPiece] != nullptr)
    this->obstacles[selectedPiece]->instance();