AI:AIInfo

From OpenTTD
(Difference between revisions)
Jump to: navigation, search
(More API updates)
(Settings)
Line 48: Line 48:
  
 
The new lines of course are those about Settings. In SetSetting it is important to define all properties. If not, an error will be given. Now in the GUI on your AI there will be an option to alter NameOfSetting between the values of 0 and 10, or a preset value if your have a difficulty setting different than 'Custom'.
 
The new lines of course are those about Settings. In SetSetting it is important to define all properties. If not, an error will be given. Now in the GUI on your AI there will be an option to alter NameOfSetting between the values of 0 and 10, or a preset value if your have a difficulty setting different than 'Custom'.
 +
 +
The flags parameter can be set to AICONFIG_BOOLEAN to make the setting a boolean choice.
  
 
Now inside your AI you can request the value of this setting, by doing:
 
Now inside your AI you can request the value of this setting, by doing:

Revision as of 20:12, 5 February 2009


info.nut is a file in which you tell NoAI basic things about your AI. It used to be part of main.nut, but it has been separated to make debugging easier (see here if you're interested in details).

info.nut structure

The internal structure of info.nut is very simple and follows a template presented below.

class MyNewAI extends AIInfo {
  function GetAuthor()      { return "Newbie AI Writer"; }
  function GetName()        { return "MyNewAI"; }
  function GetDescription() { return "An example AI by following the tutorial at http://wiki.openttd.org/"; }
  function GetVersion()     { return 1; }
  function GetDate()        { return "2007-03-17"; }
  function CreateInstance() { return "MyNewAI"; }
  function CanLoadFromVersion(version) {
    return (version == 1);
  }
}

/* Tell the core we are an AI */
RegisterAI(MyNewAI());

Where of course you should change the names however you like to call everything. Just don't forget the RegisterAI(), and don't forget to extend AIInfo. If something goes wrong, check if you did: RegisterAI(MyNewAI()). Mind the () after MyNewAI!


Settings

It is possible to allow users to change settings in your AI. The only requirement is that you tell them which settings. You can do this by altering your info.nut in the following way:

class MyNewAI extends AIInfo {
  function GetAuthor()      { return "Newbie AI Writer"; }
  function GetName()        { return "MyNewAI"; }
  function GetDescription() { return "An example AI by following the tutorial at http://wiki.openttd.org/"; }
  function GetVersion()     { return 1; }
  function GetDate()        { return "2007-03-17"; }
  function CreateInstance() { return "MyNewAI"; }
  function CanLoadFromVersion(version) {
    return (version == 1);
  }
  function GetSettings() {
    AddSetting({name = "NameOfSetting", description = "Description of setting", min_value = 0, max_value = 10, easy_value = 10, medium_value = 5, hard_value = 0, custom_value = 5, flags = 0});
  }
}

/* Tell the core we are an AI */
RegisterAI(MyNewAI());

The new lines of course are those about Settings. In SetSetting it is important to define all properties. If not, an error will be given. Now in the GUI on your AI there will be an option to alter NameOfSetting between the values of 0 and 10, or a preset value if your have a difficulty setting different than 'Custom'.

The flags parameter can be set to AICONFIG_BOOLEAN to make the setting a boolean choice.

Now inside your AI you can request the value of this setting, by doing:

this.GetSetting("NameOfSetting");

There are a few things to remember:

  • Settings never change during runtime. You set them before you start a game, and that is final.
  • Settings are always integers.
Personal tools