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