Skip to content

Emit POSTINITIALIZE notification after init() #1211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions godot-core/src/builtin/string/gstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ impl From<&str> for GString {

unsafe {
Self::new_with_string_uninit(|string_ptr| {
#[cfg(before_api = "4.3")]
let ctor = interface_fn!(string_new_with_utf8_chars_and_len);
#[cfg(since_api = "4.3")]
let ctor = interface_fn!(string_new_with_utf8_chars_and_len2);

ctor(
string_ptr,
bytes.as_ptr() as *const std::ffi::c_char,
Expand Down
6 changes: 1 addition & 5 deletions godot-core/src/classes/class_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ pub(crate) fn construct_engine_object<T>() -> Gd<T>
where
T: GodotClass + Bounds<Declarer = bounds::DeclEngine>,
{
// SAFETY: adhere to Godot API; valid class name and returned pointer is an object.
unsafe {
let object_ptr = sys::interface_fn!(classdb_construct_object)(T::class_name().string_sys());
Gd::from_obj_sys(object_ptr)
}
Gd::new_alloc_postinited()
}

pub(crate) fn ensure_object_alive(
Expand Down
6 changes: 1 addition & 5 deletions godot-core/src/obj/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,7 @@ impl Declarer for DeclEngine {
where
T: GodotDefault + Bounds<Declarer = Self>,
{
unsafe {
let object_ptr =
sys::interface_fn!(classdb_construct_object)(T::class_name().string_sys());
Gd::from_obj_sys(object_ptr)
}
Gd::new_alloc_postinited()
}
}

Expand Down
18 changes: 18 additions & 0 deletions godot-core/src/obj/gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,24 @@ impl<T: GodotClass> Gd<T> {
// Do not increment ref-count; assumed to be return value from FFI.
sys::ptr_then(object_ptr, |ptr| Gd::from_obj_sys_weak(ptr))
}

pub(crate) fn new_alloc_postinited() -> Self {
#[cfg(before_api = "4.4")]
unsafe {
let object_ptr = sys::classdb_construct_object(T::class_name().string_sys());
Gd::from_obj_sys(object_ptr)
}
#[cfg(since_api = "4.4")]
unsafe {
let object_ptr =
sys::classdb_construct_object_no_postinit(T::class_name().string_sys());
let obj = Gd::<T>::from_obj_sys(object_ptr);
obj.clone()
.upcast_object()
.notify(crate::classes::notify::ObjectNotification::POSTINITIALIZE);
obj
}
}
}

/// _The methods in this impl block are only available for objects `T` that are manually managed,
Expand Down
15 changes: 13 additions & 2 deletions godot-core/src/registry/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ pub unsafe extern "C" fn create<T: cap::GodotDefault>(
_class_userdata: *mut std::ffi::c_void,
_notify_postinitialize: sys::GDExtensionBool,
) -> sys::GDExtensionObjectPtr {
create_custom(T::__godot_user_init).unwrap_or(std::ptr::null_mut())
if let Ok(object_ptr) = create_custom(T::__godot_user_init) {
let mut obj = Gd::<T>::from_obj_sys_weak(object_ptr).upcast_object();
obj.notify(crate::classes::notify::ObjectNotification::POSTINITIALIZE);
std::mem::forget(obj);
object_ptr
} else {
std::ptr::null_mut()
}
}

#[cfg(before_api = "4.4")]
Expand Down Expand Up @@ -92,7 +99,11 @@ where
F: FnOnce(Base<T::Base>) -> T,
{
let base_class_name = T::Base::class_name();
let base_ptr = unsafe { interface_fn!(classdb_construct_object)(base_class_name.string_sys()) };
#[cfg(before_api = "4.4")]
let base_ptr = unsafe { sys::classdb_construct_object(base_class_name.string_sys()) };
#[cfg(since_api = "4.4")]
let base_ptr =
unsafe { sys::classdb_construct_object_no_postinit(base_class_name.string_sys()) };

match create_rust_part_for_existing_godot_part(make_user_instance, base_ptr) {
Ok(_extension_ptr) => Ok(base_ptr),
Expand Down
18 changes: 18 additions & 0 deletions godot-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,24 @@ pub fn is_main_thread() -> bool {
std::thread::current().id() == main_thread_id()
}

/// # Safety
/// `class_name` is assumed to be valid.
#[cfg(before_api = "4.4")]
pub unsafe fn classdb_construct_object(
class_name: GDExtensionConstStringNamePtr,
) -> GDExtensionObjectPtr {
interface_fn!(classdb_construct_object)(class_name)
}

/// # Safety
/// `class_name` is assumed to be valid.
#[cfg(since_api = "4.4")]
pub unsafe fn classdb_construct_object_no_postinit(
class_name: GDExtensionConstStringNamePtr,
) -> GDExtensionObjectPtr {
interface_fn!(classdb_construct_object2)(class_name)
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Macros to access low-level function bindings

Expand Down
2 changes: 2 additions & 0 deletions itest/rust/src/object_tests/virtual_methods_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ fn test_notifications() {
assert_eq!(
obj.bind().sequence,
vec![
#[cfg(since_api = "4.4")]
ReceivedEvent::Notification(NodeNotification::POSTINITIALIZE),
ReceivedEvent::Notification(NodeNotification::UNPAUSED),
ReceivedEvent::Notification(NodeNotification::EDITOR_POST_SAVE),
ReceivedEvent::Ready,
Expand Down
Loading