Jump to content

BSL:Manual: Difference between revisions

148 bytes removed ,  2 November 2017
m
→‎schedule ... repeat ... every ...: copy edit (style consistency)
m (lede wording)
m (→‎schedule ... repeat ... every ...: copy edit (style consistency))
Line 671: Line 671:
  schedule dprint("Is this annoying yet?") repeat 50 every 20;
  schedule dprint("Is this annoying yet?") repeat 50 every 20;


This seem to be equivalent to a simplified for loop in a C style programming language (equivalent code in C style for loop is showed next):
This is basically equivalent to the following "for" loop in a C-style programming language:


  for(int i=0; i!=repeat_value; i++){
  for (int i = 0; i < 50; i++)
     function_name();
{
     wait(every_value);
     print("Is this annoying yet?");
     wait(20);
  }
  }


<u>infinite loop</u>
To create an infinite loop, you can use "0" or a negative number as the repeat value:


To create an infinite loop you can use 0 (or a negative number like -1) as repeat value:
  schedule dprint("Is this annoying yet?") repeat 0 every 20; # repeats forever
  schedule dprint("Is this annoying yet?") repeat 0 every 20; # repeats forever


<u>using as replacement for recursive functions</u>
"schedule-repeat-every" can be used in place of recursive functions that call themselves ''n'' times using "fork". For example,


schedule ... repeat ... every ... is a very useful function and can be used to replace recursive functions that call themselves n times using fork function(), for example:
  func void hey(void)
 
{
  func void hey(){
     dmsg("hey");
     dmsg("hey");
     sleep(1);
     sleep(1);
     fork hey();
     fork hey();
  }
  }
 
  func void main () {
  func void main(void)
{
     fork hey();
     fork hey();
  }
  }
Line 699: Line 699:
can be replaced by:
can be replaced by:


  func void hey(){
  func void hey(void)
{
     dmsg("hey");
     dmsg("hey");
  }
  }
 
  schedule hey() repeat 0 every 60;
  schedule hey() repeat 0 every 60;


Note: (60 ticks <=> 60 frames <=> 1 second <=> sleep(1) [ <=> means equivalent ]
Note the significant difference between the built-in "sleep" function, which operates in whole seconds, and the "schedule" commands, which operate in ticks (1/60 of a second). Using "schedule" gives you much greater control over the timing of BSL execution than a "fork"+"sleep" loop would.
 
Recursive functions using fork have also the limitation of being called at the minimum interval of 1 second where the schedule ... repeat ... every ... can be called to the minimum interval of 1/60 seconds.


==Variables==
==Variables==