Need ResourceLoader::get_singleton(), but am unsure of how to use it.
Asked Answered
C

2

0

I know I need to ResourceLoader::get_singleton(), but I don't know what files to #include, and I'm not keen with using the ResourceLoader. I've got an ArrayMesh saved as "res://Assets/HexMesh.tres" that I need to set_mesh() on a MeshInstance3D.
these are the variables I need to set up:

private:
	ArrayMesh* mesh;
	MeshInstance3D* meshInstance;

here's the source for the HexCell:

#ifndef HEXCELL_H
#define HEXCELL_H

#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/array_mesh.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>

namespace godot {

class HexCell : public Node3D {
	GDCLASS(HexCell, Node3D)

protected:
	static void _bind_methods() {
		ClassDB::bind_method(D_METHOD("get_r"), &HexCell::get_r);
		ClassDB::bind_method(D_METHOD("set_r", "pR"), &HexCell::set_r);
		ADD_PROPERTY(PropertyInfo(Variant::INT, "r"), "set_r", "get_r");

		ClassDB::bind_method(D_METHOD("get_q"), &HexCell::get_q);
		ClassDB::bind_method(D_METHOD("set_q", "pQ"), &HexCell::set_q);
		ADD_PROPERTY(PropertyInfo(Variant::INT, "q"), "set_q", "get_q");
		
		ClassDB::bind_method(D_METHOD("get_hexSize"), &HexCell::get_hexSize);
		ClassDB::bind_method(D_METHOD("set_hexSize", "p_hexSize"), &HexCell::set_hexSize);
		ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "r"), "set_hexSize", "get_hexSize");
}

private:
	ArrayMesh* mesh;
	MeshInstance3D* meshInstance;

	float hexSize { 1.0 };
	int q;
	int r;

	Vector3 axialToCube(Vector2 axial) {
		float x = axial.x;
		float z = axial.y;
		float y = -x - z;
		return Vector3(x, y, z);
	}

	Vector3 cubeToWorld(Vector3 cube) {
		float x = hexSize * sqrt(3) * (cube.x + cube.z / 2);
		float y = 0; 
		float z = hexSize * 3/2 * cube.z;
		return Vector3(x, y, z);
	}

public:
	HexCell() {}
	~HexCell() {}

	void _ready() {
		meshInstance->set_mesh(mesh);
	}

	void setMesh(ArrayMesh* p_mesh) { mesh = p_mesh; }
	ArrayMesh* getMesh() { return mesh; }

	void set_hexSize(float p_hexSize) { hexSize = p_hexSize; }
	int get_hexSize() { return hexSize; }

	void set_q(int pQ) { q = pQ; }
	int get_q() { return q; }

	void set_r(int pR) { r = pR; }
	int get_r() { return r; }

	void placeHex() {
		Vector2 axial = Vector2(q, r);
		Vector3 cube = axialToCube(axial);
		Vector3 worldPosition = cubeToWorld(cube);
		set_global_transform(Transform3D(Basis(), worldPosition));
	}
	
};

}

#endif // HEXCELL_H
Candlepin answered 16/10, 2023 at 20:17 Comment(0)
C
0
private:
	ResourceLoader* loader = ResourceLoader::get_singleton();
	ArrayMesh* mesh = loader->load("res://Assets/HexMesh.tres");
	MeshInstance3D* meshInstance;
src/hexcell.h:31:39: error: cannot convert 'godot::Ref<godot::Resource>' to 'godot::ArrayMesh*' in initialization
   31 |         ArrayMesh* mesh = loader->load("res://Assets/HexMesh.tres");
      |                           ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                       |
      |                                       godot::Ref<godot::Resource>
In file included from src/hexmap.h:4,
                 from src/register_types.cpp:3:
src/hexcell.h:31:39: error: cannot convert 'godot::Ref<godot::Resource>' to 'godot::ArrayMesh*' in initialization
   31 |         ArrayMesh* mesh = loader->load("res://Assets/HexMesh.tres");
      |                           ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                       |
      |                                       godot::Ref<godot::Resource>
Candlepin answered 16/10, 2023 at 20:50 Comment(0)
D
0

Candlepin I already mentioned you'll need to look into Godot's source to figure things out 😉

If you lookup the return type of ResourceLoader::load() you'll se that it returns template type Ref<Resource>. This is also apparent from the error message you've got. If you next look into core/object/ref_counted.h you'll see you can get the referenced object pointer via Ref<T>::ptr(). Since template parameter is base class Resource, for quick and dirty test you can simply dynamic_cast that pointer into your derived type pointer, which is ArrayMesh* in this case.

Drawtube answered 16/10, 2023 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.