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

cant create constant array #1027

Closed
ogyrec-o opened this issue Jan 25, 2025 · 3 comments
Closed

cant create constant array #1027

ogyrec-o opened this issue Jan 25, 2025 · 3 comments
Labels
c: register Register classes, functions and other symbols to GDScript feature Adds functionality to the library status: upstream Depending on upstream fix (typically Godot) status: wontfix This will not be worked on

Comments

@ogyrec-o
Copy link

#[godot_api]
impl test
{
  #[constant]
  const dd: VariantArray = varray![42_i64, "hello", true];
}

err

error[E0277]: the trait bound `godot::prelude::Array<Variant>: Copy` is not satisfied   --> src\lib.rs:114:6
    |
113 |   #[godot_api]
    |   ------------ required by a bound introduced by this call
114 |   impl Chunk2
    |  ______^
115 | | {
116 | |   #[constant]
117 | |   const dd: VariantArray = varray![42_i64, "hello", true];
    | |__________^ the trait `Copy` is not implemented for `godot::prelude::Array<Variant>`
    |
error[E0015]: cannot call non-const fn `<bool as godot::prelude::ToGodot>::to_variant` in constants
   --> src\lib.rs:117:28
    |
117 |   const dd: VariantArray = varray![42_i64, "hello", true];
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: calls in constants are limited to constant functions, tuple structs and tuple variants
    = note: this error originates in the macro `varray` (in Nightly builds, run with -Z macro-backtrace for more info)
@miruji
Copy link

miruji commented Jan 25, 2025

This is a bug. The creation works with #[var], but does not work through #[constant]

@Bromeon
Copy link
Member

Bromeon commented Jan 25, 2025

GDExtension only allows to register integral constants:
https://github.com/godotengine/godot/blob/b15b24b087e792335d919fd83055f50f276fbe22/core/extension/gdextension_interface.h#L2918-L2935

So this isn't a bug, it's a feature that's not supported by the Godot engine. You could maybe open a proposal there.


Now, even if Godot did support it, we cannot use Rust const for values that aren't known at compile time (such as Array). So we'd need to change how to represent this in Rust code, as const in impl isn't suitable. Rough ideas:

  • use fields instead (not per-instance though)
  • abuse functions (similar to #[signal]), but that's also weird
  • find a wrapper that allows const-compatible expressions that can then be converted to Array etc at runtime.

Anyway, for now there's nothing we can do on godot-rust side, so I'm closing this. Let me know in case I missed a GDExtension API 🙂

@Bromeon Bromeon closed this as not planned Won't fix, can't repro, duplicate, stale Jan 25, 2025
@Bromeon Bromeon added feature Adds functionality to the library status: upstream Depending on upstream fix (typically Godot) c: register Register classes, functions and other symbols to GDScript status: wontfix This will not be worked on labels Jan 25, 2025
@ogyrec-o
Copy link
Author

ogyrec-o commented Jan 25, 2025

GDExtension only allows to register integral constants: https://github.com/godotengine/godot/blob/b15b24b087e792335d919fd83055f50f276fbe22/core/extension/gdextension_interface.h#L2918-L2935

So this isn't a bug, it's a feature that's not supported by the Godot engine. You could maybe open a proposal there.

Now, even if Godot did support it, we cannot use Rust const for values that aren't known at compile time (such as Array). So we'd need to change how to represent this in Rust code, as const in impl isn't suitable. Rough ideas:

  • use fields instead (not per-instance though)
  • abuse functions (similar to #[signal]), but that's also weird
  • find a wrapper that allows const-compatible expressions that can then be converted to Array etc at runtime.

Anyway, for now there's nothing we can do on godot-rust side, so I'm closing this. Let me know in case I missed a GDExtension API 🙂

godotengine/godot-proposals#11636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: register Register classes, functions and other symbols to GDScript feature Adds functionality to the library status: upstream Depending on upstream fix (typically Godot) status: wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants