【问题标题】:Godot gdnative Rust access property defined in the editorGodot gdnative 编辑器中定义的 Rust 访问属性
【发布时间】:2021-12-01 06:57:36
【问题描述】:

为了了解引擎,我正在尝试一个非常简单的项目 - 使用编辑器和 gdnative Rust 绑定程序生成一个球体。 我正在尝试跟进使用 GDScript 的 this tutorial 并转换 Rust 的代码。

我无法弄清楚如何访问编辑器中定义的属性。 我已经阅读文档并在网上搜索了一个星期,但是有 一些让我无法理解的事情,我无法理解如何进行。

我想要做的是访问 ArrayMesh 类型的 mesh 属性,就像我在上面链接的教程中一样,并将我为顶点生成的数组附加到它 - 基本上将这些数组绑定到阵列网格。这是我的场景:

[gd_scene load_steps=4 format=2]

[ext_resource path="res://procedural_earth.gdnlib" type="GDNativeLibrary" id=1]

[sub_resource type="ArrayMesh" id=4]

[sub_resource type="NativeScript" id=3]
resource_name = "ProcEarth"
class_name = "ProcEarth"
library = ExtResource( 1 )

[node name="Earth" type="Spatial"]

[node name="Sphere" type="MeshInstance" parent="."]
mesh = SubResource( 4 )
script = SubResource( 3 )

[node name="Camera" type="Camera" parent="."]
transform = Transform( 0.572229, -0.327396, 0.751909, 0, 0.916856, 0.399217, -0.820094, -0.228443, 0.524651, 4.71648, 2.5, 3.45846 )
current = true

我感兴趣的 ArrayMesh 结构,在上面的场景中称为mesh,是名为“Sphere”的节点的一部分(为了清楚起见而提及)。

我有以下 Rust 代码:

#[derive(NativeClass)]
#[inherit(MeshInstance)]
#[register_with(register_properties)]
struct ProcEarth {
     // ...
}  

impl ProcEarth {

    // ...

   #[export]
   fn _ready(&mut self, owner: &MeshInstance) {

       let mut arr = VariantArray::new_shared();
       // ...
       let blend_shapes = VariantArray::new_shared();
       owner.add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr, blend_shapes, 0);
   }
}

但这不起作用,因为我得到的错误是:

no method named `add_surface_from_arrays` found for reference `&gdnative::gdnative_bindings::MeshInstance` in the current scope

method not found in `&gdnative::gdnative_bindings::MeshInstance`rustc(E0599)

有谁知道我如何在 Rust 代码中从编辑器访问该属性,以便正确设置我的 ArrayMesh?是否有任何教程、文章、视频可以说明这一点?

任何指针都受到高度赞赏,因为我目前陷入了这种技术性问题 并且无法进步我的学习。

我在 Linux 上使用带有 gdnative 0.9.3 的 Godot 版本 v3.4.stable.official。

【问题讨论】:

    标签: rust godot gdnative


    【解决方案1】:

    方法add_surface_from_arraysArrayMesh中定义。鉴于您遇到的错误,您正尝试在 MeshInstance 上调用它。

    我们可以通过源代码确认这一点,因为您收到owner: &MeshInstance 并且您正在调用owner.add_surface_from_arrays(…)

    通常您会创建一个ArrayMesh 并在其上调用add_surface_from_arrays,并传递一个包含顶点数据的数组。之后,您应该可以通过 ArrayMeshMeshInstance 上调用 set_mesh

    let mut am = ArrayMesh::new();
    let mut arr = VariantArray::new_shared();
    // …
    let blend_shapes = VariantArray::new_shared();
    am.add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr, blend_shapes, 0);
    owner.set_mesh(am.cast::<Mesh>());
    

    我相信您可以在MeshInstance 上拨打mesh 来检索它。请注意,它可以是 MeshArrayMeshPrimitiveMesh)或 nil。 方法mesh 被记录为返回Option&lt;Ref&lt;Mesh, Shared&gt;&gt;

    【讨论】:

    • 谢谢!这引导我朝着正确的方向前进。调用“mesh”是获取编辑器中定义的 ArrayMesh 实例的方法。
    猜你喜欢
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    相关资源
    最近更新 更多