Subroutines

Lightwing features several branching commands that can alter the normal script execution order to form loops and subroutines. Previous examples use unconditional jump commands to form continuous loops of pages. This example uses the call and return commands to form subroutines of pages that produces the same results as the previous example, but makes the pages more reusable.

This example uses the Day condition to jump to an appropriate page for the current day of the week. The other valid condition names for the index option are, Hour, Month and Count. Up to 24 branch page names can be listed, if appropriate for the branch condition. If the value of the index condition is greater than the number of pages listed, the call command is ignored.

The pages named for the days of the week are subroutines in this example. They could have any number of pages, but the last page of each subroutine must end with a return command. This causes a branch back to the next page after the previous call command. The return page names are stored internally on a 64 level LIFO stack, so subroutine calls can be nested up to that level.

The page named Pluto displays the current date and uses the count option to form a loop which repeats 3 times before falling through to the page named Done. There are other condition options shared by the jump and call commands named day, before, after and portrait, which can branch based on specific dates or the orientation of the display. The return command has no condition options.

Notice that the pages named Loop and Done have no time options. These are zero-time pages that serve only to perform the call and jump commands. This is necessary because only a single branch command is allowed per page.

Look at the output in the Lightwing log window or file (lightwing\Log.txt) to see the script execution flow. Also refer to the online Scripting Guide for more details about these commands.

// Scripting Tutorial - Subroutines

version: 1.0
shadow: OpenSansBold_100    offset: 5

// Call one of seven planet pages based on the day of the week.
page: Loop
    call: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday     index: Day

// Display current date and loop to repeat 3 times.
page: Pluto    time: 1, 3    transition: Radar        image: PlanetPlutoCharonBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter    type: "Today is:"
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 70    align: CenterCenter    type: DateLong
    jump: Loop count: 3

page: Done
    jump: End

// Subroutines for each day of the week.
page: Sunday                time: 1, 3    image: PlanetMercuryBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter        type: "Sunday on Mercury!"
    return:

page: Monday                time: 1, 3    image: PlanetVenusBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter        type: "Monday on Venus!"
    return:
  
page: Tuesday                time: 1, 3    image: PlanetEarthBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter        type: "Tuesday on Earth!"
    return:
  
page: Wednesday           time: 1, 3    image: PlanetMarsBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter        type: "Wednesday on Mars!"
    return:
  
page: Thursday                time: 1, 3    image: PlanetJupiterBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter        type: "Thursday on Jupiter!"
    return:
  
page: Friday                     time: 1, 3    image: PlanetSaturnBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter        type: "Friday on Saturn!"
    return:
  
page: Saturday                time: 1, 3    image: PlanetUranusBackground.pvr
    text: OpenSansBold_100    time: 1, 2    fade: 1, 1    position: 50, 50    align: CenterCenter        type: "Saturday on Uranus!"
    return:

page: End

 

 

<   Branching                                             Introduction                                            Chaining Scripts  >