2
0

menu_lua_api.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. Minetest Lua Mainmenu API Reference 5.7.0
  2. =========================================
  3. Introduction
  4. -------------
  5. The main menu is defined as a formspec by Lua in `builtin/mainmenu/`
  6. Description of formspec language to show your menu is in `lua_api.txt`
  7. Callbacks
  8. ---------
  9. * `core.button_handler(fields)`: called when a button is pressed.
  10. * `fields` = `{name1 = value1, name2 = value2, ...}`
  11. * `core.event_handler(event)`
  12. * `event`: `"MenuQuit"`, `"KeyEnter"`, `"ExitButton"` or `"EditBoxEnter"`
  13. Gamedata
  14. --------
  15. The "gamedata" table is read when calling `core.start()`. It should contain:
  16. {
  17. playername = <name>,
  18. password = <password>,
  19. address = <IP/address>,
  20. port = <port>,
  21. selected_world = <index>, -- 0 for client mode
  22. singleplayer = <true/false>,
  23. }
  24. Functions
  25. ---------
  26. * `core.start()`
  27. * `core.close()`
  28. * `core.get_min_supp_proto()`
  29. * returns the minimum supported network protocol version
  30. * `core.get_max_supp_proto()`
  31. * returns the maximum supported network protocol version
  32. * `core.open_url(url)`
  33. * opens the URL in a web browser, returns false on failure.
  34. * Must begin with http:// or https://
  35. * `core.open_dir(path)`
  36. * opens the path in the system file browser/explorer, returns false on failure.
  37. * Must be an existing directory.
  38. * `core.share_file(path)`
  39. * Android only. Shares file using the share popup
  40. * `core.get_version()` (possible in async calls)
  41. * returns current core version
  42. Filesystem
  43. ----------
  44. * `core.get_builtin_path()`
  45. * returns path to builtin root
  46. * `core.create_dir(absolute_path)` (possible in async calls)
  47. * `absolute_path` to directory to create (needs to be absolute)
  48. * returns true/false
  49. * `core.delete_dir(absolute_path)` (possible in async calls)
  50. * `absolute_path` to directory to delete (needs to be absolute)
  51. * returns true/false
  52. * `core.copy_dir(source,destination,keep_source)` (possible in async calls)
  53. * `source` folder
  54. * `destination` folder
  55. * `keep_source` DEFAULT true --> if set to false `source` is deleted after copying
  56. * returns true/false
  57. * `core.is_dir(path)` (possible in async calls)
  58. * returns true if `path` is a valid dir
  59. * `core.extract_zip(zipfile,destination)` [unzip within path required]
  60. * `zipfile` to extract
  61. * `destination` folder to extract to
  62. * returns true/false
  63. * `core.sound_play(spec, looped)` -> handle
  64. * `spec` = `SimpleSoundSpec` (see `lua_api.txt`)
  65. * `looped` = bool
  66. * `core.sound_stop(handle)`
  67. * `core.get_video_drivers()`
  68. * get list of video drivers supported by engine (not all modes are guaranteed to work)
  69. * returns list of available video drivers' settings name and 'friendly' display name
  70. e.g. `{ {name="opengl", friendly_name="OpenGL"}, {name="software", friendly_name="Software Renderer"} }`
  71. * first element of returned list is guaranteed to be the NULL driver
  72. * `core.get_mapgen_names([include_hidden=false])` -> table of map generator algorithms
  73. registered in the core (possible in async calls)
  74. * `core.get_cache_path()` -> path of cache
  75. * `core.get_temp_path([param])` (possible in async calls)
  76. * `param`=true: returns path to a temporary file
  77. otherwise: returns path to the temporary folder
  78. HTTP Requests
  79. -------------
  80. * `core.download_file(url, target)` (possible in async calls)
  81. * `url` to download, and `target` to store to
  82. * returns true/false
  83. * `minetest.get_http_api()` (possible in async calls)
  84. * returns `HTTPApiTable` containing http functions.
  85. * The returned table contains the functions `fetch_sync`, `fetch_async` and
  86. `fetch_async_get` described below.
  87. * Function only exists if minetest server was built with cURL support.
  88. * `HTTPApiTable.fetch_sync(HTTPRequest req)`: returns HTTPRequestResult
  89. * Performs given request synchronously
  90. * `HTTPApiTable.fetch_async(HTTPRequest req)`: returns handle
  91. * Performs given request asynchronously and returns handle for
  92. `HTTPApiTable.fetch_async_get`
  93. * `HTTPApiTable.fetch_async_get(handle)`: returns `HTTPRequestResult`
  94. * Return response data for given asynchronous HTTP request
  95. ### `HTTPRequest` definition
  96. Used by `HTTPApiTable.fetch` and `HTTPApiTable.fetch_async`.
  97. {
  98. url = "http://example.org",
  99. timeout = 10,
  100. -- Timeout for connection in seconds. Default is 3 seconds.
  101. post_data = "Raw POST request data string" OR {field1 = "data1", field2 = "data2"},
  102. -- Optional, if specified a POST request with post_data is performed.
  103. -- Accepts both a string and a table. If a table is specified, encodes
  104. -- table as x-www-form-urlencoded key-value pairs.
  105. -- If post_data is not specified, a GET request is performed instead.
  106. user_agent = "ExampleUserAgent",
  107. -- Optional, if specified replaces the default minetest user agent with
  108. -- given string
  109. extra_headers = { "Accept-Language: en-us", "Accept-Charset: utf-8" },
  110. -- Optional, if specified adds additional headers to the HTTP request.
  111. -- You must make sure that the header strings follow HTTP specification
  112. -- ("Key: Value").
  113. multipart = boolean
  114. -- Optional, if true performs a multipart HTTP request.
  115. -- Default is false.
  116. }
  117. ### `HTTPRequestResult` definition
  118. Passed to `HTTPApiTable.fetch` callback. Returned by
  119. `HTTPApiTable.fetch_async_get`.
  120. {
  121. completed = true,
  122. -- If true, the request has finished (either succeeded, failed or timed
  123. -- out)
  124. succeeded = true,
  125. -- If true, the request was successful
  126. timeout = false,
  127. -- If true, the request timed out
  128. code = 200,
  129. -- HTTP status code
  130. data = "response"
  131. }
  132. Formspec
  133. --------
  134. * `core.update_formspec(formspec)`
  135. * `core.get_table_index(tablename)` -> index
  136. * can also handle textlists
  137. * `core.formspec_escape(string)` -> string
  138. * escapes characters [ ] \ , ; that cannot be used in formspecs
  139. * `core.explode_table_event(string)` -> table
  140. * returns e.g. `{type="CHG", row=1, column=2}`
  141. * `type`: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
  142. * `core.explode_textlist_event(string)` -> table
  143. * returns e.g. `{type="CHG", index=1}`
  144. * `type`: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
  145. * `core.set_formspec_prepend(formspec)`
  146. * `formspec`: string to be added to every mainmenu formspec, to be used for theming.
  147. GUI
  148. ---
  149. * `core.set_background(type,texturepath,[tile],[minsize])`
  150. * `type`: "background", "overlay", "header" or "footer"
  151. * `tile`: tile the image instead of scaling (background only)
  152. * `minsize`: minimum tile size, images are scaled to at least this size prior
  153. doing tiling (background only)
  154. * `core.set_clouds(<true/false>)`
  155. * `core.set_topleft_text(text)`
  156. * `core.show_keys_menu()`
  157. * `core.show_path_select_dialog(formname, caption, is_file_select)`
  158. * shows a path select dialog
  159. * `formname` is base name of dialog response returned in fields
  160. - if dialog was accepted `"_accepted"`
  161. will be added to fieldname containing the path
  162. - if dialog was canceled `"_cancelled"`
  163. will be added to fieldname value is set to formname itself
  164. * if `is_file_select` is `true`, a file and not a folder will be selected
  165. * returns nil or selected file/folder
  166. * `core.get_screen_info()`
  167. * returns
  168. {
  169. density = <screen density 0.75,1.0,2.0,3.0 ... (dpi)>,
  170. display_width = <width of display>,
  171. display_height = <height of display>,
  172. window_width = <current window width>,
  173. window_height = <current window height>,
  174. render_info = <active render information>
  175. }
  176. Content and Packages
  177. --------------------
  178. Content - an installed mod, modpack, game, or texture pack (txt)
  179. Package - content which is downloadable from the content db, may or may not be installed.
  180. * `core.get_user_path()` (possible in async calls)
  181. * returns path to global user data,
  182. the directory that contains user-provided mods, worlds, games, and texture packs.
  183. * `core.get_modpath()` (possible in async calls)
  184. * returns path to global modpath in the user path, where mods can be installed
  185. * `core.get_modpaths()` (possible in async calls)
  186. * returns table of virtual path to global modpaths, where mods have been installed
  187. The difference with `core.get_modpath` is that no mods should be installed in these
  188. directories by Minetest -- they might be read-only.
  189. Ex:
  190. ```
  191. {
  192. mods = "/home/user/.minetest/mods",
  193. share = "/usr/share/minetest/mods",
  194. -- Custom dirs can be specified by the MINETEST_MOD_DIR env variable
  195. ["/path/to/custom/dir"] = "/path/to/custom/dir",
  196. }
  197. ```
  198. * `core.get_clientmodpath()` (possible in async calls)
  199. * returns path to global client-side modpath
  200. * `core.get_gamepath()` (possible in async calls)
  201. * returns path to global gamepath
  202. * `core.get_texturepath()` (possible in async calls)
  203. * returns path to default textures
  204. * `core.get_games()` -> table of all games (possible in async calls)
  205. * `name` in return value is deprecated, use `title` instead.
  206. * returns a table (ipairs) with values:
  207. {
  208. id = <id>,
  209. path = <full path to game>,
  210. gamemods_path = <path>,
  211. title = <title of game>,
  212. menuicon_path = <full path to menuicon>,
  213. author = "author",
  214. DEPRECATED:
  215. addon_mods_paths = {[1] = <path>,},
  216. }
  217. * `core.get_content_info(path)`
  218. * returns
  219. {
  220. name = "technical_id",
  221. type = "mod" or "modpack" or "game" or "txp",
  222. title = "Human readable title",
  223. description = "description",
  224. author = "author",
  225. path = "path/to/content",
  226. depends = {"mod", "names"}, -- mods only
  227. optional_depends = {"mod", "names"}, -- mods only
  228. }
  229. * `core.check_mod_configuration(world_path, mod_paths)`
  230. * Checks whether configuration is valid.
  231. * `world_path`: path to the world
  232. * `mod_paths`: list of enabled mod paths
  233. * returns:
  234. {
  235. is_consistent = true, -- true is consistent, false otherwise
  236. unsatisfied_mods = {}, -- list of mod specs
  237. satisfied_mods = {}, -- list of mod specs
  238. error_message = "", -- message or nil
  239. }
  240. Logging
  241. -------
  242. * `core.debug(line)` (possible in async calls)
  243. * Always printed to `stderr` and logfile (`print()` is redirected here)
  244. * `core.log(line)` (possible in async calls)
  245. * `core.log(loglevel, line)` (possible in async calls)
  246. * `loglevel` one of "error", "action", "info", "verbose"
  247. Settings
  248. --------
  249. * `core.settings:set(name, value)`
  250. * `core.settings:get(name)` -> string or nil (possible in async calls)
  251. * `core.settings:set_bool(name, value)`
  252. * `core.settings:get_bool(name)` -> bool or nil (possible in async calls)
  253. * `core.settings:save()` -> nil, save all settings to config file
  254. For a complete list of methods of the `Settings` object see
  255. [lua_api.txt](https://github.com/minetest/minetest/blob/master/doc/lua_api.txt)
  256. Worlds
  257. ------
  258. * `core.get_worlds()` -> list of worlds (possible in async calls)
  259. * returns
  260. {
  261. [1] = {
  262. path = <full path to world>,
  263. name = <name of world>,
  264. gameid = <gameid of world>,
  265. },
  266. }
  267. * `core.create_world(worldname, gameid, init_settings)`
  268. * `core.delete_world(index)`
  269. Helpers
  270. -------
  271. * `core.get_us_time()`
  272. * returns time with microsecond precision
  273. * `core.gettext(string)` -> string
  274. * look up the translation of a string in the gettext message catalog
  275. * `fgettext_ne(string, ...)`
  276. * call `core.gettext(string)`, replace "$1"..."$9" with the given
  277. extra arguments and return the result
  278. * `fgettext(string, ...)` -> string
  279. * same as `fgettext_ne()`, but calls `core.formspec_escape` before returning result
  280. * `core.parse_json(string[, nullvalue])` -> something (possible in async calls)
  281. * see `core.parse_json` (`lua_api.txt`)
  282. * `dump(obj, dumped={})`
  283. * Return object serialized as a string
  284. * `string:split(separator)`
  285. * eg. `string:split("a,b", ",")` == `{"a","b"}`
  286. * `string:trim()`
  287. * eg. `string.trim("\n \t\tfoo bar\t ")` == `"foo bar"`
  288. * `core.is_yes(arg)` (possible in async calls)
  289. * returns whether `arg` can be interpreted as yes
  290. * `minetest.encode_base64(string)` (possible in async calls)
  291. * Encodes a string in base64.
  292. * `minetest.decode_base64(string)` (possible in async calls)
  293. * Decodes a string encoded in base64.
  294. Async
  295. -----
  296. * `core.handle_async(async_job,parameters,finished)`
  297. * execute a function asynchronously
  298. * `async_job` is a function receiving one parameter and returning one parameter
  299. * `parameters` parameter table passed to `async_job`
  300. * `finished` function to be called once `async_job` has finished
  301. the result of `async_job` is passed to this function
  302. ### Limitations of Async operations
  303. * No access to global lua variables, don't even try
  304. * Limited set of available functions
  305. e.g. No access to functions modifying menu like `core.start`, `core.close`,
  306. `core.show_path_select_dialog`
  307. Background music
  308. ----------------
  309. The main menu supports background music.
  310. It looks for a `main_menu` sound in `$USER_PATH/sounds`. The same naming
  311. conventions as for normal sounds apply.
  312. This means the player can add a custom sound.
  313. It will be played in the main menu (gain = 1.0), looped.