Sign in to follow this  
JHL

Saving threads for later use in lua 5.0

Recommended Posts

I'm working on a level scripting for my engine, the way I'm working it is to define a table of functions in a script file then the engine traverses this table runing each function the reasons for this ia a) each function can disable its self by setting its own table entry to nil and b) each function can be a coroutine the coroutine part is where I'm having trouble each function is run in its own thread and if that function 'waits' then I need to save that thread but whats the best way to save an arbitrary number of threads?

Share this post


Link to post
Share on other sites
Quote:
I'm working on a level scripting for my engine, the way I'm working it is to define a table of functions in a script file then the engine traverses this table runing each function the reasons for this ia a) each function can disable its self by setting its own table entry to nil and b) each function can be a coroutine


If you do it like this, you may want to have the engine remove the functions, depending on their return values. This way you can add the same function multiple times in the table, or you can have multiple coroutines that use the same function.

Quote:
the coroutine part is where I'm having trouble each function is run in its own thread and if that function 'waits' then I need to save that thread but whats the best way to save an arbitrary number of threads?


The coroutine itself _saves_ it's own thread. That's what coroutines do, they're objects that save a point in the execution of a function and allow you to resume that execution from that exact point. So you add the coroutine to the table, and have the engine call it, and then if the coroutine signals it finished it's computation remove it from the table.

table.insert and table.remove are all you need.


Quote:
If I recall correctly, Sneftel was working on something like this called Pluto.


Sneftel's project saves any Lua data to a file (including coroutines and closures and functions).

Share this post


Link to post
Share on other sites
Darn you guys.... I'm too late to plug my own project! [lol]

Anyway, yeah, check out Pluto to see if it's what you want. I'm assuming that by "save" you literally mean saving the game-state, to restore later. If you need something else, try describing it more clearly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this