Subroutines

Subroutines or subs contain code that can be called on demand. When a subroutine is called, it can either be called synchronously or asynchronously.

Rhythm Heaven Megamix
Megamix handles subroutines in a similar fashion to the other games in the series.

Usage in Tickflow
(Part of this section was taken and edited from the Subs and Remixes Guide document)

Overview
Each tickflow file contains multiple sections that can be called to run by each other at the same time. Each of these sections is called a sub (short for subroutine) and they always start with a name followed by a colon ':' (i.e. sub2:, start:). Every line of code from that name to where the next stop command is will be executed if the sub is called. There are two ways to call a sub: with the sub (value) or call (name) commands or with the async_sub (value) or async_call (name) commands. The sub and call commands will pause the script that called them while the sub is executing, and then continue running the original script once the called sub has finished. async_sub and async_call will call the specified sub and run it while the original script is running too.

When to use "async_"
When you want to call a subroutine that is to be executed at the same time as the "parent" code, that's when you append async_ before your call or sub. If you want the tickflow to call and complete your subroutine before moving past it, you use the normal call or sub commands. It is CRUCIAL that you know how subs are terminated. stop is used for async_ (asynchronous) subroutines, while return is used for synchronous subroutines. You're "stopping" asynchronous subs from executing further, and you're "returning" to the parent code after executing synchronous subs.

Asynchronous (async_) subroutines can be executed after a delay of your choosing. To do this, just type async_call (name), X or async_sub (value), X, with X being the time in ticks before the sub is executed.

Subs in Remixes
Usually you would just write the name of the sub after the command (i.e. async_call sub7), however remixes do things in a slightly different way. Remixes use the command get_async X, 0 in place of async_call (name), with X being a hexadecimal number. The two commands are functionally identical, however get_async looks for a matching set_func X, (name) command to get its corresponding sub. To find which sub a get_async command corresponds to, remember the hex number following it, go to the top of the tickflow file, and match the hex number to a set_func with the same number. The corresponding sub will go by the name right next to it. It's recommended to use Ctrl+F to find where its commands are located and use Notepad++'s "clone to other view" setting so you can put one view where the get_async is and use the other to find the sub.

"call" vs. "sub"
The call (name) and async_call (name) commands are the ones you should usually be using, as they call subs that are on the tickflow file you are currently editing. sub (value) and async_sub (value) will call one of the global subs that are hardcoded into the game itself. These hardcoded subs range from loading assets to count-ins (values 0 through 0x55) to cues for games (values 0x56 and above). To keep it short, call/async_call is for subs on the file, sub/async_sub is for game cues and global operations.

Example
Pretend you're editing Munchy Monk.

If you wanted to call the subroutine that triggers a single "One, go!" cue, you'd type in async_call sub56, because that was the given name of that sub within Munchy Monk's tickflow file. Note how we use async_call instead of call because the sub ends with a stop command.

If you wanted to call the same subroutine but using sub instead, you'd type in async_sub 0x56 since that's the hardcoded value of the sub.

If you wanted to call the subroutine after a delay of one beat, you'd type in async_call sub56, 0x30, because 0x30 is the hexadecimal value for 48 ticks, and 48 ticks is equal to one beat.