makspll / bevy_mod_scripting

Bevy Scripting Plugin
Apache License 2.0
398 stars 31 forks source link

How do I correctly despawn Entities with scripts? #79

Closed zwazel closed 1 year ago

zwazel commented 1 year ago

I despawn an entity (recursively) that has a script attached to it. but the script still "exists" and receives the events.

I'm currently only working with the Rhai implementation, so idk if this is a general problem or not. but its definitely not what i expect it to work like XD

zwazel commented 1 year ago

I have updated my Example in my Fork to showcase the issue:

https://github.com/zwazel/bevy_mod_scripting/blob/e383c5b97653a037a54451aa9ceea1cd31ff7c53/examples/rhai/multiple_events_rhai.rs#L108-L117

The example now immediately despawns the entity after calling "on_update" once. The "on_test" is still being called by the scripts, even though their entity doesn't exist anymore.

(The script)

zwazel commented 1 year ago

I was experimenting a little bit, and interestingly in my example, out of the 10 entities, the context is only being removed from 9. so the "on_test" is called on only 1 of them.

Updated the example again that shows this.

zwazel commented 1 year ago

okay, there seems to be a problem in my own logic. the crate does it as expected, now i just need to find my issue. the example will be shortly updated, it works as intended

zwazel commented 1 year ago

Okay, nope. there is indeed a bug.

(sorry for all the messages...)

makspll commented 1 year ago

Haha, you're right there is a bug, and it's really weird I haven't seen this before, guess I haven't had a situation where entity id != script Id and then I removed it? very good catch thanks!