Skip to content

Array Functions(new and append) #55

Closed
@thebigG

Description

@thebigG

Hi there,

In the spirit of starting a discussion first rather than just implementing(potentially the wrong thing), I'm sharing my progress in this issue.

I started using the port and realized that Arrays are not complete. So I took a stab at implementing a ::new function and this is what I got so far:

    pub fn new() -> Array {
        // For now, this unnit business seems to be required. But I'd like to study it more
        //and really understand what it does.
        let mut uninit = std::mem::MaybeUninit::<Array>::uninit();
        unsafe {
            let self_ptr = (*uninit.as_mut_ptr()).sys_mut();
            let ctor = sys::method_table().array_construct_default;
            ctor(self_ptr, std::ptr::null_mut());

            uninit.assume_init()
        }
    }

This seems to work from GdScript.

From Rust code I'm sticking to the following pattern(not sure if this is the best way of doing this):

let mut points = Array::new();
let v = Vector2Array::from(&points);

Ok, so we can create an empty array. So the next question is how do we append items to it? I thought that I might be able to do something like the following:

    pub fn append(&mut self, v: Vector2) -> () {
        unsafe {
            let class_name = StringName::from("PackedVector2Array");
            let method_name = StringName::from("append");
            let method_bind = {
                unsafe {
                    ::godot_ffi::get_interface()
                        .classdb_get_method_bind
                        .unwrap_unchecked()
                }
            }(
                class_name.string_sys(),
                method_name.string_sys(),
                4188891560i64,
            );
            let call_fn = {
                unsafe {
                    ::godot_ffi::get_interface()
                        .object_method_bind_ptrcall
                        .unwrap_unchecked()
                }
            };
            let args = [
                <Vector2 as sys::GodotFfi>::sys(&v),
            ];
            let args_ptr = args.as_ptr();
            <Vector2Array as sys::GodotFfi>::from_sys_init(|return_ptr| {
                call_fn(method_bind, self.sys() as GDNativeObjectPtr, args_ptr, return_ptr);
            });

        }
    }

But it looks like ClassDB does not have "non-Node" classes such as Arrays? So I can't really just reach out to the engine and have it "append" something for me.

Or am I wrong about that?
Is there a way to call the append/push_back functions from Rust at the moment? It seems like Rust GDNative did something similar with the godot_pool_vector2_array_append_array family of functions? But there might be something else going in the case of GDNative that I don't know since these are new APIs to me.

Hope this is clear enough.

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: duplicateThis issue or pull request already exists

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions