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

Small cleaning up + Fix for crash when looping through empty Vec #87

Merged
merged 27 commits into from
Nov 18, 2023

Conversation

zwazel
Copy link
Contributor

@zwazel zwazel commented Nov 10, 2023

This pull request cleans the Rhai side of the project up a bit.

  • Removed a second get_children that didn't even work
  • fixed the get_parent, before it wasn't found by Scripts, presumably because not the correct World was asked for (&ScriptWorld instead of ScriptWorld). Also made it return a Entity if a parent was found, and a i32 of value -1 if not, instead of Option, as that isnt really supported by Rhai.
  • added an additional check for accessing an entity in the reload_script as the game often crashed there. This adds on check if entity exists before accessing #68
  • Also made a partial fix for Crash when trying to loop through empty Vec #86. As the app no longer crashes, but we still get an error in the console (better documented in the issue itself).

Copy link
Owner

@makspll makspll left a comment

Choose a reason for hiding this comment

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

Great catches, just a few comments

bevy_mod_scripting_core/src/hosts.rs Show resolved Hide resolved
bevy_script_api/src/common/std.rs Show resolved Hide resolved
bevy_script_api/src/rhai/bevy/mod.rs Show resolved Hide resolved
@zwazel
Copy link
Contributor Author

zwazel commented Nov 15, 2023

I suggest we move the existing Wrapper example into lua, as it only really shows how to work with Lua.
I'm working on the example for Rhai right now, so we can test the fixes

@zwazel zwazel marked this pull request as draft November 15, 2023 12:20
@zwazel
Copy link
Contributor Author

zwazel commented Nov 15, 2023

btw while working with the Wrappers in Rhai, i noticed that i always have to manually implement RhaiCopy, even though it's completely empty.

#[derive(Resource, Reflect, Default, Clone, Debug)]
#[reflect(Resource, RhaiProxyable)]
pub struct MyThing {
    usize: usize,
    string: String,
    array: Vec<usize>,
}

impl RhaiCopy for MyThing {}

Is this wanted or an oversight?

@zwazel
Copy link
Contributor Author

zwazel commented Nov 15, 2023

As i'm working on the wrapper example for rhai, i noticed following thing:
When I Reflect on RhaiProxyable, I no longer can access the values of my Struct. seen in: c282ada
When I don't use RhaiProxyable, everything works fine. as seen in: 59ac562

Am I misunderstanding the need for RhaiProxyable?

@zwazel zwazel requested a review from makspll November 17, 2023 09:28
Copy link
Owner

@makspll makspll left a comment

Choose a reason for hiding this comment

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

Some more discussion, and minor changes

bevy_script_api/src/common/std.rs Outdated Show resolved Hide resolved
bevy_script_api/src/common/std.rs Outdated Show resolved Hide resolved
bevy_script_api/src/common/std.rs Show resolved Hide resolved
examples/rhai/wrappers_rhai.rs Outdated Show resolved Hide resolved
assets/scripts/multiple_events_rhai.rhai Outdated Show resolved Hide resolved
@makspll
Copy link
Owner

makspll commented Nov 17, 2023

As i'm working on the wrapper example for rhai, i noticed following thing: When I Reflect on RhaiProxyable, I no longer can access the values of my Struct. seen in: c282ada When I don't use RhaiProxyable, everything works fine. as seen in: 59ac562

Am I misunderstanding the need for RhaiProxyable?

So the discussion above might shed some more light on this but let me know if you are still confused. Basically if you don't register RhaiProxyable yourself, you will receive a ReflectedValue type object, which means you are operating using pure reflection, and have no function support.

Registering it yourself is needed when you need a custom proxy type which has custom methods and behaviours attached. But because Rhai doesn't have support for generating those nicely it's not easilly available here yet as seen in lua.

The need for RhaiCopy arises from the automatic implementation for RhaiProxyable which assumes your type is its own proxy and that it is converted into this proxy by copying, hence the name of the trait. This lets you register all Rhai goodies on your type in your APIProvider and use them that way. But of course since you are not using ReflectedValue anymore you lose the automatically generated getters for reflected fields.

Hope this makes sense

Co-authored-by: Maksymilian Mozolewski <[email protected]>
@zwazel zwazel marked this pull request as ready for review November 18, 2023 21:16
@zwazel zwazel requested a review from makspll November 18, 2023 21:16
@zwazel
Copy link
Contributor Author

zwazel commented Nov 18, 2023

So this pull request should be good now, applied all your suggestions. And am looking forward for the wrapper rewrite ;)

Copy link
Owner

@makspll makspll left a comment

Choose a reason for hiding this comment

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

LGTM, thanks again, great work!

@makspll makspll changed the title Small cleaning up + Partial fix for crash when looping through empty Vec Small cleaning up + Fix for crash when looping through empty Vec Nov 18, 2023
@makspll makspll merged commit 9c830b1 into makspll:main Nov 18, 2023
12 of 13 checks passed
@zwazel zwazel deleted the fix/check_entity_first branch November 30, 2023 09:49
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.

Crash when trying to loop through empty Vec
2 participants