hollow-cube / luau-java

Luau bindings for Java
MIT License
11 stars 0 forks source link

Running multiple lua scripts at the same time without multi-threading #1

Closed orange451 closed 6 days ago

orange451 commented 2 months ago

In my project I would like to implement luau. I want to follow a similar design paradigm to Roblox. By this, I want to create a concept called a Script, which is a container for lua code, that executes at runtime. I want to have multiple scripts able to run at the same time, but still exist within the same cpu thread. Kind of like a green-thread. Roblox solves this with their own scheduler, which halts scripts execution and allows for others to run. This gives the appearance of a multi-threaded application. Is this something that I can easily implement with your library?

My main concern is implementing a task scheduler. Most other lua libraries I see with java implement the Thread using javas' Threads, which means multiple scripts cannot run at the same time without some serious sandboxing/synchronizing to prevent the game state from becoming unstable.

mworzala commented 1 month ago

Hey! Sorry for the delayed reply, I didn't notice the issue until now.

I haven't tried this, and am still learning how the library works myself so it's possible I will get some things wrong (I will have to do something similar eventually so can write more after). It is valid to yield from an interrupt, so I believe you could use that mechanic to pause execution after some set wall time, and then resume the thread later (from the same JVM thread).

More generally, this is a light wrapper around luau. The mechanics of the c library are maintained (ie, Lua threads are not mapped to JVM threads in any way unless you do so yourself).