Helpful Tips

"Whitespace" is a term programmers use to describe things like blank lines, tab characters and spaces that are used to improve the readability of their code. You can use whitespace to make your AutoPlay scripts easier to read.

Unless they are contained within a string, whitespace characters (like spaces and tabs) are completely ignored by the AutoPlay application at run time. Their only purpose is to allow you to separate and format your actions a little and make your scripts easier to read.

For example, separating groups of actions with one or more blank lines makes it easier to recognize that the actions are meant to work together as a group. This is especially useful when combined with comments...a single comment can explain an entire group actions this way. Take a look at the following script:

Debug.ShowWindow(true);
file = _TempFolder .. "\\MyTempFile.tmp";
-- convert that path into its various parts
path_parts = String.SplitPath(file);
-- display the parts on the debug window
Debug.Print("Drive: " .. path_parts.Drive .. "\r\n");
Debug.Print("Folder: " .. path_parts.Folder .. "\r\n");
Debug.Print("Filename: " .. path_parts.Filename .. "\r\n");
Debug.Print("Extension: " .. path_parts.Extension .. "\r\n");
-- get the file's current attributes
attrib = File.GetAttributes( file );
-- override a couple of the values in the table
attrib.ReadOnly = true;
attrib.Hidden = false;
-- set the modified attributes back to the file
File.SetAttributes( file, attrib );

A bit hard to read, isn't it?

Now here's the same script with a few blank lines added:

Debug.ShowWindow(true);

file = _TempFolder .. "\\MyTempFile.tmp";

-- convert that path into its various parts
path_parts = String.SplitPath(file);

-- display the parts on the debug window
Debug.Print("Drive: " .. path_parts.Drive .. "\r\n");
Debug.Print("Folder: " .. path_parts.Folder .. "\r\n");
Debug.Print("Filename: " .. path_parts.Filename .. "\r\n");
Debug.Print("Extension: " .. path_parts.Extension .. "\r\n");

-- get the file's current attributes
attrib = File.GetAttributes( file );

-- override a couple of the values in the table
attrib.ReadOnly = true;
attrib.Hidden = false;

-- set the modified attributes back to the file
File.SetAttributes( file, attrib );

The whitespace makes it much easier to read.

Block Indentation

Another common use of whitespace is to make script blocks easier to see.

A block is a section of script that is controlled or affected by a control structure, such as an if statement or a while loop. For example, in an if statement, the block consists of the lines of script "inside" the if statement, that are only executed if that if statement's test is found to be true:

if(foo == true) then
    -- tell the user foo is true
    Dialog.Message("Hey User", "foo is true!");
end

In the example above, the two indented lines form a script "block." Technically, a block doesn't have to be indented, but it's common practice to do so, and it really makes it easier to see the part that will be affected by the if statement in the script.

AutoPlay's script editor makes it easy to add this kind of indentation to your scripts. To indent your first line in a block, just press the tab key at the start of the line. This will move the cursor over by one "level" of indentation to the right. Then continue adding lines to the block. Whenever you press Enter on a line that is indented, the editor automatically positions your cursor at the same level of indentation on the next line.

Tip: You can even indent or unindent multiple lines at once, by selecting the lines and pressing Tab (to increase the level of indentation) or pressing Shift+Tab (to decrease the level of indentation).

Indentation is especially important when there are multiple levels involved. For example, here is a short script that has a nested for loop (i.e. one for loop inside another) and a bunch of if statements. First let's see the script with no indentation:

Debug.ShowWindow(true);
if bCount then
Debug.Print("\r\nCounting to a hundred five times...\r\n");
for j = 5, 1, -1 do
if(j == 1) then
Debug.Print("\r\n\r\nThis is the last time!\r\n");
else
Debug.Print("\r\n\r\n" .. j - 1 .. " more times to go!");
end
for i = 1, 100 do
Debug.Print(i.."\r\n");
if count == 50 then
Debug.Print("We're half way to a hundred!\r\n")
end
end
end
end

Now here's the script with standard block formatting:

Debug.ShowWindow(true);
if bCount then
    Debug.Print("\r\nCounting to a hundred five times...\r\n");
    for j = 5, 1, -1 do
        if(j == 1) then
            Debug.Print("\r\n\r\nThis is the last time!\r\n");
        else
            Debug.Print("\r\n\r\n" .. j - 1 .. " more times to go!");
        end
        for i = 1, 100 do
            Debug.Print(i.."\r\n");
            if count == 50 then
                Debug.Print("We're half way to a hundred!\r\n")
            end
        end
    end
end

See how the indentation makes it easier to see the different blocks?

Here's another example from an On Click script used in a tic-tac-toe application. First with no indentation:

Hotspot.SetEnabled("5", false);
if (player == "o") then
Label.SetVisible("o5", true);
pos5 = "o";
player = "x";
skip = "true";
if (pos1 == "o" and pos9 == "o") then
Dialog.Message("Game Over", "O Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
if (pos7 == "o" and pos3 == "o") then
Dialog.Message("Game Over", "O Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
if (pos2 == "o" and pos8 == "o") then
Dialog.Message("Game Over", "O Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
if (pos4 == "o" and pos6 == "o") then
Dialog.Message("Game Over", "O Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
end
if (player == "x" and skip ~= "true") then
Label.SetVisible("x5", true);
pos5 = "x";
player = "o";
if (pos1 == "x" and pos9 == "x") then
Dialog.Message("Game Over", "X Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
if (pos7 == "x" and pos3 == "x") then
Dialog.Message("Game Over", "X Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
if (pos2 == "x" and pos8 == "x") then
Dialog.Message("Game Over", "X Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
if (pos4 == "x" and pos6 == "x") then
Dialog.Message("Game Over", "X Wins! Click to restart");
clicked = 0;
Page.Jump("Page1");
end
end
skip = "false";
clicked = clicked + 1;
if (clicked == 9) then
result = Dialog.Message("Game Over", "Cat's game (tie) Click to restart", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
Page.Jump("Page1");
end

Now, the same script with some whitespace added:

Hotspot.SetEnabled("5", false);

if (player == "o") then
    Label.SetVisible("o5", true);
    pos5 = "o";
    player = "x";
    skip = "true";
    if (pos1 == "o" and pos9 == "o") then
        Dialog.Message("Game Over", "O Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
    if (pos7 == "o" and pos3 == "o") then
        Dialog.Message("Game Over", "O Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
    if (pos2 == "o" and pos8 == "o") then
        Dialog.Message("Game Over", "O Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
    if (pos4 == "o" and pos6 == "o") then
        Dialog.Message("Game Over", "O Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
end

if (player == "x" and skip ~= "true") then
    Label.SetVisible("x5", true);
    pos5 = "x";
    player = "o";
    if (pos1 == "x" and pos9 == "x") then
        Dialog.Message("Game Over", "X Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
    if (pos7 == "x" and pos3 == "x") then
        Dialog.Message("Game Over", "X Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
    if (pos2 == "x" and pos8 == "x") then
        Dialog.Message("Game Over", "X Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
    if (pos4 == "x" and pos6 == "x") then
        Dialog.Message("Game Over", "X Wins! Click to restart");
        clicked = 0;
        Page.Jump("Page1");
    end
end

skip = "false";
clicked = clicked + 1;
if (clicked == 9) then
    result = Dialog.Message("Game Over", "Cat's game (tie) Click to restart", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
    Page.Jump("Page1");
end

The second version is much easier to follow.