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 TrueUpdate scripts easier to read.

Unless they are contained within a string, whitespace characters (like spaces and tabs) are completely ignored by the update 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.

TrueUpdate's action 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
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
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
        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
            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?