Skip to content
LuisAntonRebollo edited this page Dec 4, 2013 · 1 revision
<SCRIPT SRC="../../../include/tutorial.js" LANGUAGE="JavaScript"></SCRIPT> <SCRIPT SRC="../../../include/prototype.js" LANGUAGE="JavaScript"></SCRIPT> <SCRIPT SRC="../../../include/scriptaculous.js" LANGUAGE="JavaScript"></SCRIPT> <SCRIPT SRC="../../../include/glossaryLookUp.js" LANGUAGE="JavaScript"></SCRIPT> <SCRIPT SRC="../../../include/referenceLookUp.js" LANGUAGE="JavaScript"></SCRIPT> <SCRIPT SRC="../../../include/component.js" LANGUAGE="JavaScript"></SCRIPT> <SCRIPT SRC="../../../include/componentContainer.js" LANGUAGE="JavaScript"></SCRIPT> <SCRIPT>DocImagePath = "../../../";</SCRIPT> <script> // this script chunk is to update the ToC to the current doc and expand it pageID = 67; parent.leftFrame.expandToItem('tree2', 'doc67'); var element = parent.leftFrame.document.getElementById('doc67'); if((element) && (element.className==parent.leftFrame.nodeClosedClass)) { element.className = parent.leftFrame.nodeOpenClass } ; </script> <title>Torque 3D - Echo Examples</title>
    <table border="0" cellpadding="0" cellspacing="0" width="700">
      <tbody>
        <tr>
          <td width="700"><table id="toc" summary="Contents">
              <tbody>
                <tr>
                  <td><div id="toctitle">
                      <h2>Contents</h2>
                      
                    <ul>
                      <li class="toclevel-1"><a href="#Syntax_Review"><span class="tocnumber">1</span> <span class="toctext">Syntax Review</span></a></li>
                      <li class="toclevel-1"><a href="#Example"><span class="tocnumber">2</span> <span class="toctext">Example</span></a></li>
                      <li class="toclevel-1"><a href="#Creating_the_Script"><span class="tocnumber">3</span> <span class="toctext">Creating the Script</span></a></li>
                      <li class="toclevel-1"><a href="#Testing_the_Script"><span class="tocnumber">4</span> <span class="toctext">Testing the Script</span></a></li>
                      <li class="toclevel-1"><a href="#Conclusion"><span class="tocnumber">5</span> <span class="toctext">Conclusion</span></a></li>
                    </ul></td>
                </tr>
              </tbody>
            </table>
            <a name="Syntax_Review" id="Syntax_Review"></a>
            <h2> <span class="mw-headline">Syntax Review</span></h2>
            <p>The <strong>echo(...)</strong> method syntax is syntax is easy to memorize and is a extremely valuable debugging command:</p>
            <p><br />
              <b>Syntax:</b> </p>
            <table border="1" cellpadding="6" cellspacing="0" width="700">
              <tbody>
                <tr>
                  <td bgcolor="#6699ff" width="280"><b> echo(string,all)</b></td>
                </tr>
                <tr>
                  <td align="left" bgcolor="#eeeeee" valign="top" width="226"><p>Sends output to the console<br />
                      <br />
                      <i><b>Syntax</b></i><br />
                    </p>
                    <p>echo(string <b>text</b>, all <b>[...]</b>);<br />
                    </p>
                    <ul>
                      <li><b>text</b>: Text sent to console<br />
                      </li>
                      <li><b>[...]</b>: Optional value, of any type, that will be appended to the text<br />
                      </li>
                    </ul>
                    <p><br />
                      <b>Returns</b><br />
                      No return value.<br />
                      <br />
                      <i><b>Examples</b></i><br />
                    </p>
                    <pre>// Print "Hello World" in the console

echo("Hello World");



Example

Start by running a Torque 3D project. Once the game is up, open the console by pressing the tilde (~) key. In the console, type the following:


Example 1

echo("Hello World");

OUTPUT: Hello World


Now, let's make use of the second parameter. Passing in a value for the second argument will append it to your text:


Example 2

echo("Hello World", 3);

OUTPUT: Hello World3


Notice how there is no space between World and 3. The optional text is appended exactly how you type it. If you want, you can include your own white space to format the output:


Example 3

echo("Hello World: ", 5);

OUTPUT: Hello World: 5


As you can see, the colon and space are included in the output. 5 is still appended, but does not ignore the whitespace. In addition to echo(...), there are two other output functions you will find useful. Their syntax and functionality are nearly identical to echo, but the output is different.


The two functions I'm referring to are warn(...) and error(...). You can post a message in the console and log the same way you echo:


Example 4

warn("Be careful. Something bad might happen");

error("Something has gone horribly wrong");

OUTPUT: Be careful. Something bad might happen (teal color) Something has gone horribly wrong (red color)


You can use these functions to output multicolored text to the console, which will help you identify problems with your scripts.



Creating the Script

There is no real reason to have a script full of echo statements. You will want to use echo(...) while debugging your other functions. However, as an example, you can create a script consisting only of output statements.


First, we need to create a new script:

  1. Navigate to your project's game/scripts/client directory.
  2. Create a new script file called "Output.cs". In Torsion, right click on the directory, click the "New Script" option, then name your script. On Windows or OS X, create a new text file and change the extension to .cs
  3. Open your new script using a text editor or Torsion.


Add the following code to the script:


Output.cs

//-----------------------------------------------------------------------------
// Torque 3D
// Copyright (C) GarageGames.com 2000 - 2009 All Rights Reserved
//-----------------------------------------------------------------------------

// Create a nice border effect around these echos, makes it easier to find echo(""); echo("");

// Standard use echo("Hello"); echo("World"); echo("Hello World");

// With escape commands echo("H\ne\nl\nl\no\nW\no\nr\nl\nd\n");

// Appending echo("Hello World", 1); echo("Hello World ", 2); echo("Hello World: ", 3);

// Warning warn("Warning! Watch for teal text");

// Error error("Something has gone horribly wrong"); echo(""); echo("");


Save the script now.



Testing the Script

Open game/scripts/client/init.cs and locate the initClient() function. At the end of that function, execute your new script by typing the following:

exec("./Output.cs");


Run your game, then open the console by pressing tilde (~). Look for the long string of asterisks (*****), and you will find your echo statements. Note: you may need to scroll up to find the echo statements.



Conclusion

Use echo(...), warn(...), and error(...) as often as you can. They can be very helpful when debugging your scripts. The rest of the TorqueScript documentation will use these functions to demonstrate functionality and give you cues on how things are being run.


To download this script example, CLICK HERE.

              </td>
        </tr>
      </tbody>
    </table>
Clone this wiki locally