menu_lua_api.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. Minetest Lua Mainmenu API Reference 5.2.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.buttonhandler(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/adress>,
  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. Filesystem:
  29. core.get_builtin_path()
  30. ^ returns path to builtin root
  31. core.create_dir(absolute_path) (possible in async calls)
  32. ^ absolute_path to directory to create (needs to be absolute)
  33. ^ returns true/false
  34. core.delete_dir(absolute_path) (possible in async calls)
  35. ^ absolute_path to directory to delete (needs to be absolute)
  36. ^ returns true/false
  37. core.copy_dir(source,destination,keep_soure) (possible in async calls)
  38. ^ source folder
  39. ^ destination folder
  40. ^ keep_source DEFAULT true --> if set to false source is deleted after copying
  41. ^ returns true/false
  42. core.extract_zip(zipfile,destination) [unzip within path required]
  43. ^ zipfile to extract
  44. ^ destination folder to extract to
  45. ^ returns true/false
  46. core.download_file(url,target) (possible in async calls)
  47. ^ url to download
  48. ^ target to store to
  49. ^ returns true/false
  50. core.get_version() (possible in async calls)
  51. ^ returns current core version
  52. core.sound_play(spec, looped) -> handle
  53. ^ spec = SimpleSoundSpec (see lua-api.txt)
  54. ^ looped = bool
  55. core.sound_stop(handle)
  56. core.get_video_drivers()
  57. ^ get list of video drivers supported by engine (not all modes are guaranteed to work)
  58. ^ returns list of available video drivers' settings name and 'friendly' display name
  59. ^ e.g. { {name="opengl", friendly_name="OpenGL"}, {name="software", friendly_name="Software Renderer"} }
  60. ^ first element of returned list is guaranteed to be the NULL driver
  61. core.get_mapgen_names([include_hidden=false]) -> table of map generator algorithms
  62. registered in the core (possible in async calls)
  63. core.get_cache_path() -> path of cache
  64. Formspec:
  65. core.update_formspec(formspec)
  66. core.get_table_index(tablename) -> index
  67. ^ can also handle textlists
  68. core.formspec_escape(string) -> string
  69. ^ escapes characters [ ] \ , ; that can not be used in formspecs
  70. core.explode_table_event(string) -> table
  71. ^ returns e.g. {type="CHG", row=1, column=2}
  72. ^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
  73. core.explode_textlist_event(string) -> table
  74. ^ returns e.g. {type="CHG", index=1}
  75. ^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
  76. core.set_formspec_prepend(formspec)
  77. ^ string to be added to every mainmenu formspec, to be used for theming.
  78. GUI:
  79. core.set_background(type, texturepath,[tile],[minsize])
  80. ^ type: "background", "overlay", "header" or "footer"
  81. ^ tile: tile the image instead of scaling (background only)
  82. ^ minsize: minimum tile size, images are scaled to at least this size prior
  83. ^ doing tiling (background only)
  84. core.set_clouds(<true/false>)
  85. core.set_topleft_text(text)
  86. core.show_keys_menu()
  87. core.show_path_select_dialog(formname, caption, is_file_select)
  88. ^ shows a path select dialog
  89. ^ formname is base name of dialog response returned in fields
  90. ^ -if dialog was accepted "_accepted"
  91. ^ will be added to fieldname containing the path
  92. ^ -if dialog was canceled "_cancelled"
  93. ^ will be added to fieldname value is set to formname itself
  94. ^ if `is_file_select` is `true`, a file and not a folder will be selected
  95. ^ returns nil or selected file/folder
  96. core.get_screen_info()
  97. ^ returns {
  98. density = <screen density 0.75,1.0,2.0,3.0 ... (dpi)>,
  99. display_width = <width of display>,
  100. display_height = <height of display>,
  101. window_width = <current window width>,
  102. window_height = <current window height>
  103. }
  104. ### Content and Packages
  105. Content - an installed mod, modpack, game, or texture pack (txt)
  106. Package - content which is downloadable from the content db, may or may not be installed.
  107. * core.get_modpath() (possible in async calls)
  108. * returns path to global modpath
  109. * core.get_clientmodpath() (possible in async calls)
  110. * returns path to global client-side modpath
  111. * core.get_gamepath() (possible in async calls)
  112. * returns path to global gamepath
  113. * core.get_texturepath() (possible in async calls)
  114. * returns path to default textures
  115. * core.get_game(index)
  116. * returns:
  117. {
  118. id = <id>,
  119. path = <full path to game>,
  120. gamemods_path = <path>,
  121. name = <name of game>,
  122. menuicon_path = <full path to menuicon>,
  123. author = "author",
  124. DEPRECATED:
  125. addon_mods_paths = {[1] = <path>,},
  126. }
  127. * core.get_games() -> table of all games in upper format (possible in async calls)
  128. * core.get_content_info(path)
  129. * returns
  130. {
  131. name = "name of content",
  132. type = "mod" or "modpack" or "game" or "txp",
  133. description = "description",
  134. author = "author",
  135. path = "path/to/content",
  136. depends = {"mod", "names"}, -- mods only
  137. optional_depends = {"mod", "names"}, -- mods only
  138. }
  139. Favorites:
  140. core.get_favorites(location) -> list of favorites (possible in async calls)
  141. ^ location: "local" or "online"
  142. ^ returns {
  143. [1] = {
  144. clients = <number of clients/nil>,
  145. clients_max = <maximum number of clients/nil>,
  146. version = <server version/nil>,
  147. password = <true/nil>,
  148. creative = <true/nil>,
  149. damage = <true/nil>,
  150. pvp = <true/nil>,
  151. description = <server description/nil>,
  152. name = <server name/nil>,
  153. address = <address of server/nil>,
  154. port = <port>
  155. },
  156. }
  157. core.delete_favorite(id, location) -> success
  158. Logging:
  159. core.debug(line) (possible in async calls)
  160. ^ Always printed to stderr and logfile (print() is redirected here)
  161. core.log(line) (possible in async calls)
  162. core.log(loglevel, line) (possible in async calls)
  163. ^ loglevel one of "error", "action", "info", "verbose"
  164. Settings:
  165. core.settings:set(name, value)
  166. core.settings:get(name) -> string or nil (possible in async calls)
  167. core.settings:set_bool(name, value)
  168. core.settings:get_bool(name) -> bool or nil (possible in async calls)
  169. core.settings:save() -> nil, save all settings to config file
  170. For a complete list of methods of the Settings object see
  171. [lua_api.txt](https://github.com/minetest/minetest/blob/master/doc/lua_api.txt)
  172. Worlds:
  173. core.get_worlds() -> list of worlds (possible in async calls)
  174. ^ returns {
  175. [1] = {
  176. path = <full path to world>,
  177. name = <name of world>,
  178. gameid = <gameid of world>,
  179. },
  180. }
  181. core.create_world(worldname, gameid)
  182. core.delete_world(index)
  183. Helpers:
  184. core.get_us_time()
  185. ^ returns time with microsecond precision
  186. core.gettext(string) -> string
  187. ^ look up the translation of a string in the gettext message catalog
  188. fgettext_ne(string, ...)
  189. ^ call core.gettext(string), replace "$1"..."$9" with the given
  190. ^ extra arguments and return the result
  191. fgettext(string, ...) -> string
  192. ^ same as fgettext_ne(), but calls core.formspec_escape before returning result
  193. core.parse_json(string[, nullvalue]) -> something (possible in async calls)
  194. ^ see core.parse_json (lua_api.txt)
  195. dump(obj, dumped={})
  196. ^ Return object serialized as a string
  197. string:split(separator)
  198. ^ eg. string:split("a,b", ",") == {"a","b"}
  199. string:trim()
  200. ^ eg. string.trim("\n \t\tfoo bar\t ") == "foo bar"
  201. core.is_yes(arg) (possible in async calls)
  202. ^ returns whether arg can be interpreted as yes
  203. minetest.encode_base64(string) (possible in async calls)
  204. ^ Encodes a string in base64.
  205. minetest.decode_base64(string) (possible in async calls)
  206. ^ Decodes a string encoded in base64.
  207. Version compat:
  208. core.get_min_supp_proto()
  209. ^ returns the minimum supported network protocol version
  210. core.get_max_supp_proto()
  211. ^ returns the maximum supported network protocol version
  212. Async:
  213. core.handle_async(async_job,parameters,finished)
  214. ^ execute a function asynchronously
  215. ^ async_job is a function receiving one parameter and returning one parameter
  216. ^ parameters parameter table passed to async_job
  217. ^ finished function to be called once async_job has finished
  218. ^ the result of async_job is passed to this function
  219. Limitations of Async operations
  220. -No access to global lua variables, don't even try
  221. -Limited set of available functions
  222. e.g. No access to functions modifying menu like core.start,core.close,
  223. core.show_path_select_dialog
  224. Background music
  225. ----------------
  226. The main menu supports background music.
  227. It looks for a `main_menu` sound in `$USER_PATH/sounds`. The same naming
  228. conventions as for normal sounds apply.
  229. This means the player can add a custom sound.
  230. It will be played in the main menu (gain = 1.0), looped.