Skip to content
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

Dynamic children during init #637

Closed
Schr3da opened this issue Mar 2, 2024 · 5 comments
Closed

Dynamic children during init #637

Schr3da opened this issue Mar 2, 2024 · 5 comments
Labels
question Not a problem with the library, but a question regarding usage.

Comments

@Schr3da
Copy link

Schr3da commented Mar 2, 2024

Hey,

I'm trying to find out how to dynamically add children to my container during init of the editor so that they are also displayed by the editor initially but it seems not to work.

grid_init.txt

However it works if I move the code to ready and press Run Project.

grid_ready.txt

Based on error of the editor crash it seems that I can not access self.base_mut during init.
Any solution or workaround for that?

Thanks a lot

@Schr3da
Copy link
Author

Schr3da commented Mar 2, 2024

Please also find attached the dumb

dumb.txt

so the issue is related to self.base_mut().add_child(label.clone().upcast());

@Schr3da
Copy link
Author

Schr3da commented Mar 2, 2024

use godot::engine::{GridContainer, IGridContainer, Label};
use godot::obj::{NewAlloc, WithBaseField};
use godot::prelude::*;

#[derive(GodotClass)]
#[class(base=GridContainer)]
pub struct CustomGrid {
    base: Base<GridContainer>,
}

#[godot_api]
impl IGridContainer for CustomGrid {
    fn init(base: Base<GridContainer>) -> Self {
        let mut inner = Self { base };
        inner.sample(); //--> unable to call as otherwise it will panic because of self.base_mut()
        inner
    }
}

#[godot_api]
impl CustomGrid {
    pub fn sample(&mut self) {
        for _x in 0..10 {
            let mut label = Label::new_alloc();
            label.set_text("Hallo".to_godot());
            label.reset_size();
            self.base_mut().add_child(label.clone().upcast());
            label.set_owner(self.base().get_owner().unwrap());
        }

        self.base_mut().set_size(Vector2::new(100.0, 100.0));
    }
}

@Schr3da
Copy link
Author

Schr3da commented Mar 2, 2024

Initialize godot-rust (API v4.2.stable.official, runtime v4.2.1.stable.official)
Godot Engine v4.2.1.stable.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.277 - Forward Mobile - Using Vulkan Device #0: AMD - AMD Radeon Graphics (RADV VANGOGH)
 
WARNING: Blend file import is enabled in the project settings, but no Blender path is configured in the editor settings. Blend files will not be imported.
     at: _editor_init (modules/gltf/register_types.cpp:63)
thread '<unnamed>' panicked at /home/schreda/.cargo/git/checkouts/gdext-76630c89719e160c/17f0c9e/godot-core/src/obj/gd.rs:348:13:
downcast from GridContainer to CustomGrid failed; instance Gd { id: 1531189456529, class: GridContainer }
stack backtrace:
   0:     0x7f63671f2096 - std::backtrace_rs::backtrace::libunwind::trace::hbee8a7973eeb6c93
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f63671f2096 - std::backtrace_rs::backtrace::trace_unsynchronized::hc8ac75eea3aa6899
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f63671f2096 - std::sys_common::backtrace::_print_fmt::hc7f3e3b5298b1083
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f63671f2096 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hbb235daedd7c6190
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f6367215470 - core::fmt::rt::Argument::fmt::h76c38a80d925a410
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/rt.rs:142:9
   5:     0x7f6367215470 - core::fmt::write::h3ed6aeaa977c8e45
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f63671f040f - std::io::Write::write_fmt::h78b18af5775fedb5
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/io/mod.rs:1810:15
   7:     0x7f63671f1e74 - std::sys_common::backtrace::_print::h5d645a07e0fcfdbb
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f63671f1e74 - std::sys_common::backtrace::print::h85035a511aafe7a8
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f63671f3547 - std::panicking::default_hook::{{closure}}::hcce8cea212785a25
  10:     0x7f63671f32a9 - std::panicking::default_hook::hf5fcb0f213fe709a
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:292:9
  11:     0x7f63671f3b66 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hbc5ccf4eb663e1e5
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/boxed.rs:2029:9
  12:     0x7f63671f3b66 - std::panicking::rust_panic_with_hook::h095fccf1dc9379ee
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:783:13
  13:     0x7f63671f38b2 - std::panicking::begin_panic_handler::{{closure}}::h032ba12139b353db
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:657:13
  14:     0x7f63671f2596 - std::sys_common::backtrace::__rust_end_short_backtrace::h9259bc2ff8fd0f76
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:171:18
  15:     0x7f63671f3610 - rust_begin_unwind
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
  16:     0x7f6367010bc5 - core::panicking::panic_fmt::h784f20a50eaab275
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
  17:     0x7f63670148bc - godot_core::obj::gd::Gd<T>::cast::{{closure}}::h2b0170befbe5f0dd
                               at /home/schreda/.cargo/git/checkouts/gdext-76630c89719e160c/17f0c9e/godot-core/src/obj/gd.rs:348:13
  18:     0x7f636701384f - core::result::Result<T,E>::unwrap_or_else::h3553d5f82e9a6652
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1426:23
  19:     0x7f636701468c - godot_core::obj::gd::Gd<T>::cast::h0f1967635b423e58
                               at /home/schreda/.cargo/git/checkouts/gdext-76630c89719e160c/17f0c9e/godot-core/src/obj/gd.rs:347:9
  20:     0x7f6367011c2a - <core::grid::CustomGrid as godot_core::obj::traits::WithBaseField>::to_gd::h4429a7c927de9b8e
                               at /home/schreda/Documents/game-project/core/src/grid.rs:5:10
  21:     0x7f63670123f7 - godot_core::obj::traits::WithBaseField::base_mut::h89425a6739e2cd70
                               at /home/schreda/.cargo/git/checkouts/gdext-76630c89719e160c/17f0c9e/godot-core/src/obj/traits.rs:343:18
  22:     0x7f63670120b8 - core::grid::CustomGrid::sample::hdd9a374d17b2bd53
                               at /home/schreda/Documents/game-project/core/src/grid.rs:27:13
  23:     0x7f6367011cbb - <core::grid::CustomGrid as godot_core::gen::classes::grid_container::re_export::IGridContainer>::init::hbe3bb6a12a64eaf1
                               at /home/schreda/Documents/game-project/core/src/grid.rs:15:9
  24:     0x7f6367011cfd - <core::grid::CustomGrid as godot_core::obj::traits::cap::GodotDefault>::__godot_user_init::ha38c5a1c6a0a2923
                               at /home/schreda/Documents/game-project/core/src/grid.rs:11:1
  25:     0x7f636701c819 - core::ops::function::FnOnce::call_once::h8a7e5f7bcb9dd8be
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
  26:     0x7f63670174b9 - godot_core::registry::callbacks::create_rust_part_for_existing_godot_part::h9ce33b0d5af5dc97
                               at /home/schreda/.cargo/git/checkouts/gdext-76630c89719e160c/17f0c9e/godot-core/src/registry/callbacks.rs:68:25
  27:     0x7f63670172bd - godot_core::registry::callbacks::create_custom::h6cecf915cd870c90
                               at /home/schreda/.cargo/git/checkouts/gdext-76630c89719e160c/17f0c9e/godot-core/src/registry/callbacks.rs:44:5
  28:     0x7f6367017d1a - godot_core::registry::callbacks::create::h97deafb990ce7de6
                               at /home/schreda/.cargo/git/checkouts/gdext-76630c89719e160c/17f0c9e/godot-core/src/registry/callbacks.rs:24:5
  29:          0x3b434c3 - <unknown>
  30:          0x3ba916c - <unknown>
  31:          0x1125da3 - <unknown>
  32:          0x1157d89 - <unknown>
  33:          0x401d4cd - <unknown>
  34:          0x3b8f5dc - <unknown>
  35:          0x20ebd43 - <unknown>
  36:           0x4f8c3e - <unknown>
  37:           0x41ed28 - <unknown>
  38:     0x7f637ac5914a - __libc_start_call_main
  39:     0x7f637ac5920b - __libc_start_main_impl
  40:           0x42aa6a - <unknown>
  41:                0x0 - <unknown>
fatal runtime error: failed to initiate panic, error 5
Aborted (core dumped)

@StatisMike
Copy link
Contributor

I feel like the main question is - is it a general pattern in Godot, in regards to GDScript classes?

The init function is called repeatedly in the editor, and it could potentially lead to massive recursive proliferation of nodes (dynamically added child has children that also have dynamically added children). I think it would be better to, eg. create some builder to generate a dynamically created Scene (for usage in-editor) or just a Node with children (for usage on runtime).

That said, there are already some development about giving access to the Base in init phase #557

@Schr3da
Copy link
Author

Schr3da commented Mar 2, 2024

thanks for the help and clarification

@Schr3da Schr3da closed this as completed Mar 2, 2024
@Schr3da Schr3da closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2024
@Bromeon Bromeon added the question Not a problem with the library, but a question regarding usage. label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a problem with the library, but a question regarding usage.
Projects
None yet
Development

No branches or pull requests

3 participants