From The Battle for Wesnoth Wiki

This is a proposal to clean up and reorganize the existing set of Lua functions, helpers, tables, and proxy tables into a coherent, organized API, separated by category. The global wesnoth table would be the access point for the API categories, with subtables for each category within it from which the specific API item can be accessed (these subtables will essentially each represent a namespace). The idea would be to deprecate, but not immediately remove, the old access points, keeping them as valid aliases for a few versions to ensure backwards compatibility for developers and content creators and allowing them time to switch over.

The status of this document is currently an unapproved draft proposal pending discussion, consensus, and implementation. If and when these changes are implemented, LuaWML should be kept intact to (a) temporarily serve as documentation for the deprecated access points and (b) permanently describe the [lua] tag and its usage. The new "Lua API" should then be documented on its own page by the same name.

Some portions of the original proposal have already been implemented (though not always in precisely the proposed manner). These portions have been removed from this document; see the page history for more information on removed portions.

Most names will remain the same, but some will be changed for consistency (ex: anything previously referring to a "tile" now refers to a "hex"), accuracy (ex: "theme_items" has nothing to do with themes or items), or because their category makes part of their name redundant (ex: "wesnoth.get_unit" is now "wesnoth.unit.get")

Note also that the metatable functions are planned to be modified to no longer require a table to be passed in for use, and simply return a proxy table for whatever kind of metatable is being created, and as such, their names will be radically changed in this proposal to reflect their primary purpose (to get a proxy table which behaves a certain way). This also raises the question of why exactly certain data need to have a proxy returned from some creation function (such as the proxy for working with WML variables), and certain others are available through direct access to proxy tables (such as sides, game config, current, etc.), and others yes are modified through getters and/or setters despite being heavily id-based and thus prime candidates for proxy table usage (such as traits and eras). There is also the question of missing functionality and providing access to data which should be available but is not. These topics can and should be discussed and fleshed out as well, but are beyond the current scope of this document, which is to perform a one-to-one reorganization of existing functionality.

In addition, the author of this proposal is very much aware that there are likely to be a great deal of differing opinions on how such a reorganization should take place, and therefore a discussion has been opened on the forum. Specific topics that are not set in stone are:

  • Whether ALL API items should be within a category
  • Whether the wesnoth table should, indeed, be the ONLY global table in the API
  • Whether there should be a "misc" category for stuff that doesn't fit nicely anywhere else
  • The specific names and categorizations of each specific API item
  • Should the scope be expanded to drop functions and proxies which no longer make sense?
  • Should the scope be expanded to modify the behavior of existing functions or proxies to make things more consistent?
  • Pretty much anything and everything else about this proposal, indeed, all the way down to "should we even DO this at all?"

The categories and names being proposed are as follows:


APIs for manipulation of strings

Old Access Point New Access Point Comments
wesnoth.textdomain wesnoth.string.textdomain


APIs for the output of text messages

Old Access Point New Access Point Comments
helper.wml_error wesnoth.output.wml_error
wesnoth.message wesnoth.output.message
wesnoth.clear_messages wesnoth.output.clear_messages
wesnoth.log wesnoth.output.log


APIs relating to sound

Old Access Point New Access Point Comments
wesnoth.set_music wesnoth.sound.set_music or
wesnoth.add_sound_source wesnoth.sound.add_source
wesnoth.remove_sound_source wesnoth.sound.remove_source
wesnoth.get_sound_source wesnoth.sound.get_source


APIs for working with the game map

Old Access Point New Access Point Comments


APIs for working with files

Old Access Point New Access Point Comments
wesnoth.dofile Should this remain as "dofile", which is the standard name used by lua?
wesnoth.require wesnoth.file.require
wesnoth.get_image_size wesnoth.file.image_size
wesnoth.have_file wesnoth.file.exists


APIs for working with game states and data

Old Access Point New Access Point Comments
wesnoth.get_era Should this be somewhere else? If so, where?
wesnoth.set_next_scenario Should this be somewhere else? If so, where?
wesnoth.name_generator Should this be somewhere else? If so, where?
wesnoth.compare_versions Should this be somewhere else? If so, where?
wesnoth.get_time_stamp Should this be somewhere else? If so, where?
wesnoth.get_terrain_info Should this be in map?
wesnoth.simulate_combat Should this be in unit?
wesnoth.get_traits Should this be in unit?
wesnoth.races Should this be somewhere else? If so, where?
wesnoth.game_events Should this be somewhere else? If so, where?


APIs for working with Wesnoth Formula Language

Old Access Point New Access Point Comments
wesnoth.eval_formula wesnoth.formula.evaluate
wesnoth.compile_formula wesnoth.formula.compile


Mathematical APIs

Old Access Point New Access Point Comments
wesnoth.random wesnoth.math.random
helper.rand wesnoth.math.randvar
helper.round wesnoth.math.round
helper.shuffle wesnoth.math.shuffle


APIs for working with WML actions

Old Access Point New Access Point Comments wesnoth.action.execute
wesnoth.wml_actions wesnoth.action.tags
helper.set_wml_action_metatable wesnoth.action.get_execution_table


APIs for working with WML events

Old Access Point New Access Point Comments
wesnoth.fire_event_by_id wesnoth.event.fire_by_id
wesnoth.add_event_handler wesnoth.event.add_handler
wesnoth.remove_event_handler wesnoth.event.remove_handler


APIs for working with WML conditionals

Old Access Point New Access Point Comments
wesnoth.wml_conditionals wesnoth.conditional.tags
wesnoth.eval_conditional wesnoth.conditional.evaluate


APIs for working with WML effects

Old Access Point New Access Point Comments
wesnoth.effects wesnoth.effect.types
This page was last edited on 6 December 2019, at 05:09.