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