最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - How can I cast my own custom godot ext class in to a node - Stack Overflow

programmeradmin2浏览0评论

I was trying to add some nodes in to a Node parent in godotext with rust, but I don't know how to do this casting:

use godot::{
    classes::{AnimatedSprite2D, IAnimatedSprite2D},
    prelude::*,
};

#[derive(GodotClass)]
#[class(base=AnimatedSprite2D)]
pub struct BackgroundTile {
    pub base: Base<AnimatedSprite2D>,
    pub position: Vector2,
}

#[godot_api]
pub impl IAnimatedSprite2D for BackgroundTile {
    fn init(base: Base<AnimatedSprite2D>) -> Self {
        return Self {
            base,
            position: Vector2::ZERO,
        };
    }

    fn ready(&mut self) {
        let actual_position = self.position;
        self.base_mut().set_global_position(actual_position);
    }
}

#[godot_api]
impl BackgroundTile {
    #[signal]
    fn on_create(position: Vector2);
}
use crate::background_scripts::background_tile::BackgroundTile;
use godot::classes::AnimatableBody2D;
use godot::classes::INode2D;
use godot::classes::Node2D;
use godot::prelude::*;

#[derive(GodotClass)]
#[class(base=Node2D)]
struct Background {
    base: Base<Node2D>,
}

#[godot_api]
impl INode2D for Background {
    fn init(base: Base<Node2D>) -> Self {
        return Self { base };
    }

    fn ready(&mut self) {
        for _index in 0..10 {
            let mut tile = BackgroundTile::new_alloc();
            self.base_mut().add_child(tile);
        }
    }
}

Issue line 22:

Diagnostics:
1. Argument of type `godot::prelude::Gd<BackgroundTile>` cannot be passed to an `impl AsObjectArg<Node>` parameter
   the trait bound `godot::prelude::Gd<BackgroundTile>: AsObjectArg<Node>` is not satisfied [E0277]
2. required by a bound introduced by this call [E0277]
3. consider borrowing here: `&` [E0277]

As you can see it says that the BackgroundTile, which implements already a AnimatedSprite2D can't be considered as a Node and I even tried with objects that are native of godot and the problem persisted, I found a tutorial that is like a year old which did the things I did for copying objects.

发布评论

评论列表(0)

  1. 暂无评论