godot_core/classes/
type_safe_replacements.rs

1/*
2 * Copyright (c) godot-rust; Bromeon and contributors.
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6 */
7
8//! Replaces existing Godot APIs with more type-safe ones where appropriate.
9//!
10//! Each entry here *must* be accompanied by
11//!
12//! See also sister module [super::manual_extensions].
13
14use crate::builtin::{Callable, GString, StringName, Variant};
15use crate::classes::notify::NodeNotification;
16use crate::classes::object::ConnectFlags;
17use crate::classes::scene_tree::GroupCallFlags;
18use crate::classes::{Object, SceneTree, Script};
19use crate::global::Error;
20use crate::meta::{arg_into_ref, AsArg, ToGodot};
21use crate::obj::{EngineBitfield, Gd};
22
23impl Object {
24    pub fn get_script(&self) -> Option<Gd<Script>> {
25        let variant = self.raw_get_script();
26        if variant.is_nil() {
27            None
28        } else {
29            Some(variant.to())
30        }
31    }
32
33    pub fn set_script(&mut self, script: impl AsArg<Option<Gd<Script>>>) {
34        arg_into_ref!(script);
35
36        self.raw_set_script(&script.to_variant());
37    }
38
39    pub fn connect(&mut self, signal: impl AsArg<StringName>, callable: &Callable) -> Error {
40        self.raw_connect(signal, callable)
41    }
42
43    pub fn connect_flags(
44        &mut self,
45        signal: impl AsArg<StringName>,
46        callable: &Callable,
47        flags: ConnectFlags,
48    ) -> Error {
49        self.raw_connect_ex(signal, callable)
50            .flags(flags.ord() as u32)
51            .done()
52    }
53}
54
55impl SceneTree {
56    // Note: this creates different order between call_group(), call_group_flags() in docs.
57    // Maybe worth redeclaring those as well?
58
59    pub fn call_group_flags(
60        &mut self,
61        flags: GroupCallFlags,
62        group: impl AsArg<StringName>,
63        method: impl AsArg<StringName>,
64        varargs: &[Variant],
65    ) {
66        self.raw_call_group_flags(flags.ord() as i64, group, method, varargs)
67    }
68
69    pub fn set_group_flags(
70        &mut self,
71        call_flags: GroupCallFlags,
72        group: impl AsArg<StringName>,
73        property: impl AsArg<GString>,
74        value: &Variant,
75    ) {
76        self.raw_set_group_flags(call_flags.ord() as u32, group, property, value)
77    }
78
79    /// Assumes notifications of `Node`. To relay those of derived constants, use [`NodeNotification::Unknown`].
80    pub fn notify_group(&mut self, group: impl AsArg<StringName>, notification: NodeNotification) {
81        self.raw_notify_group(group, notification.into())
82    }
83
84    /// Assumes notifications of `Node`. To relay those of derived constants, use [`NodeNotification::Unknown`].
85    pub fn notify_group_flags(
86        &mut self,
87        call_flags: GroupCallFlags,
88        group: impl AsArg<StringName>,
89        notification: NodeNotification,
90    ) {
91        self.raw_notify_group_flags(call_flags.ord() as u32, group, notification.into())
92    }
93}
94
95#[cfg(feature = "codegen-full")] #[cfg_attr(published_docs, doc(cfg(feature = "codegen-full")))]
96mod codegen_full {
97    // For future, expanding manual replacements for classes in the codegen-full set.
98}