Use "core" namespace internally
[oweals/minetest.git] / doc / menu_lua_api.txt
1 Minetest Lua Mainmenu API Reference 0.4.9
2 ========================================
3
4 Introduction
5 -------------
6 The main menu is defined as a formspec by Lua in builtin/mainmenu/
7 Description of formspec language to show your menu is in lua_api.txt
8
9 Callbacks
10 ---------
11 engine.buttonhandler(fields): called when a button is pressed.
12 ^ fields = {name1 = value1, name2 = value2, ...}
13 engine.event_handler(event)
14 ^ event: "MenuQuit", "KeyEnter", "ExitButton" or "EditBoxEnter"
15
16 Gamedata
17 --------
18 The "gamedata" table is read when calling engine.start(). It should contain:
19 {
20         playername     = <name>,
21         password       = <password>,
22         address        = <IP/adress>,
23         port           = <port>,
24         selected_world = <index>, -- 0 for client mode
25         singleplayer   = <true/false>,
26 }
27
28 Functions
29 ---------
30 engine.start()
31 engine.close()
32
33 Filesystem:
34 engine.get_scriptdir()
35 ^ returns directory of script
36 engine.get_modpath() (possible in async calls)
37 ^ returns path to global modpath
38 engine.get_modstore_details(modid) (possible in async calls)
39 ^ modid numeric id of mod in modstore
40 ^ returns {
41         id                      = <numeric id of mod in modstore>,
42         title           = <human readable title>,
43         basename        = <basename for mod>,
44         description = <description of mod>,
45         author          = <author of mod>,
46         download_url= <best match download url>,
47         license         = <short description of license>,
48         rating          = <float value of current rating>
49 }
50 engine.get_modstore_list() (possible in async calls)
51 ^ returns {
52         [1] = {
53                 id               = <numeric id of mod in modstore>,
54                 title    = <human readable title>,
55                 basename = <basename for mod>
56         }
57 }
58 engine.get_gamepath() (possible in async calls)
59 ^ returns path to global gamepath
60 engine.get_texturepath() (possible in async calls)
61 ^ returns path to default textures
62 engine.get_dirlist(path,onlydirs) (possible in async calls)
63 ^ path to get subdirs from
64 ^ onlydirs should result contain only dirs?
65 ^ returns list of folders within path
66 engine.create_dir(absolute_path) (possible in async calls)
67 ^ absolute_path to directory to create (needs to be absolute)
68 ^ returns true/false
69 engine.delete_dir(absolute_path) (possible in async calls)
70 ^ absolute_path to directory to delete (needs to be absolute)
71 ^ returns true/false
72 engine.copy_dir(source,destination,keep_soure) (possible in async calls)
73 ^ source folder
74 ^ destination folder
75 ^ keep_source DEFAULT true --> if set to false source is deleted after copying
76 ^ returns true/false
77 engine.extract_zip(zipfile,destination) [unzip within path required]
78 ^ zipfile to extract
79 ^ destination folder to extract to
80 ^ returns true/false
81 engine.download_file(url,target) (possible in async calls)
82 ^ url to download
83 ^ target to store to
84 ^ returns true/false
85 engine.get_version() (possible in async calls)
86 ^ returns current core version
87 engine.sound_play(spec, looped) -> handle
88 ^ spec = SimpleSoundSpec (see lua-api.txt)
89 ^ looped = bool
90 engine.sound_stop(handle)
91
92 Formspec:
93 engine.update_formspec(formspec)
94 engine.get_table_index(tablename) -> index
95 ^ can also handle textlists
96 engine.formspec_escape(string) -> string
97 ^ escapes characters [ ] \ , ; that can not be used in formspecs
98 engine.explode_table_event(string) -> table
99 ^ returns e.g. {type="CHG", row=1, column=2}
100 ^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
101 engine.explode_textlist_event(string) -> table
102 ^ returns e.g. {type="CHG", index=1}
103 ^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
104
105 GUI:
106 engine.set_background(type, texturepath)
107 ^ type: "background", "overlay", "header" or "footer"
108 engine.set_clouds(<true/false>)
109 engine.set_topleft_text(text)
110 engine.show_keys_menu()
111 engine.file_open_dialog(formname,caption)
112 ^ shows a file open dialog
113 ^ formname is base name of dialog response returned in fields
114 ^     -if dialog was accepted "_accepted"
115 ^^       will be added to fieldname containing the path
116 ^     -if dialog was canceled "_cancelled"
117 ^        will be added to fieldname value is set to formname itself
118 ^ returns nil or selected file/folder
119 engine.get_screen_info()
120 ^ returns {
121         density         = <screen density 0.75,1.0,2.0,3.0 ... (dpi)>,
122         display_width   = <width of display>,
123         display_height  = <height of display>,
124         window_width    = <current window width>,
125         window_height   = <current window height>
126         }
127
128 Games:
129 engine.get_game(index)
130 ^ returns {
131         id               = <id>,
132         path             = <full path to game>,
133         gamemods_path    = <path>,
134         name             = <name of game>,
135         menuicon_path    = <full path to menuicon>,
136         DEPRECATED:
137         addon_mods_paths = {[1] = <path>,},
138 }
139 engine.get_games() -> table of all games in upper format (possible in async calls)
140
141 Favorites:
142 engine.get_favorites(location) -> list of favorites (possible in async calls)
143 ^ location: "local" or "online"
144 ^ returns {
145         [1] = {
146         clients       = <number of clients/nil>,
147         clients_max   = <maximum number of clients/nil>,
148         version       = <server version/nil>,
149         password      = <true/nil>,
150         creative      = <true/nil>,
151         damage        = <true/nil>,
152         pvp           = <true/nil>,
153         description   = <server description/nil>,
154         name          = <server name/nil>,
155         address       = <address of server/nil>,
156         port          = <port>
157         },
158 }
159 engine.delete_favorite(id, location) -> success
160
161 Logging:
162 engine.debug(line) (possible in async calls)
163 ^ Always printed to stderr and logfile (print() is redirected here)
164 engine.log(line) (possible in async calls)
165 engine.log(loglevel, line) (possible in async calls)
166 ^ loglevel one of "error", "action", "info", "verbose"
167
168 Settings:
169 engine.setting_set(name, value)
170 engine.setting_get(name) -> string or nil (possible in async calls)
171 engine.setting_setbool(name, value)
172 engine.setting_getbool(name) -> bool or nil (possible in async calls)
173 engine.setting_save() -> nil, save all settings to config file
174
175 Worlds:
176 engine.get_worlds() -> list of worlds (possible in async calls)
177 ^ returns {
178         [1] = {
179         path   = <full path to world>,
180         name   = <name of world>,
181         gameid = <gameid of world>,
182         },
183 }
184 engine.create_world(worldname, gameid)
185 engine.delete_world(index)
186
187 Helpers:
188 engine.gettext(string) -> string
189 ^ look up the translation of a string in the gettext message catalog
190 fgettext(string, ...) -> string
191 ^ call engine.gettext(string), replace "$1"..."$9" with the given
192 ^ extra arguments, call engine.formspec_escape and return the result
193 engine.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
204 Async:
205 engine.handle_async(async_job,parameters,finished)
206 ^ execute a function asynchronously
207 ^ async_job is a function receiving one parameter and returning one parameter
208 ^ parameters parameter table passed to async_job
209 ^ finished function to be called once async_job has finished
210 ^    the result of async_job is passed to this function
211
212 Limitations of Async operations
213  -No access to global lua variables, don't even try
214  -Limited set of available functions
215         e.g. No access to functions modifying menu like engine.start,engine.close,
216         engine.file_open_dialog
217                         
218
219 Class reference
220 ----------------
221 Settings: see lua_api.txt