'branch' command explained


#1

Continuing the discussion from:

branch takes an expression that contains one or more coroutines that may take time to complete (more than one frame) and starts it off and then immediately proceeds to the next expression.

It essentially turns any coroutine into a method. This also allows you to start coroutines in methods.

branch guy._path_to_pos(pos)
guy._yell // runs same time as _path_to_pos

You may see branch used in constructor calls since a constructor is a method and methods can only call other expressions that return instantly. Coroutines can take more than one frame to complete and branch will make one or more coroutines return immediately - even if sub-expressions of the branch continue on after it completes.

branch also returns the invoked object that is running in the background such as an InvokedCoroutine

!icoro: branch guy._path_to_pos(pos)
guy._yell
// can interact with coro as desired

The expression called by the branch can be a compound expression such as a code block, then all the expressions are branched.

branch
  [
  _wait(30)
  guy._path_to_pos(pos)
  guy._jump
  ]
guy._yell

!iexpr: branch
  [
  _wait(30)
  guy._path_to_pos(pos)
  guy._jump
  ]
guy._yell
// can use iexpr

The expression called by a branch is essentially a closure and it follows the same rules as a closure such as you cannot rebind : a temporary variable, If you want to change a variable you must assign := a new value.

banch is quite handy and very powerful.

Other concurrent commands

  • sync - runs two or more durational expressions (a coroutine or other blocks of code that contain one or more coroutines) simultaneously and when the last durational expression completes (longest time) then it runs any following expressions
    sync
      [
      _do_this
      _do_that
      ]
    // Runs after both _do_this and _do_that complete
    _do_next
  • race - runs two or more durational expressions simultaneously and when the first durational expression (shortest time) it aborts the other durational expressions and then runs any following expressions
    race
      [
      _do_this
      _do_that
      ]
    // Runs after either _do_this or _do_that completes
    _do_next
  • Or as a closure that runs concurrently.

The `Mind`, the `Master` and who pulls the strings in SkookumScript
Stop/Suspend/Resume a Coroutine?
Branch, Race, Sync clarification
Great curated posts to learn about SkookumScript
Rotate a cube basic test?
What does it mean for a coroutine to be "terminated externally"?
What is branch and change and what's the difference between these two?
Re: Sync and aborting coroutines
Basic Tutorial 1: Create a cube Blueprint and rotate it using Skookum
Basic Tutorial 2: Spawn cubes dynamically and manipulate them using Skookum