Announcement

Collapse
No announcement yet.

For Loops, Tables, and Setup Factory 9

Collapse
This is a sticky topic.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • For Loops, Tables, and Setup Factory 9

    Since there have been a few posts about For loops in Setup Factory 9.0 here is a quick snippet from the help file that should help anyone having problems iterating over tables using for loops:

    Setup Factory now contains an updated version of the Lua scripting library, version 5.1 (previously 5.0). Setup Factory's version has been compiled to try to maintain compatibility, however there are some minor syntax changes/deprecations made to the language that you need to be aware of for future development as well as previously created projects and Lua scripts.

    When importing a project made with an earlier version of Setup Factory, Setup Factory 9 will automatically analyze your project file and create a report detailing any areas where it thinks the old style for loop is in use. If there are any old for loops found the report will automatically be saved in the same folder as your new 9 project file and opened in your default web-browser. We recommend analyzing each line of code found in the report to ensure that you only use the new for loop syntax. If your project uses external, custom Lua script files, they are not automatically analyzed - make sure that they conform to the new syntax.

    Table Iterating For Loops

    The syntax for iterating over a table using a for loop has changed (has been deprecated). If the new syntax is not used, a runtime error will be thrown when executed.

    Old syntax:

    Code:
    mytable [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{[/COLOR][COLOR="#800080"]"One"[/COLOR][COLOR="#FF0000"],[/COLOR][COLOR="#800080"]"Two"[/COLOR][COLOR="#FF0000"],[/COLOR][COLOR="#800080"]"Three"[/COLOR][COLOR="#FF0000"]}[/COLOR];
    
    [COLOR="#0000FF"]for[/COLOR] k[COLOR="#FF0000"],[/COLOR] v [COLOR="#0000FF"]in[/COLOR] mytable [COLOR="#0000FF"]do[/COLOR]
        Dialog[COLOR="#FF0000"].[/COLOR]Message([COLOR="#800080"]"Table Item"[/COLOR][COLOR="#FF0000"],[/COLOR] k [COLOR="#FF0000"]..[/COLOR] [COLOR="#800080"]"="[/COLOR] [COLOR="#FF0000"]..[/COLOR] v);
    [COLOR="#0000FF"]end[/COLOR] [COLOR="#008000"]-- for[/COLOR]
    Needs to be changed to:

    Code:
    mytable [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{[/COLOR][COLOR="#800080"]"One"[/COLOR][COLOR="#FF0000"],[/COLOR][COLOR="#800080"]"Two"[/COLOR][COLOR="#FF0000"],[/COLOR][COLOR="#800080"]"Three"[/COLOR][COLOR="#FF0000"]}[/COLOR];
    
    [COLOR="#0000FF"]for[/COLOR] k[COLOR="#FF0000"],[/COLOR] v [COLOR="#0000FF"]in[/COLOR] [highlight][COLOR="#0000FF"]pairs[/COLOR](mytable)[/highlight] [COLOR="#0000FF"]do[/COLOR]
        Dialog[COLOR="#FF0000"].[/COLOR]Message([COLOR="#800080"]"Table Item"[/COLOR][COLOR="#FF0000"],[/COLOR] k [COLOR="#FF0000"]..[/COLOR] [COLOR="#800080"]"="[/COLOR] [COLOR="#FF0000"]..[/COLOR] v);
    [COLOR="#0000FF"]end[/COLOR] [COLOR="#008000"]-- for[/COLOR]
    The functions table.foreach and table.foreachi are also deprecated. You can use a for loop as above with pairs or ipairs instead. While these two are deprecated, they still work (unlike the example above), however you are advised to also rework these types of loops in case they are removed from future versions.

    If you commonly use Lua functions (non-Setup Factory actions) in your scripts, you may also want to review the list of other changes to the language in case other changes affect you. You can find this information in the Lua 5.1 Reference Manual found on http://www.lua.org, specifically the Changes in the Language section.

    Remember if you are going to iterate over a table in Setup Factory 9 you will generally need to use the pairs function or less commonly the ipairs function.
Working...
X