(I think this goes here, really i'm asking about scripting language design, for games, so i really don't know where to put it).
I was modifying the scripting language (Jewel specifically) I picked for use in my game, and I ran into some trouble when I was attempting to expand on it's cooperative multithreading. The problem I'm running into is that in most cases a thread is blocking because it's waiting on an animation to finish, or a sound to play, or something like that, so I should have the scheduler resume the thread on an event. But that can't work because if another callback says to play a different animation: the one I told to play will never finish, and the thread context will never resume. I could solve this by resuming each thread each tick--most of which would return almost immediately--to check that nothing went wrong; however, while this would work it doesn't really solve the problem as much as move it, and add a performance hit. The only other thing I can come up with is to have the event manager throw an error if a thread is waiting on something that got interrupted; which would necessitate putting try/catch blocks everywhere in the scripts, which I feel would make them rather rigid and confusing, when flexibility and abstraction are really the whole point of having a scripting language.
Then it occurred to me that the rest of the time I'm almost always waiting on a condition, so I could either create a modified if statement which would block the thread until the condition is true, and a modified while that runs it's block once every tick instead of constantly; or use yields and loops for similar results. But this has similar problems to the above, only worse, for example if I have an object that uses a while loop to approach another object every tick: I need to check that nothing deleted it each tick. Again I know how to handle this, but I can't think of any good way to abstract the problem to retain flexibility of a scripting language.
And because the way to deal with this is essentially using a lot of similar code I feel like there must be some way to abstract the issue to make it easy for the script writer, but I can't think of what that would be, and I'd like to know if anyone here has any ideas on how to do it.
On a related note, if an object has callbacks such as:
method OnCameIntoView(Enemy e);
method OnWentOutOfView(Enemy e);
Obviously I should do something if I am in both callbacks at the same time in reference to the same enemy. I considered doing things like finite state machines and a "schedule(functor, time)" function (mostly because i saw this in another game engine) instead of yielding, but I don't see how those would do more than move the problem to another structure without making it easier to solve. Maybe i'm just thinking about it in the wrong way, but this is something i can't think of how to solve, and any advice would be appreciated.