4 ms·
I actually hacked together my own "coroutine" scripting library in C once upon a time. Lots of macros. Same problem though. When I said slow, I meant slow to s
by SomeCallMeTim 10y ago
I actually hacked together my own "coroutine" scripting library in C once upon a time. Lots of macros. Same problem though.
When I said slow, I meant slow to save the entire state of the Lua VM. You can't even do that in C, so it doesn't really apply to the problem I was pointing out.
- gravypod 10y agoAll saveing your object state should be is memcpy'ing your array of all objects into some sort of static memory and on load looping through every object and starting a coroutine for every object. I don't think that's too much overhead.
- SomeCallMeTim 10y agoSorry, but you're totally missing the key point. Say you have an AI that's made 3 decisions and you're in the MIDDLE of a coroutine where it's waiting for the next criteria that will shift it to its next state. So the AI code looks like: local player = lookForAPlayer() if navigateToPlayerLocation() then local quest = givePlayerQuest(); while (!quest:done()) do waitWithStandardReply("Waiting for you to finish the quest!"); end if (quest:resolved()) then giveQuestReward(); while (true) do waitWithStandardReply("Thanks for finishing that quest for me!"; end else -- quest abandonded while (true) do waitWithStandardReply("Thanks for nothing!"); end end end This is rough example code, and not how I'd probably handle this case specifically, but it gives you the general idea. Each of those functions (except the quest member functions) has, somewhere inside it, one or more "yield()" statements, and some logic to make its goal happen incrementally. Yes, it's awesome to be able to code AI logic that way. But how do you save the coroutine state? The actual execution state of the coroutine itself, including what line it will resume next, the values of any bound variables, the values of any local variables, are all part of the state of the AI in that case. In C, you've got functions that may be relocated somewhere else in RAM and potentially stacks to save (if you're using a "real" coroutine library), so just doing a memcpy won't work. What would you memcpy to save the line that the script is paused on? In Lua, it can be done with Pluto, but then you can't use LuaJIT. I see libdill has the ability to use a user-supplied stack, but it doesn't appear to have a "resume" call that takes such a stack memory block, so the best that would happen is that the coroutine would start over from the beginning. Meaning that if you saved the above coroutine in the middle of a quest, the character involved would offer you a new quest when you saw them instead of accepting the result of a successfully allocated quest. If your object states are all being handled in traditional objects, then you're not using the coroutines in the way described by the article.