Unable/not sure how to deal with Textures and Images in c++

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

I just started trying to rewrite some of my code in C++ to make it more performant, but I’m running into some trouble using Godot’s internal types within c++ due to the lack of documentation. I followed the GDNative C++ example on on Github and I’m trying to write a function the will take in a texture and convert it to an array of floats. But right now I’m just trying to figure out how to even call Texture.get_data().

My project structure is the same as the example, but my class is called Cubifier rather than GDTest

here is my cubifier.cpp:

#include "cubifier.hpp"


using namespace godot;

void Cubifier::_register_methods() {
	register_method("set_heightmap", &Cubifier::set_heightmap);
	register_method("get_heightmap", &Cubifier::get_heightmap);
}

void Cubifier::_init() {
}

void Cubifier::set_heightmap(Ref<Texture> new_heightmap) {
	_heightmap = new_heightmap->get_data();
}

Ref<Image> Cubifier::get_heightmap() const{
	return _heightmap;
}

and my cubifier.hpp:

#ifndef Cubifier_HPP
#define Cubifier_HPP

#include <Godot.hpp>
#include <Node2D.hpp>
#include <Image.hpp>
#include <Texture.hpp>

namespace godot {

class Cubifier : public Node2D {
	GODOT_CLASS(Cubifier, Node2D)

private:
	Ref<Image> _heightmap;

public:
	static void _register_methods();

	void _init();

	void set_heightmap(Ref<Texture> new_heightmap);
	Ref<Image> get_heightmap() const;
};

}

#endif /* !Cubifier_HPP */

When I run SCons, everything builds fine but when I try to run my project in Godot, I immediately get:

 drivers/unix/net_socket_posix.cpp:190 - Socket error: 10054

Here is where I call the functions in godot

var test = load("res://bin/cubifier.gdns").new()
test.set_heightmap(height_map_img)
test.get_heightmap()

where height_map_img is just an image that works perfectly fine everywhere else

In your gdscript, are you passing an image to test.set_heightmap when it is expecting a texture?

Bernard Cloutier | 2020-01-28 16:28