Replies: 1 comment
-
Since I didn't get any answers, I came back to this after taking a break from it for a bit. The solution was that I needed to add both the Sync and Send to my trait:
This did not require an unsafe marker so I assume Rust will check each of the implementations for things that will make them break the contract implied by those traits. Since these structs are only intended to add a Component to the entity, they should be safe but of course there is nothing in the struct definition that enforces that the implementations only do this. I can then create an implementation for each technology:
With this in place, I can then register the innovation types in a Startup system:
And an update system that is generic for all technologies:
And, lastly, a technology specific system:
I should note that, in general, this object-oriented approach has limited use because of the way systems work in Bevy. For example, I would have liked to generate a technology-specific Event but that is not possible because the Event system requires Events are always generated from a system with an EventWriter parameter for each Event class it generates. Thus, this approach is pretty much limited to spawning or despawning Entities and adding or removing Components. I would be interested to hear if people have alternate approaches to this problem. But, in general, I would suggest not to "throw out the baby with the bath water". I appreciate the value of an ECS system but there are still things that an object-oriented approach is better suited for. |
Beta Was this translation helpful? Give feedback.
-
My Bevy application includes a technology tree. Each technology uses the same code for determining when its requirements are met and tracking the research progress using a standard ECS approach. But, my difficulty arises when the technologies are researched because each one needs custom code for when that happens.
I admit I have done object-oriented programming for most of my development career so my thoughts immediately went to an object-oriented approach for this. I did then try to find an ECS approach but came up empty. And then I also ran into roadblocks with an object-oriented approach.
If anyone has a more ECS-style approach, I would be open to it. But, the problem is that I am going from generic system for all technologies to specific code for a given technology and so, at the very least, you would need to spawn a technology-specific Event class or attach a technology-specific Component class to some entity that I could then build a system to Query on and I think I need at least a little tiny bit of object-oriented code for this.
For that, I was thinking about a Resource with a HashMap of a technology id to an implementation of a Trait where the Trait would have nothing but one method to add a specific Component to an Entity. It seemed like a simple thing to do but does not seem possible.
This produced:
error[E0277]:
(dyn InnovationTypeTrait + 'static)
cannot be sent between threads safelyI attempted a few things like adding an unsafe Sync trait to InnovationTypeTrait but had no luck.
I then thought all I really needed was a single lambda function. So, I tried a HashMap pointing to a lambda:
But. for some reason I can't fathom, I can't use "mut" in a lambda:
Patterns aren't allowed in function pointer types [E0561]
Beta Was this translation helpful? Give feedback.
All reactions