4 ms·
Sorry, 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
by SomeCallMeTim 10y ago
Sorry, 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.