Difference between revisions of "Sync"
(→Use) |
|||
(13 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | ==Syntax== | ||
''Syntax:'' <span style='font-size:12px;font-family:courier'><span style='color:green'>sync</span></span> | ''Syntax:'' <span style='font-size:12px;font-family:courier'><span style='color:green'>sync</span></span> | ||
− | + | * ''None'' | |
− | * None | + | |
==Use== | ==Use== | ||
− | + | '''Sync''' must be used when creating a thread on a looping function. Examples are functions that wait more than one frame for a condition to occur, such as a room being cleared, a switch being pressed, or a register being set. Sync should be called at least once in such loops. Not using sync inside of these loops may cause the game to freeze. | |
+ | |||
+ | One sync equals one frame elapsed in game, and 30 frames equals one second. Sega sometimes used multiple syncs in a function to force a specific delay, such as displaying a [[window_msg]] some number of frames after zooming in the camera. | ||
+ | |||
+ | On more technical terms, sync will cause the current coroutine to yield, allowing other coroutines to run ([[thread]] actually creates a coroutine and not a thread, and they don't run parallelly). Special menus called through opcodes will yield on their own and do not require a sync inside their function. | ||
+ | |||
+ | Opcodes that will automatically yield: | ||
+ | * [[ret]] | ||
+ | * [[exit]] | ||
+ | * [[window_msg]] | ||
+ | * [[message]] | ||
+ | * [[chat_box]] | ||
+ | * [[disp_msg_qb]] | ||
+ | * [[add_msg]] | ||
+ | * [[list]] | ||
+ | * [[fadein]] | ||
+ | * [[fadeout]] | ||
+ | * [[award_item_name]] | ||
+ | * [[award_item_select]] | ||
+ | * [[award_item_ok]] | ||
+ | * [[get_item_id]] | ||
==Example== | ==Example== | ||
<span style='font-size:12px;font-family:courier'> | <span style='font-size:12px;font-family:courier'> | ||
− | <span style='color:blue'>100: </span><span style='color:green'>call </span>101 <span style='color:orange'> | + | <span style='color:blue'>100: </span><span style='color:green'>call </span>101 <span style='color:orange'>//Call function 101</span> |
<span style='color:green'> window_msg </span>'Okay. I've waited 1 second. Now what?' | <span style='color:green'> window_msg </span>'Okay. I've waited 1 second. Now what?' | ||
<span style='color:green'> winend </span> | <span style='color:green'> winend </span> | ||
<span style='color:green'> ret </span> | <span style='color:green'> ret </span> | ||
− | <span style='color:blue'>101: </span><span style='color:green'> | + | <span style='color:blue'>101: </span><span style='color:green'>sync </span><span style='color:orange'>//Pause for 1 frame</span> |
− | + | <span style='color:green'> addi </span>R1, 00000001 <span style='color:orange'>//Add 1 to register R1</span> | |
− | <span style='color:green'> | + | <span style='color:green'> jmpi_<= </span>R1, 0000001E, 101 <span style='color:orange'>//If R1 is less then or equal to 30 jump to 101</span> |
− | + | <span style='color:green'> clear </span>R1 <span style='color:orange'>//Set R1 to 0</span> | |
− | <span style='color:green'> | + | |
− | + | ||
− | + | ||
− | + | ||
− | <span style='color:green'> | + | |
− | + | ||
− | + | ||
− | + | ||
<span style='color:green'> ret </span> | <span style='color:green'> ret </span> | ||
</span> | </span> | ||
− | == | + | ==Also see== |
− | [[ret]] | + | [[ret]], [[call]], [[window_msg]], [[winend]], [[addi]], [[jmpiule|jmp_<=]], [[clear]] |
Latest revision as of 05:39, 31 December 2022
Contents
Syntax
Syntax: sync
- None
Use
Sync must be used when creating a thread on a looping function. Examples are functions that wait more than one frame for a condition to occur, such as a room being cleared, a switch being pressed, or a register being set. Sync should be called at least once in such loops. Not using sync inside of these loops may cause the game to freeze.
One sync equals one frame elapsed in game, and 30 frames equals one second. Sega sometimes used multiple syncs in a function to force a specific delay, such as displaying a window_msg some number of frames after zooming in the camera.
On more technical terms, sync will cause the current coroutine to yield, allowing other coroutines to run (thread actually creates a coroutine and not a thread, and they don't run parallelly). Special menus called through opcodes will yield on their own and do not require a sync inside their function.
Opcodes that will automatically yield:
- ret
- exit
- window_msg
- message
- chat_box
- disp_msg_qb
- add_msg
- list
- fadein
- fadeout
- award_item_name
- award_item_select
- award_item_ok
- get_item_id
Example
100: call 101 //Call function 101
window_msg 'Okay. I've waited 1 second. Now what?'
winend
ret
101: sync //Pause for 1 frame
addi R1, 00000001 //Add 1 to register R1
jmpi_<= R1, 0000001E, 101 //If R1 is less then or equal to 30 jump to 101
clear R1 //Set R1 to 0
ret