Addonstructure

From The Battle for Wesnoth Wiki


In order to create custom content for Wesnoth, you need to set up a folder in userdata/data/add-ons/. If you have not found your userdata directory yet, read EditingWesnoth and come back here. This page will explain the basic directory structure of an add-on and where stuff goes.

Several pages on this wiki will assume you have done so and refer to relative paths in such.

The Main File

For this example, we will assume you are creating an add-on called A Simple Addon.

  • Create the empty directory userdata/data/add-ons/A_Simple_Addon.

Important: Remember to use underscores or hyphens in all directory and file names instead of spaces. Failure to do so will result in your content not working properly.

  • Create a file called _main.cfg inside the A_Simple_Addon folder. Without this file, the game will not even recognize that your add-on exists.

The _main.cfg file will instruct the game on how to load your content. Here is shown a very simple example of the file.

#textdomain wesnoth-A_Simple_Addon
[textdomain]
    name="wesnoth-A_Simple_Addon"
    path="data/add-ons/A_Simple_Addon/translations"
[/textdomain]

#ifdef MULTIPLAYER
[binary_path]
    path=data/add-ons/A_Simple_Addon
[/binary_path]

{~add-ons/A_Simple_Addon/scenarios}
#endif

The [textdomain] WML tag specifies where the game should look for translations to the strings in the add-on. The textdomain should be the add-on's directory name prefixed with "wesnoth-", to ensure that it does not conflict with other textdomains that might be specified on a given system and is compatible with WesCamp.

Note: A textdomain is only required if your add-on contains text meant to be translated. In the case of, for example, a music pack, no textdomain is needed.

All tags save for [campaign] and [textdomain] must be enclosed in an #ifdef preprocessor conditional - including those substituted through inclusion (see PreprocessorRef for more information). This is to make sure your content only loads at the correct time and does not conflict with other add-ons. In the example above, the scenarios will be loaded when you enter multiplayer mode. In a campaign, you can set up a specific flag to allow your content to only load when that campaign is played.

Note: Only code must be included from this file. You do not need to include binary content (such as sounds, images, or music). Those may be referred to simply by path relative to the one specified in [binary_path]. Also, including the units directory cannot be done by a simple path inclusion; it also must be wrapped in a [units][/units] tag pair, as such:

[units]
    {~add-ons/A_Simple_Addon/units}
[/units]

The Directory Structure

What to create next depend on what type of content you are creating. For example, campaigns or map packs will have scenarios and maps directories, while a music pack would have only music. Here we will assume you are creating a campaign or single-scenario add-on.

Create the following directories:

  • userdata/data/add-ons/A_Simple_Addon/scenarios
  • userdata/data/add-ons/A_Simple_Addon/maps

All map files used in scenarios go in maps (see BuildingMaps. All configuration (‘.cfg’) files for scenarios go in scenarios (see BuildingScenarios).

If you have additional custom content such as images, sounds or music, it's necessary to create additional directories. Even for other custom content, creating directories to organise it is recommended.

Directory structure defined by the engine

You may name the directories containing code (such as scenarios and macros) anything you like, but the binary content directories listed below must be named as such and be relative to your [binary_path]. This is because when trying to resolve an image path, Wesnoth will look under images/, and likewise for sounds and music, in as explained in the BinaryPathWML page.

  • userdata/data/add-ons/A_Simple_Addon/images
  • userdata/data/add-ons/A_Simple_Addon/music
  • userdata/data/add-ons/A_Simple_Addon/sounds

Some subdirectories of images are also supported by the engine:

  • userdata/data/add-ons/A_Simple_Addon/images/attacks Icons for each attack, see UnitTypeWML#Attacks's documentation for icon
  • userdata/data/add-ons/A_Simple_Addon/images/terrain Terrain graphics

While Lua can be embedded in WML files, the following directory is supported for loading them via Lua's wesnoth.require "module_name".

  • userdata/data/add-ons/A_Simple_Addon/lua

If you set up a textdomain, create a translations directory, even if it remains empty. Failure to do so generates a warning in stderr. This path must be the same as specified in the [textdomain] tag.

  • userdata/data/add-ons/A_Simple_Addon/translations

Directory structure defined by convention

The names of these directories are not prescribed by the engine, but the current "best practice" is:

  • userdata/data/add-ons/A_Simple_Addon/ai Lua files for the RCA_AI
  • userdata/data/add-ons/A_Simple_Addon/masks files for TerrainMaskWML
  • userdata/data/add-ons/A_Simple_Addon/units
  • userdata/data/add-ons/A_Simple_Addon/utils containing WML macros

The "images" directory is prescribed by the engine, but can also use subdirectories:

  • userdata/data/add-ons/A_Simple_Addon/images/halo for [halo_frame], etc
  • userdata/data/add-ons/A_Simple_Addon/images/items
  • userdata/data/add-ons/A_Simple_Addon/images/portraits for [unit_type]profile=
  • userdata/data/add-ons/A_Simple_Addon/images/story backgrounds for [story] screens
  • userdata/data/add-ons/A_Simple_Addon/images/units for [unit_type]image=, etc

Finally, the customary fallback for not putting stuff in the parent directory:

  • userdata/data/add-ons/A_Simple_Addon/images/misc

See also

This page was last edited on 8 November 2020, at 22:51.