Let's say that I want to embed a scripting engine inside some other program, to allow users to create custom behaviours for objects.
For example, when a particular event happens, the server would launch an embedded scripting engine and run a user-supplied response-to-event script supplied by each actor. Conceptually you can imagine it like a movie script. A new character enters the scene. Each of the actors in the scene must now respond to the new arrival, and that behaviour is governed by their user-supplied scripts.
Now I can launch an embedded interpreter which will run the user-supplied scripts, but I need somehow for the main application to "timeslice" or schedule those sub-processes.
For example there might be a rule that says each actor can perform one action per second. So after an actor performs the one action, I would like suspend their interpreter until their next chance to act.
Another way I thought of implementing it was for the actors to voluntarily release control. e.g. their script might go "say hello; sleep 1000 millis; wave arms; sleep 1000 mills;" If I was going to do that I might as well give them a "crash me" button to push as well.
If I had to destroy or re-enter the interpreters, I would also need some way of storing the state/program counter so it could resume.
I'm also considering running them as separate threads or processes, but I'm not sure how exactly how much control I will have over them, to be able to suspend and resume a thread or subprocess in this manner. Edit: I would also be dependant on the clients behaviour to synchronise everything, just as if I was depending on them to sleep.
I am told this is a common game design pattern, but I can't really find any useful information on how to implement it. I've looked at embedded interpreters, for example Nashorn JS in Java or Lua in C++ and they don't appear to support suspending the interpreter, which would be "easy way" to do this.
Can anyone give guidelines here? At the moment I think thread or process control might be the best solution?
Edit: I don't really to want to have write my own interpreter ... although if I did, I could obviously suspend it at will. It would be a lot of work even for a basic stack machine.
Edit: I'm beginning to think that I need to have a custom interpreter that I tell to "run 100 opcodes (etc), then sleep".