-
-
Notifications
You must be signed in to change notification settings - Fork 43
feat: Add on_script_reloaded callback. #421
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
base: main
Are you sure you want to change the base?
Conversation
A further alternativeI guess as an alternative going with the two functions, one could do this: mode = 1
function on_script_reload()
print("Before I go, take this.")
return mode
end
function on_script_loaded(value)
if value then
print("I'm back. Where was I?")
mode = value
end
end My criticism above doesn't apply because
The preceding design looks like this for comparison:
|
NOTE: This currently only works when context sharing is enabled. I made an attempt to support non-shared contexts, but it seemed potentially infeasible. I'd love to hear @makspll's take on whether that seems right. |
There was a problem hiding this 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 adds an on_script_reloaded callback to support state preservation during script reloads, addressing issue #419.
- Adds documentation for the on_script_reloaded callback.
- Registers the new callback event in the event system.
- Implements state saving and restoring logic during script reloads.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
docs/src/ScriptingReference/core-callbacks.md | Updates documentation to include on_script_reloaded details and example code. |
crates/bevy_mod_scripting_core/src/event.rs | Registers the OnScriptReloaded callback label. |
crates/bevy_mod_scripting_core/src/commands.rs | Implements state handling for on_script_reloaded in the script reloading command. |
crates/bevy_mod_scripting_core/src/asset.rs | Adds a warning when a script’s language is unknown. |
&OnScriptReloaded::into_callback_label(), | ||
&self.id, | ||
Entity::from_raw(0), | ||
vec![ScriptValue::Bool(true)], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None
for the missing argument
My thoughts so far: In favour ✅
Points of hmm 🤔
|
|
||
The first parameter `save` informs you whether it is time to save a value or restore it. | ||
|
||
Before the reload, it is called with one argument: `true`. After the script is reloaded, it is called with two parameters: the first is `false` and the second is value returned from before. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as of now this seems to be misleading for non-shared contexts, in which case only the second half of the callback would happen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is misleading. The second callback though will never happen because it's dependent on the first callback happening.
Sorry, I wrote it before I realized it didn't cover non-shared context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohh, I didn't catch the second conditional
Let me see if I can recreate the problem I faced. Huh. Well, that's funny. It's working now and it's as easy as I hoped it would be. New PR #424 incoming. The block of code that calls the
I agree. It'd be best to unify them. |
Summary
I added an
on_script_reloaded
function that is called before and after on_script_loaded on reloads. This addresses the issue I filed #419.I updated the core-callbacks doc with example code. Here's the example code shown there:
I'm not certain about the name.
on_script_reload
might be the name I'd choose with a blank slate.Alternatives
One could designate a special variable to capture. But I think a function is preferable because a reload can interrupt at any time and keeping that value up to date all the time instead of when it's required seems inefficient.
One could break this into two functions, but they don't do anything unless both are implemented so it seemed wise to keep them as a single function.
Testing
I exercised it manually with errors in both the saving of state and restoring state, and both were reported as the usual mechanism.
Mistake
I started this branch on top of my other pull request #418. Oops. Luckily #418 is not controversial or large.