Last Error
NoAI Framework

NoAI Main Page

Development

Development milestones
Suggested API changes

AI Programming

API Documentation
Introduction
info.nut file
main.nut file
Basics
Using the road pathfinder
Using the rail pathfinder
Saving / Loading data
Things you need to know
Squirrel
Home page
Common mistakes
Lists
Coping with OTTD errors
Trams
AI Libraries
Forum
Forum FAQ
AI Behavior
ShortNames in use

AIs

TestAI
WrightAI
Convoy
SimpleAI
Comparison of AIs
See forum
See BaNaNaS
All articles in NoAI category

OpenTTD is a realtime game, so no program can predict whether a certain planned action will fail or succeed until it has been executed. When letting AIs build bridges, vehicles, reroute aircraft chances are some of these commands will fail due to some reason or another. Aircraft can crash, more money may be required and other opponents might interfere with its plans. As of now NoAI can report whether a command has been executed successfully, however, if a command has failed it can't report why it has failed. This page is both a manual for AI writers as it provides coding guidelines for developers who want to help updating this feature and fixing bugs.

Code

Every error code in OpenTTD has its own ID (StringID) and is used to translate the error into one of the various languages to notify the user through a message on the GUI. In order to translate these errors we have to translate these StringIDs into error messages and expose them through the NoAI API. The last error message can be acquired by calling AIError.GetLastError(), and error messages can be checked by:

if (!AIAirport.BuildAirport(...)) {
  switch(AIError.GetLastError()) {
    case AIError.ERR_NOT_ENOUGH_CASH: // Get some more change
      ...
      break;
    case AIError.ERR_LOCAL_AUTHORITY_REFUSES: // Bribe!
      ...
      break;
    case AIError.ERR_LAND_SLOPED_WRONG: // Do some terraforming
      ...
      break;
    ...
    default:
      AILog.Warning("Unhandled exception: " + AIError.GetLastErrorString());
      break;
  }
}

Internally error messages are mapped to both enum constants (like AIError.ERR_NOT_ENOUGH_CASH) and a string representation. The string representation can be acquired by calling AIError.GetLastErrorString and will match with the name of the enum constant, note that this is meant to be used for debug purposes only! The API documentation lists all errors per method that can occur by invoking it. There are however some exceptions, the following errors will not be listed as they apply to (nearly) all methods:

The meaning of the other error messages can be looked up in the API.