Jump to content

BSL:Manual: Difference between revisions

177 bytes added ,  11 December 2023
m
this sample code was always cursed; should be more logical now
m (more wording)
m (this sample code was always cursed; should be more logical now)
Line 492: Line 492:
Programmers commonly use "else if" statements to save some CPU cycles. For instance, if you had a long string of "if" statements to handle different possible values for a variable, why should they all be evaluated once you have already found the one that is true? Imagine the above example, but with 30 possible values for "error". In reality, you won't find much benefit in trying to save cycles with BSL since typical game scripts are not computationally expensive. However, "else if" can also simplify the flow of logic:
Programmers commonly use "else if" statements to save some CPU cycles. For instance, if you had a long string of "if" statements to handle different possible values for a variable, why should they all be evaluated once you have already found the one that is true? Imagine the above example, but with 30 possible values for "error". In reality, you won't find much benefit in trying to save cycles with BSL since typical game scripts are not computationally expensive. However, "else if" can also simplify the flow of logic:


  if (d eq 0)
  if (d eq 10)
     ...
     # special-case code for d being 10
if (d eq 1)
  if ((d ne 10) and (d eq 3))
    ...
     # special-case code for d being 3
  if ((n eq d) and (n > 0) and (d > 1))
  if ((d ne 10) and (d ne 3) and (d > 0))
     ...
     # code for handling any other positive value for d
  if ((n eq 0) and (d > 1))
     ...


In checking the condition of "n" and "d" in this example, we have to keep checking after the second statement whether "d" is greater than 1; otherwise, multiple "if" statements could evaluate to true and execute their code, and we only want to act on one of these possible situations. Using "else if" allows us to be confident that only one statement can run:
In checking the condition of "d" in this example, we have to keep checking after the first statement whether our previous conditions were not true; if we didn't, multiple "if" statements could evaluate to true and execute their code, and we only want to act on one of these possible situations. Using "else if" allows us to be confident that only one statement can run:


  if (d eq 0)
  if (d eq 10)
     ...
     # special-case code for d being 10
  else if (d eq 1)
  else if (d eq 3)
     ...
     # special-case code for d being 3
  else if (n eq d)
  else if (d > 0)
    ...
     # code for handling any other positive value for d
else if (n eq 0)
     ...


Notice how we can shorten the logical tests without losing anything. "d" is still implicitly required to be non-zero in statements 2-4, or else they will not even be evaluated.
Thanks to the "else" keyword, "d" is implicitly guaranteed to not be 10 if statement 2's code is being run, and to not be 10 or 3 if statement 3's code is being run.


===Flow interrupt===
===Flow interrupt===
Use these keywords to interrupt the execution of BSL. Note that Bungie West also frequently used these built-in commands to halt the execution of a script until the current or previous command finished: [[chr_animate_block]], [[chr_envanim_block]], [[chr_playback_block]], [[cm_anim_block]], [[cm_interpolate_block]], [[cm_orbit_block]], [[env_anim_block]], [[env_setanim_block]], [[sound_dialog_play_block]].
Use the keywords below to interrupt the execution of BSL. Note that Bungie West also frequently used these built-in commands to halt the execution of a script until the current or previous command finished: [[chr_animate_block]], [[chr_envanim_block]], [[chr_playback_block]], [[cm_anim_block]], [[cm_interpolate_block]], [[cm_orbit_block]], [[env_anim_block]], [[env_setanim_block]], [[sound_dialog_play_block]].


====return====
====return====