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