GdScript2All icon indicating copy to clipboard operation
GdScript2All copied to clipboard

Error when compiling CPP methods

Open the-key-point opened this issue 11 months ago • 1 comments

I give another try for exploration. Also this time i learned a bit about rules of github.

I wrote player.gd scripted Node. It has a child Camera3D named "PlayerCam"

extends Node3D

var cam : Camera3D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	cam = get_node("PlayerCam")
	cam.current = true		
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	
	pass


#ifndef PLAYER_H
#define PLAYER_H

#include <godot_cpp/godot.hpp>
#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/dictionary.hpp>
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/node3d.hpp>

using namespace godot;

class player : public Node3D {
	GDCLASS(player, Node3D);
public:

protected:
	Ref<Camera3D> cam;

// Called when the node enters the scene tree for the first time.
// Called every frame. 'delta' is the elapsed time since the previous frame.

public:
	void _ready() override;

	void _process(double delta) override;

	static void _bind_methods();
};

#endif // PLAYER_H


#include "player.hpp"

#include <godot_cpp/core/object.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

void player::_ready()
{
	cam = get_node("PlayerCam");
	cam->set_current(true);

	// Replace with function body.

}

void player::_process(double delta)
{

}

void player::_bind_methods() {

}


But in terminal i get that error.

src/player.cpp: In member function 'virtual void player::_ready()':                                                                            
src/player.cpp:10:23: error: no matching function for call to 'player::get_node(const char [10])'                                              
   10 |         cam = get_node("PlayerCam");                                                                                                   
      |               ~~~~~~~~^~~~~~~~~~~~~                                                                                                    
In file included from godot-cpp/gen/include/godot_cpp/classes/node3d.hpp:38,                                                                   
                 from godot-cpp/gen/include/godot_cpp/classes/camera3d.hpp:36,                                                                 
                 from src/player.hpp:8,                                                                                                        
                 from src/player.cpp:2:                                                                                                        
godot-cpp/gen/include/godot_cpp/classes/node.hpp:349:12: note: candidate: 'template<class T> T* godot::Node::get_node(const godot::NodePath&) const'                                                                                                                                          
  349 |         T *get_node(const NodePath &p_path) const { return Object::cast_to<T>(get_node_internal(p_path)); }                            
      |            ^~~~~~~~                                                                                                                    
godot-cpp/gen/include/godot_cpp/classes/node.hpp:349:12: note:   template argument deduction/substitution failed:                              
src/player.cpp:10:23: note:   couldn't deduce template parameter 'T'                                                                           
   10 |         cam = get_node("PlayerCam");                                                                                                   
      |               ~~~~~~~~^~~~~~~~~~~~~                                                                                                    
In file included from godot-cpp/gen/include/godot_cpp/classes/node.hpp:41,                                                                     
                 from src/example.hpp:8,                                                                                                       
                 from src/register_types.cpp:5:                                                                                                
godot-cpp/include/godot_cpp/classes/ref.hpp: In instantiation of 'void godot::Ref<T>::unref() [with T = godot::Camera3D]':                     
godot-cpp/include/godot_cpp/classes/ref.hpp:217:3:   required from 'godot::Ref<T>::~Ref() [with T = godot::Camera3D]'                          
  217 |                 unref();                                                                                                               
      |                 ^~~~~                                                                                                                  
src/player.hpp:13:7:   required from here                                                                                                      
   13 | class player : public Node3D {                                                                                                         
      |       ^~~~~~                                                                                                                           
godot-cpp/include/godot_cpp/classes/ref.hpp:204:45: error: 'class godot::Camera3D' has no member named 'unreference'                           
  204 |                 if (reference && reference->unreference()) {                                                                           
      |                                  ~~~~~~~~~~~^~~~~~~~~~~                                                                                
In file included from godot-cpp/gen/include/godot_cpp/classes/node.hpp:41:                                                                     
godot-cpp/include/godot_cpp/classes/ref.hpp: In instantiation of 'void godot::Ref<T>::unref() [with T = godot::Camera3D]':                     
godot-cpp/include/godot_cpp/classes/ref.hpp:217:3:   required from 'godot::Ref<T>::~Ref() [with T = godot::Camera3D]'                          
  217 |                 unref();                                                                                                               
      |                 ^~~~~                                                                                                                  
src/player.hpp:13:7:   required from here                                                                                                      
   13 | class player : public Node3D {
      |       ^~~~~~
godot-cpp/include/godot_cpp/classes/ref.hpp:204:45: error: 'class godot::Camera3D' has no member named 'unreference'
  204 |                 if (reference && reference->unreference()) {
      |                                  ~~~~~~~~~~~^~~~~~~~~~~

I thought like maybe its because of get_node because that can be complicated due to path string value so modifying it to creating a new Camera3D object but still i got error.


#include "player.hpp"

#include <godot_cpp/core/object.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

void player::_ready()
{
	cam = Camera3D->new();
	cam->set_current(true);
	this->add_child(cam);

	// Replace with function body.

}

void player::_process(double delta)
{

}

void player::_bind_methods() {

}

src/player.cpp: In member function 'virtual void player::_ready()':
src/player.cpp:10:23: error: expected primary-expression before '->' token
   10 |         cam = Camera3D->new();
      |                       ^~
src/player.cpp:10:25: error: expected unqualified-id before 'new'
   10 |         cam = Camera3D->new();
      |                         ^~~
src/player.cpp:12:25: error: cannot convert 'godot::Ref<godot::Camera3D>' to 'godot::Node*'
   12 |         this->add_child(cam);
      |                         ^~~
      |                         |
      |                         godot::Ref<godot::Camera3D>
In file included from godot-cpp/gen/include/godot_cpp/classes/node3d.hpp:38,
                 from godot-cpp/gen/include/godot_cpp/classes/camera3d.hpp:36,
                 from src/player.hpp:8,
                 from src/player.cpp:2:
godot-cpp/gen/include/godot_cpp/classes/node.hpp:158:30: note:   initializing argument 1 of 'void godot::Node::add_child(godot::Node*, bool, InternalMode)'
  158 |         void add_child(Node *p_node, bool p_force_readable_name = false, Node::InternalMode p_internal = (Node::InternalMode)0);
      |                        ~~~~~~^~~~~~
In file included from godot-cpp/gen/include/godot_cpp/classes/node.hpp:41:
godot-cpp/include/godot_cpp/classes/ref.hpp: In instantiation of 'void godot::Ref<T>::unref() [with T = godot::Camera3D]':
godot-cpp/include/godot_cpp/classes/ref.hpp:217:3:   required from 'godot::Ref<T>::~Ref() [with T = godot::Camera3D]'
  217 |                 unref();
      |                 ^~~~~
src/player.hpp:13:7:   required from here
   13 | class player : public Node3D {
      |       ^~~~~~
godot-cpp/include/godot_cpp/classes/ref.hpp:204:45: error: 'class godot::Camera3D' has no member named 'unreference'
  204 |                 if (reference && reference->unreference()) {
      |                                  ~~~~~~~~~~~^~~~~~~~~~~
In file included from godot-cpp/gen/include/godot_cpp/classes/node.hpp:41,
                 from src/example.hpp:8,
                 from src/register_types.cpp:5:
godot-cpp/include/godot_cpp/classes/ref.hpp: In instantiation of 'void godot::Ref<T>::unref() [with T = godot::Camera3D]':
godot-cpp/include/godot_cpp/classes/ref.hpp:217:3:   required from 'godot::Ref<T>::~Ref() [with T = godot::Camera3D]'
  217 |                 unref();
      |                 ^~~~~
src/player.hpp:13:7:   required from here
   13 | class player : public Node3D {
      |       ^~~~~~
godot-cpp/include/godot_cpp/classes/ref.hpp:204:45: error: 'class godot::Camera3D' has no member named 'unreference'
  204 |                 if (reference && reference->unreference()) {
      |                                  ~~~~~~~~~~~^~~~~~~~~~~
scons: *** [src/player.os] Error 1

It feels like i am missing a basic simple step that is because i am pretty noob in c++..

the-key-point avatar Jan 25 '25 16:01 the-key-point

For the first error I think it should be get_node<Camera3D>(), the compiler says it can't find the template type (called 'T' in godot source code)

The second is because c++ creates new objects with the syntax new Camera3D()

I'll see if I can make the generate c++ take care of those nuances automagically

Lcbx avatar Jan 26 '25 13:01 Lcbx

closing, the get_node not having the type argument is a bit too complex to put in the transpiler, and the 'new' keyword would have to be handled by hand in cpp no matter what I think

Lcbx avatar Jun 09 '25 18:06 Lcbx