AI:Squirrel

From OpenTTD
Jump to: navigation, search


OpenTTD uses squirrel 2.2.5.

Some examples of the Squirrel markup are found on the Squirrel Language Website.

To view the core Squirrel documentation, download or view the documentation, although not all of the standard libraries are enabled.

Contents

Converted Squirrel Functions

Some of the basic functions are included from the Squirrel framework but may differ. Anything not explitly mentioned here or in the AI documentation may not work. These are:

print()

Avoid using. Rather use AILog::Info() or AILog::Warning() to send messages. They are captured nicely and printed in the AI Debug Window (as well as all normal debug outputs).

abs()

abs() returns the absolute value of an integer/float value. For example:

print("abs(-5) is: " + abs(-5));

Will print:

abs(-5) is: 5

require()

Includes a snippet of code from an other file. The path is relative, and will load the code from that file immediately. Every file needs to be compilable on its own, keep that in mind.

require("test.nut");

_cmp()

_cmp is a meta-method that allows comparison operations (<, >, <=, >=) on an class instance to be overridden. The method takes a single argument of it's parent class's type, and should return an integer describing whether the argument is less than, equal to, or greater than the instance.

class Node {
  cost = 0;

  constructor(cost) {
    this.cost = cost;
  }
}

function Node::_cmp(node) {
  if(this.cost < node.cost) return -1;
  if(this.cost > node.cost) return 1;
  return 0;
}

local a = Node(7);
local b = Node(3);
local cheapest = (a < b) ? a : b;

AILog.Info(cheapest.cost) // Will print "3"

Counter-intuitively, _cmp is NOT used for the equality operator (==). It can be used to test for equality however, by using the following trick...

local equal = nodeA <= nodeB && nodeA >= nodeB;

Development Tools

Squirrel is a scripting language so you will not need to compile an AI, however there are some tools that will help make development easier, including functions such as syntax highlighting and error checking.

Eclipse and SQDEV

Eclipse is a well known Java based IDE. SQDEV is an Eclipse plugin to facilitate Squirrel development. In order to use SQDEV with you will need Java 5 and Eclipse 3.2 or higher. Although Eclipse says it only needs a JRE (Java Runtime Environment) it's better to install a JDK (Java Development Kit) to avoid certain library errors. To install SQDEV, follow these steps.

  1. In Eclipse, select Help > Software Update > Find and Install...
  2. Select "Search for new features to install"
  3. Select "New Remote Site" and enter the following: Name: SQDev Update Site, URL: http://sqdev.sf.net/update/
  4. Make sure the new update site is checked. (Note: You might need to uncheck "Ignore features not applicable to this environment" to make it work)
  5. Click "Finish" and follow the prompts.
  6. When finished, please restart the workbench when asked.

To enable the Squirrel perspective form the main eclipse menu Window > Open Perspective > Others and select Squirrel

This information is taken from the Squirrel wiki here.

Personal tools