Godot Version
4.1.4
Issue
I am trying to create a GDExtension Node which internally works with a String that can be modified with an added property from the editor. While other extensions work fine, my current extension does not even compile when binding only a simple getter.
I tried using the types String and StringName as in the end I want to define an input action. Both types caused the same compile error which you can see below. When commenting out all method bindings, the code compiles.
Question
Does anyone know how String and StringName work for GDExtensions and how they can be used in the code below?
Compile Error
scons -Q
cl /Fosrc\DefaultExit.obj /c src\DefaultExit.cpp /TP /std:c++17 /EHsc /nologo “/IC:\GodotExtensions\godot-cpp\gdextension” “/IC:\GodotExtensions\godot-cpp\include” “/IC:\GodotExtensions\godot-cpp\gen\include” “/IC:\GodotExtensions\godot-cpp\gen\include\core” /Isrc
DefaultExit.cpp
C:\GodotExtensions\godot-cpp\include\godot_cpp/core/method_bind.hpp(591): error C2440:
“reinterpret_cast”: “R (__cdecl godot::DefaultExit::* )(void) const” cannot be converted to “R (__cdecl godot::_gde_UnexistingClass::* )(void) const”
with
[
R=godot::StringName
]
C:\GodotExtensions\godot-cpp\include\godot_cpp/core/method_bind.hpp(591): note: Pointers to elements have different representations; conversion not possible
C:\GodotExtensions\godot-cpp\include\godot_cpp/core/method_bind.hpp(591): note: The template instantiation context (oldest first) is
src\DefaultExit.cpp(12): note: See reference to the currently compiled instantiation “godot::MethodBind godot::ClassDB::bind_method<godot::MethodDefinition,godot::StringName(__cdecl godot::DefaultExit:: )(void) const,>(N,M)” of the function template.
with
[
N=godot::MethodDefinition,
M=godot::StringName (__cdecl godot::DefaultExit::* )(void) const
]
C:\GodotExtensions\godot-cpp\include\godot_cpp/core/class_db.hpp(241): note: See reference to the currently compiled instantiation “godot::MethodBind godot::create_method_bindgodot::DefaultExit,godot::StringName,(R (__cdecl godot::DefaultExit:: )(void) const)” of the function template.
with
[
R=godot::StringName
]
C:\GodotExtensions\godot-cpp\include\godot_cpp/core/method_bind.hpp(591): error C2512:
“godot::MethodBindTRC::MethodBindTRC”: No suitable default constructor available
with
[
R=godot::StringName
]
C:\GodotExtensions\godot-cpp\include\godot_cpp/core/method_bind.hpp(591): note: when adjusting the argument list “()”
scons: *** [src\DefaultExit.obj] Error 2
Code
DefaultExit.h
#ifndef DEFAULTEXIT
#define DEFAULTEXIT
#include <godot_cpp/classes/node.hpp>
#include <godot_cpp/classes/input_event.hpp>
#include <godot_cpp/variant/string_name.hpp>
namespace godot{
class DefaultExit : public Node{
GDCLASS(DefaultExit, Node)
private:
StringName exit_string;
protected:
static void _bind_methods();
public:
DefaultExit();
~DefaultExit();
StringName get_ExitString() const;
void set_ExitString(const StringName strg);
void _input(const Ref<InputEvent> &event);
};
}
#endif
DefaultExit.cpp
#include "DefaultExit.h"
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
using namespace godot;
void DefaultExit::_bind_methods(){
ClassDB::bind_method(D_METHOD("get_exit_string"), &DefaultExit::get_ExitString);
//ClassDB::bind_method(D_METHOD("set_exit_string", "exit_string"), &DefaultExit::set_ExitString);
//ClassDB::add_property("DefaultExit",PropertyInfo(Variant::STRING, "Exit Action"), "set_exit_string", "get_exit_string");
}
DefaultExit::DefaultExit(){
this->exit_string = "exit";
}
DefaultExit::~DefaultExit(){
}
StringName DefaultExit::get_ExitString() const{
return exit_string;
}
void DefaultExit::set_ExitString(const StringName strg){
this->exit_string = strg;
}
void DefaultExit::_input(const Ref<InputEvent> &event){
if (!Engine::get_singleton()->is_editor_hint()) {
if (event->is_action_pressed(exit_string)) {
get_tree()->quit();
}
}
}