[GDNative] "Class 'PhysicsDirectSpaceState' or its base class cannot be instantiated"

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

I’m attempting to extend the GDNative C++ example provided through the official documentation. I want to inherit PhysicsDirectSpaceState.

When I attempt to create a new instance of this class, I’m greeted with
ERROR: Class 'PhysicsDirectSpaceState' or its base class cannot be instantiated.

The module compiles fine and I’m even presented with autocomplete when I attempt to load the class using GDScript. Unfortunately Godot doesn’t provide any further information on why this can’t be instantiated, so I’m hoping someone here might have answers.

test.gdscript

extends Node

onready var ct = load("res://bin/gdexample.gdns").new();

func _ready():
    ct.test_print();
	return;

gdexample.h

#ifndef GDEXAMPLE_H
#define GDEXAMPLE_H

#include <Godot.hpp>
#include <PhysicsDirectSpaceState.hpp>
#include <PhysicsShapeQueryParameters.hpp>

namespace godot 
{

class GDExample : public PhysicsDirectSpaceState 
{
	GODOT_CLASS(GDExample, PhysicsDirectSpaceState)

public:
	static void _register_methods();

	GDExample();
	~GDExample();

	void _init(); // our initializer called by Godot

	void test_print();
};

}

#endif

gdexample.cpp

#include "gdexample.h"

using namespace godot;

void GDExample::_register_methods() 
{
	register_method("test_print", &GDExample::test_print);
}

GDExample::GDExample() 
{
	//
}

GDExample::~GDExample() 
{
	// 
}

void GDExample::_init() {
	//
}

void GDExample::test_print()
{
	Godot::print("I am (hopefully) working.");
}

For what it’s worth, I have also tried “preload” instead of “load” but it didn’t seem to make a difference. The path to the .gdns is obviously correct as Godot wouldn’t know to complain about PhysicsDirectSpaceState.

Any help would be greatly appreciated. Thank you.

:bust_in_silhouette: Reply From: sash-rc

As you can see, the problem is not about GDNative part, you can’t instantiate PhysicsDirectSpaceState (what’s said in error message) because it depends on PhyscsEngine and its BVH. You should get it from the world, which is acquired from Spatial :

Spatial* someNode; // ....
PhysicsDirectSpaceState* spaceState = someNode->get_world()->get_direct_space_state();