Comments (7)
Hi @apriori! Thanks for opening this issue!
As for why that's not working, I am not entirely sure, I would start with creating an issue/question in the mlua repo, It looks like this should work. For context are you using the unsafe_lua_modules
feature? It may be worth trying that out as well, although I don't think that's the problem since it seems you have the dofile
function loaded in. It seems to me that the root of the issue may stem from there.
As for alternative approaches, you can always define an API for running scripts, for example run_script('script.lua')
or run_script_with_env('script.lua', _ENV)
(maybe you can pass the environment down with https://docs.rs/mlua/latest/mlua/struct.Chunk.html#method.set_environment) which could take care of setting up the script context as you would like to. This is of course a more involved solution.
from bevy_mod_scripting.
Hello. Thanks for your swift response.
The context is of course a lot more involved. My current approach loads a an entire script without executing it in a coroutine that is stored in a known global member.
What I would like to do now have is to have a globally available override function for "dofile etc." that will suspend the lua vm execution until the respective file has been loaded by the asset loader and its value/content is available for injection.
So far I came up with the following solution:
- Replace LuaScripting host with a variant that does the following:
- Attach APIs before executing the script snippet
- Attach the following snippet before script loading:
__active_async_func = {} function doscript(arg) __active_async_func['doscript'] = arg coroutine.yield() end
- Pack the script snippet in a coroutine and assign to e.g.
__mod_coroutine
- Resume that coroutine immediately
After that an exclusive system can look in the global variable __mod_courtine
and the __active_async_func
for asset loading dispatch and only call resume after the script has been injected via mlua::Lua::load
.
While this will work, it is quite involved. So far this solution is only working for depth 1, so I guess I need to create a stack of coroutines on the lua side and always drive the stack head further.
The thing is, I cannot really change the available lua code too much, because I am loading game assets of an old game in an unchanged way.
Do you have async functions for the API provider on the agenda?
from bevy_mod_scripting.
Hmm, thanks for the detailed explanation! I am currently prioritizing completing language and Bevy API support, but I am happy to take on PR's! Did you try the mlua_async
feature yet? To help me implement a good solution from your perspective, how would you like to see async support for APIProviders
implemented ?
from bevy_mod_scripting.
Honestly, I can't tell yet, how to do this in an acceptable way. Bevy ergonomics around async is a bit lacking. However, I was able to so solve my usecase using your proposed mlua_async
. It is ... quite verbose. Not sure how to generalize this to more general async functions that require some kind of world access.
You will get the general idea looking at this snippet (not a fully runnable example. if you want one, I could create it)
https://gist.github.com/apriori/08c7c9e909dd4e9701cd9b460ad23763
Of course one could completely bypass all the mumbo jumbo involving PriorityEvent
by directly refering to a world AssetIO
and call its async functions directly, however I did not want to do that. At least using mlua_async
prevents having to pretty much suspend and resume using coroutine magic on both the rust and lua end. It is completely done by mlua
.
from bevy_mod_scripting.
I see! Glad to see you got something working, the gist is much appreciated, it's always good to see how the consumers of this crate use it in order to improve it! I will look into this, can't guarantee when but hopefully should get something into next release that will help with your use case!
I noticed a new reflect type data ReflectAsset
is present on AssetIO
so it may well be possible to directly expose that in the Bevy API
from bevy_mod_scripting.
The lua engine used (Luajit) claims to be fully resumable, so it should be possible to yield across the language barriers
The "fully resumable" term in this context relates to language features, such as (x)pcall/metamethods/iterators/etc (which lua 5.1 does not support). Unfortuantely LuaJIT cannot yield across C boundaries (this is not part of the VM) and generates the error you see.
The mlua's Rust async integration seems the best way to achieve this. It allows to yield a coroutine and then resume it based on an external event (channel message / networking call / etc). You just need an executor to do this (tokio/async-std/etc).
from bevy_mod_scripting.
Ah! Thanks @khvzak, that's very good to know!
from bevy_mod_scripting.
Related Issues (20)
- Bevy 0.12 HOT 2
- Can't compile with mlua anymore HOT 4
- Rune - Proxy macros HOT 2
- Luajit feature does not build. Other build issues as well. HOT 2
- lua globals are not registered until callback function. HOT 16
- tealr macro does not play nice with re-exported bevy_mod_scripting_lua crate
- Request: More exaustive tealr docgen example would be nice HOT 1
- REQUEST: Add instructions to readme on how to use bevy api gen HOT 2
- Registering a Bundle for Lua HOT 5
- Improved trait system
- Unified documentation Frontend + Backend
- Support for pre-processing HOT 1
- Feature for shared context
- Panic when mutating world from proxied raw method HOT 4
- Support for bevy 0.13.2 HOT 2
- Consider adding a WebAssembly interpreter as a host HOT 1
- Indexing the `Children` component always yields `nil` HOT 3
- Expose the proxy types for Bevy builtins HOT 10
- Support for bevy 0.14 HOT 3
- luau?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bevy_mod_scripting.