Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4082074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:08:32+00:00 2026-05-20T18:08:32+00:00

This example has been used in another question to illustrate how coroutines can be

  • 0

This example has been used in another question to illustrate how coroutines can be used to script cutscenes in a video game:

bob.walkto(jane)
bob.lookat(jane)
bob.say("How are you?")
wait(2)
jane.say("Fine")
...

Each function yields to the main engine which does animation, timing, etc. before resuming the coroutine. A possible alternative to coroutines would be an event queue instead of code, but then one has to implement control logic and loops as events. Are there any other alternatives to coroutines that can be used to implement this kind of functionality? I’ve seen callbacks mentioned in some articles, but I’m not sure how the code would look.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:08:33+00:00Added an answer on May 20, 2026 at 6:08 pm

    Coroutines are well suited for this since you get to keep all your local state variables with no hassle. I.e. without having to manually store it in a context somewhere.

    But I don’t see an event system as an alternative. More as a supplement that you will most likely want to have in addition to a coroutine-based scripting system.

    Example (in somewhat coherent C++):

    You have implemented a behavior using coroutines along these lines:

    class EnterHouse : public NPCBehavior
    {
        EnterHouse(House theHouse) { _theHouse = theHouse; }
        void Begin() { _theHouse.AddNPC(NPC()); }
        void Update()
        {
            NPC().WalkTo(_theHouse.GetDoor().Position());
            NPC().FaceObject(_theHouse.GetDoor());
            NPC().PlayAnimation(NPC().Animations().Get(eAnimKnockOnDoor));
            Sleep(1.0f);       
            NPC().OpenDoor(_theHouse.GetDoor());
        }
        void End() { /* Nothing */ }
    
        private House _theHouse;
    }
    

    Imagine that the methods on the NPCs will themselves create NPCBehavior objects, push them on some sort of behavior stack and return from the call when those behaviors complete.

    The Sleep(1.0f) call will yield to your script scheduler and allow other scripts to run. The WalkTo, FaceObject, PlayAnimation and OpenDoor will also call Sleep to yield. Either based on a known animation duration, to wake up periodically to see if the pathfinder and locomotion system are done walking or whatever.

    What happens if the NPC encounters a situation he will have to deal with on the way to the door? You don’t want to have to check for all these events everywhere in your coroutine-based code. Having an event system supplement the coroutines will make this easy:

    An trashcan topples over: The trashcan can broadcast an event to all nearby NPCs. The NPC object decides to push a new behavior object on his stack to go and fix it. The WalkTo behavior is in a yielding Sleep call somewhere, but now the FixTrashcan behavior is running due to the event. When FixTrashcan completes the WalkTo behavior will wake up from Sleep and never know about the trashcan incident. But it will still be on its way to the door, and underneath it we are still running EnterHouse.

    An explosion happens: The explosion broadcasts an event just like the trashcan, but this time the NPC object decides to reset it running behaviors and push a FleeInPanic behavior. He will not return to EnterHouse.

    I hope you see what I mean by having events and coroutines live together in an AI system. You can use coroutines to keep local state while still yielding to your script scheduler, and you can use events to handle interruptions and keep the logic to deal with them centralized without polluting your behaviors.

    If you haven’t already seen this article by Thomas Tong on how to implement single-threaded coroutines in C/C++ I can highly recommend it.

    He uses only the tiniest bit of inline assembly (a single instruction) to save the stack pointer, and the code is easily portable to a whole bunch of platforms. I’ve had it running on Wintel, Xbox 360, PS3 and Wii.

    Another nice thing about the scheduler/script setup is that it becomes trivial to starve off-screen or far-away AI characters/scripted objects if you need the resources for something else. Just couple it with a priority system in you scheduler and you are good to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone done this / have some example code?
Has anyone seen this type of IE display problem? Example http://xs133.xs.to/xs133/08465/ie_problem910.jpg.xs.jpg Note that it
Following this example, I can list all elements into a pdf file import pyPdf
This example is from php.net: <?php function Test() { static $a = 0; echo
Given this example: <img class=a /> <img /> <img class=a /> <img class=a id=active
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this example (typical in OOP books): I have an Animal class, where each
In this example the .Stub returns a new memory stream. The same memory stream
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
There is this example code, but then it starts talking about millisecond / nanosecond

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.