Skip to content

feat: ✨ Dynamic Script Components, register_new_component binding, remove_component no longer requires ReflectComponent data #379

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

Merged
merged 12 commits into from
Mar 21, 2025

Conversation

makspll
Copy link
Owner

@makspll makspll commented Mar 20, 2025

Summary

In order to support dynamic script components we need:

  • to relax some requirements on having a ReflectComponent registration to allow component operations
  • add a script component registry, which stores metadata about each dynamically registered component
  • Add ScriptValue to into_script_ref and from_script_ref implementations to allow any value to be inserted into the components payload

The nice thing about this implementation is it just works with the reference system.

The current problems are that the global type cache will not reflect this script component, to fix that we'd need a mechanism for "re-computing" certain globals for scripts. This also does not allow setting a schema on these, which is handy but also footgunny

As a side effect I've also made remove_component no longer require the ReflectComponent registration

@makspll makspll requested a review from Copilot March 20, 2025 21:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces dynamic script component support by adding a new script component registry and associated binding functions, as well as enhancing world access methods to accommodate dynamic components.

  • Introduces dynamic script component registration and new binding function register_new_component.
  • Updates WorldAccessGuard and query insertion methods to handle dynamic script components.
  • Enhances logging and type registration methods to better report errors and support new component types.

Reviewed Changes

Copilot reviewed 11 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/bevy_mod_scripting_core/src/bindings/script_component.rs Added dynamic script component registration, new ScriptComponentRegistry, and corresponding tests.
crates/bevy_mod_scripting_core/src/bindings/query.rs Extended ScriptComponentRegistration to support dynamic insertion and adjusted insertion logic for dynamic components.
crates/bevy_mod_scripting_core/src/bindings/world.rs Updated WorldAccessGuard to include component registry handling and refined type registration lookups.
crates/bevy_mod_scripting_functions/src/core.rs Added the register_new_component binding and minor error message improvements.
crates/bevy_mod_scripting_core/src/lib.rs Modified plugin initialization to load the DynamicScriptComponentPlugin and added logging during initialization.
Other files Miscellaneous adjustments to support dynamic script components and type conversions.
Files not reviewed (3)
  • assets/tests/register_new_component/new_component_can_be_retrieved.lua: Language not supported
  • assets/tests/register_new_component/new_component_can_be_set.lua: Language not supported
  • crates/bevy_mod_scripting_core/src/bindings/mod.rs: Language not supported
Comments suppressed due to low confidence (1)

crates/bevy_mod_scripting_core/src/bindings/query.rs:154

  • The use of Box::leak is intentional to avoid double dropping, but it bypasses Rust's ownership management. Consider adding explicit documentation or an abstraction to ensure that this design does not inadvertently cause memory leaks in production code.
let ptr = (Box::leak(cast) as *mut ScriptComponent).cast();

@makspll makspll changed the title feat: Dynamic Script Components & register_new_component binding feat: Dynamic Script Components, register_new_component binding, remove_component no longer requires ReflectComponent data Mar 20, 2025
@makspll makspll changed the title feat: Dynamic Script Components, register_new_component binding, remove_component no longer requires ReflectComponent data feat: ✨ Dynamic Script Components, register_new_component binding, remove_component no longer requires ReflectComponent data Mar 20, 2025
@makspll makspll requested a review from Copilot March 20, 2025 22:49
@makspll makspll enabled auto-merge (squash) March 20, 2025 22:54
@makspll makspll disabled auto-merge March 21, 2025 08:50
@makspll makspll merged commit a6a989c into main Mar 21, 2025
16 checks passed
@makspll makspll deleted the feat/dynamic-components branch March 21, 2025 08:50
This was referenced Mar 21, 2025
makspll added a commit that referenced this pull request Mar 29, 2025
## 🤖 New release

* `bevy_mod_scripting_derive`: 0.10.0 -> 0.11.0
* `bevy_mod_scripting_core`: 0.10.0 -> 0.11.0 (✓ API compatible changes)
* `bevy_mod_scripting_lua`: 0.10.0 -> 0.11.0 (✓ API compatible changes)
* `bevy_mod_scripting_rhai`: 0.10.0 -> 0.11.0 (✓ API compatible changes)
* `bevy_mod_scripting_functions`: 0.10.0 -> 0.11.0 (✓ API compatible
changes)
* `ladfile`: 0.4.0 -> 0.5.0 (⚠ API breaking changes)
* `mdbook_lad_preprocessor`: 0.1.4 -> 0.1.5 (✓ API compatible changes)
* `ladfile_builder`: 0.2.6 -> 0.3.0 (⚠ API breaking changes)
* `bevy_mod_scripting`: 0.10.0 -> 0.11.0 (✓ API compatible changes)

### ⚠ `ladfile` breaking changes

```text
--- failure constructible_struct_adds_field: externally-constructible struct adds field ---

Description:
A pub struct constructible with a struct literal has a new pub field. Existing struct literals must be updated to include the new field.
        ref: https://doc.rust-lang.org/reference/expressions/struct-expr.html
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/constructible_struct_adds_field.ron

Failed in:
  field LadType.generated in /tmp/.tmpI1fySQ/bevy_mod_scripting/crates/ladfile/src/lib.rs:458
  field LadType.insignificance in /tmp/.tmpI1fySQ/bevy_mod_scripting/crates/ladfile/src/lib.rs:465

--- failure method_parameter_count_changed: pub method parameter count changed ---

Description:
A publicly-visible method now takes a different number of parameters.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#fn-change-arity
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/method_parameter_count_changed.ron

Failed in:
  ladfile::LadFile::get_type_identifier now takes 3 parameters instead of 2, in /tmp/.tmpI1fySQ/bevy_mod_scripting/crates/ladfile/src/lib.rs:55
```

### ⚠ `ladfile_builder` breaking changes

```text
--- failure constructible_struct_adds_field: externally-constructible struct adds field ---

Description:
A pub struct constructible with a struct literal has a new pub field. Existing struct literals must be updated to include the new field.
        ref: https://doc.rust-lang.org/reference/expressions/struct-expr.html
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/constructible_struct_adds_field.ron

Failed in:
  field LadFileSettings.exclude_types_containing_unregistered in /tmp/.tmpI1fySQ/bevy_mod_scripting/crates/ladfile_builder/src/plugin.rs:42

--- failure method_parameter_count_changed: pub method parameter count changed ---

Description:
A publicly-visible method now takes a different number of parameters.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#fn-change-arity
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/method_parameter_count_changed.ron

Failed in:
  ladfile_builder::plugin::ScriptingDocgenPlugin::new now takes 4 parameters instead of 3, in /tmp/.tmpI1fySQ/bevy_mod_scripting/crates/ladfile_builder/src/plugin.rs:61
```

<details><summary><i><b>Changelog</b></i></summary><p>

## `bevy_mod_scripting_derive`

<blockquote>

##
[0.11.0](bevy_mod_scripting_derive-v0.10.0...bevy_mod_scripting_derive-v0.11.0)
- 2025-03-29

### Added

- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))
</blockquote>

## `bevy_mod_scripting_core`

<blockquote>

##
[0.11.0](bevy_mod_scripting_core-v0.10.0...bevy_mod_scripting_core-v0.11.0)
- 2025-03-29

### Added

- optimize access map
([#395](#395))
- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- improve tracing spans, add `profile_with_tracy` feature flag
([#394](#394))
- add `profile_with_tracy` feature which plays nicely with bevy's
`bevy/trace_tracy` feature
([#393](#393))
- Add initial benchmarks, integrate them into CI & add getters/settters
for `Scripts` resource
([#381](#381))
- add ScriptValue override for printing opaque values
([#380](#380))
- ✨ Dynamic Script Components, `register_new_component`
binding, `remove_component` no longer requires `ReflectComponent` data
([#379](#379))
- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))

### Fixed

- fix global type cache not containing generic types
([#388](#388))

### Other

- switch to hashbrown hashmap in the function registry
([#399](#399))
- try play with hashing for access maps
([#398](#398))
- allow check creation for bencher
</blockquote>

## `bevy_mod_scripting_lua`

<blockquote>

##
[0.11.0](bevy_mod_scripting_lua-v0.10.0...bevy_mod_scripting_lua-v0.11.0)
- 2025-03-29

### Added

- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- allow the conversion of lua functions into `ScriptValue` via
`DynamicScriptFunction`
([#396](#396))
- Add initial benchmarks, integrate them into CI & add getters/settters
for `Scripts` resource
([#381](#381))
- ✨ Dynamic Script Components, `register_new_component`
binding, `remove_component` no longer requires `ReflectComponent` data
([#379](#379))
</blockquote>

## `bevy_mod_scripting_rhai`

<blockquote>

##
[0.11.0](bevy_mod_scripting_rhai-v0.10.0...bevy_mod_scripting_rhai-v0.11.0)
- 2025-03-29

### Added

- [**breaking**] bump bersion
- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- Add initial benchmarks, integrate them into CI & add getters/settters
for `Scripts` resource
([#381](#381))
</blockquote>

## `bevy_mod_scripting_functions`

<blockquote>

##
[0.11.0](bevy_mod_scripting_functions-v0.10.0...bevy_mod_scripting_functions-v0.11.0)
- 2025-03-29

### Added

- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- ✨ Dynamic Script Components, `register_new_component`
binding, `remove_component` no longer requires `ReflectComponent` data
([#379](#379))
- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))
</blockquote>

## `ladfile`

<blockquote>

##
[0.5.0](v0.4.0-ladfile...v0.5.0-ladfile)
- 2025-03-29

### Added

- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))
</blockquote>

## `mdbook_lad_preprocessor`

<blockquote>

##
[0.1.5](v0.1.4-mdbook_lad_preprocessor...v0.1.5-mdbook_lad_preprocessor)
- 2025-03-29

### Added

- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))

### Fixed

- make all links in the mdbook preprocessor relative
([#392](#392))
- mdbook preprocessor links not taking into account root url
([#391](#391))
</blockquote>

## `ladfile_builder`

<blockquote>

##
[0.3.0](v0.2.6-ladfile_builder...v0.3.0-ladfile_builder)
- 2025-03-29

### Added

- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))
</blockquote>

## `bevy_mod_scripting`

<blockquote>

##
[0.11.0](v0.10.0...v0.11.0)
- 2025-03-29

### Added

- allow the conversion of lua functions into `ScriptValue` via
`DynamicScriptFunction`
([#396](#396))
- improve tracing spans, add `profile_with_tracy` feature flag
([#394](#394))
- add `profile_with_tracy` feature which plays nicely with bevy's
`bevy/trace_tracy` feature
([#393](#393))
- Add initial benchmarks, integrate them into CI & add getters/settters
for `Scripts` resource
([#381](#381))
- ✨ Dynamic Script Components, `register_new_component`
binding, `remove_component` no longer requires `ReflectComponent` data
([#379](#379))
- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- optimize access map
([#395](#395))
- add ScriptValue override for printing opaque values
([#380](#380))
- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))
- [**breaking**] bump bersion

### Fixed

- fix global type cache not containing generic types
([#388](#388))

### Other

- switch to hashbrown hashmap in the function registry
([#399](#399))
- try play with hashing for access maps
([#398](#398))
- allow check creation for bencher
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Maksymilian Mozolewski <makspl17@gmail.com>
makspll pushed a commit that referenced this pull request Mar 30, 2025
## 🤖 New release

* `bevy_mod_scripting_derive`: 0.11.0 -> 0.11.1
* `bevy_system_reflection`: 0.1.0 -> 0.1.1 (✓ API compatible changes)
* `bevy_mod_scripting_core`: 0.11.0 -> 0.11.1 (✓ API compatible changes)
* `bevy_mod_scripting_lua`: 0.11.0 -> 0.11.1 (✓ API compatible changes)
* `bevy_mod_scripting_rhai`: 0.11.0 -> 0.11.1 (✓ API compatible changes)
* `bevy_mod_scripting_functions`: 0.11.0 -> 0.11.1 (✓ API compatible
changes)
* `ladfile_builder`: 0.3.0 -> 0.3.1 (✓ API compatible changes)
* `bevy_mod_scripting`: 0.11.0 -> 0.11.1 (✓ API compatible changes)

<details><summary><i><b>Changelog</b></i></summary><p>

## `bevy_mod_scripting_derive`

<blockquote>

##
[0.11.0](bevy_mod_scripting_derive-v0.10.0...bevy_mod_scripting_derive-v0.11.0)
- 2025-03-29

### Added

- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))
</blockquote>


## `bevy_mod_scripting_core`

<blockquote>

##
[0.11.0](bevy_mod_scripting_core-v0.10.0...bevy_mod_scripting_core-v0.11.0)
- 2025-03-29

### Added

- optimize access map
([#395](#395))
- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- improve tracing spans, add `profile_with_tracy` feature flag
([#394](#394))
- add `profile_with_tracy` feature which plays nicely with bevy's
`bevy/trace_tracy` feature
([#393](#393))
- Add initial benchmarks, integrate them into CI & add getters/settters
for `Scripts` resource
([#381](#381))
- add ScriptValue override for printing opaque values
([#380](#380))
- ✨ Dynamic Script Components, `register_new_component`
binding, `remove_component` no longer requires `ReflectComponent` data
([#379](#379))
- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))

### Fixed

- fix global type cache not containing generic types
([#388](#388))

### Other

- switch to hashbrown hashmap in the function registry
([#399](#399))
- try play with hashing for access maps
([#398](#398))
- allow check creation for bencher
</blockquote>

## `bevy_mod_scripting_lua`

<blockquote>

##
[0.11.0](bevy_mod_scripting_lua-v0.10.0...bevy_mod_scripting_lua-v0.11.0)
- 2025-03-29

### Added

- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- allow the conversion of lua functions into `ScriptValue` via
`DynamicScriptFunction`
([#396](#396))
- Add initial benchmarks, integrate them into CI & add getters/settters
for `Scripts` resource
([#381](#381))
- ✨ Dynamic Script Components, `register_new_component`
binding, `remove_component` no longer requires `ReflectComponent` data
([#379](#379))
</blockquote>

## `bevy_mod_scripting_rhai`

<blockquote>

##
[0.11.0](bevy_mod_scripting_rhai-v0.10.0...bevy_mod_scripting_rhai-v0.11.0)
- 2025-03-29

### Added

- [**breaking**] bump bersion
- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- Add initial benchmarks, integrate them into CI & add getters/settters
for `Scripts` resource
([#381](#381))
</blockquote>

## `bevy_mod_scripting_functions`

<blockquote>

##
[0.11.0](bevy_mod_scripting_functions-v0.10.0...bevy_mod_scripting_functions-v0.11.0)
- 2025-03-29

### Added

- optimize `get` and `set` functions, add `MagicFunctions` sub-registry
([#397](#397))
- ✨ Dynamic Script Components, `register_new_component`
binding, `remove_component` no longer requires `ReflectComponent` data
([#379](#379))
- overhaul mdbook preprocessor, prettify generated docs, support dummy
globals ([#377](#377))
</blockquote>

## `ladfile_builder`

<blockquote>

##
[0.3.1](v0.3.0-ladfile_builder...v0.3.1-ladfile_builder)
- 2025-03-29

### Added

- bump bevy to 0.15.3
([#401](#401))
</blockquote>

## `bevy_mod_scripting`

<blockquote>

##
[0.11.1](v0.11.0...v0.11.1)
- 2025-03-29

### Added

- bump bevy to 0.15.3
([#401](#401))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This was referenced Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant