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

Scoped alternative to World API with granular dynamic borrow checking #247

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

kabergstrom
Copy link

@kabergstrom kabergstrom commented Jan 22, 2022

This PR implements a scoped API that mirrors much of the World API, but does component-level borrow checking at runtime instead of relying on static borrow checking. See ErgoScope.

Still have a few improvements to do, and I hope to expand the API to include queries, but hopefully I can get some feedback on the design and implementation.

The primary use for this is more straight-forward gameplay code by avoiding mutable borrows on World, but also

  • Supporting arbitrarily deep callbacks that can modify any world state, while having ComponentRefs active
  • Supporting interacting with World from languages that don't support compile time borrow checking

@kabergstrom
Copy link
Author

I recently realized that with the indirection for data access that exists, it'd be possible to keep component references around between scopes, and just patch up the data pointer on scope exit/enter. Of course, actual access when a scope isn't active would panic. I imagine this could be nice as a way of storing persistent component references in scripts for example. What do you think @Ralith ?

@Ralith
Copy link
Owner

Ralith commented Feb 4, 2022

It's an intersecting prospect. Off the cuff, I'm concerned that it would be very easy to capture pseudo-references that might be unpredictably invalidated while the scope is inactive. For example, if you have a script/task that gets a component, then waits, then manipulates that component, it would be prone to panicing if the associated entity was despawned for any reason. Would this be too large a footgun?

@kabergstrom
Copy link
Author

kabergstrom commented Feb 5, 2022

I think we can provide try_read/try_write functions for borrowing, so that errors can be propagated within the scripting environment as opposed to panicking. This would be equivalent to a null reference exception in Unity's C# for example, where the behaviour is similar to what you're talking about. Game objects & components can be destroyed between wait points of coroutines, in which case a null reference exception is thrown on access.

For what it's worth, in a game, you often can't define lifetimes of objects statically. Coroutines do have to handle dynamic conditions like objects being destroyed between wait points, as it can be the reality of the gameplay rules.

@kabergstrom kabergstrom changed the title WIP: Scoped alternative to World API with granular dynamic borrow checking Scoped alternative to World API with granular dynamic borrow checking Jul 16, 2022
@kabergstrom
Copy link
Author

I wrote a small simulation to test how it is to use the API

https://github.com/kabergstrom/cogito/blob/master/src/main.rs

Fixed doc comment for remove_dynamic
Resize storage in AccessControl on spawn
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.

2 participants