Update API examples and function descriptions

master
Wuzzy 2018-05-17 15:11:52 +02:00
parent 1288177745
commit 76575687bd
1 changed files with 16 additions and 10 deletions

26
API.md
View File

@ -20,7 +20,7 @@ to filter out unwanted items.
#### Parameters
* `playername`: Name of player to show dialog to
* `dialogname`: Unique identifier of the dialog (must not contain “%%”)
* `dialogname`: Identifier of the dialog (must not contain “%%”)
* `filter`: Optional filter function to narrow down the visible
items (see below)
@ -47,25 +47,24 @@ Whenever a player selects an item or cancels the selection,
`callback` is called.
#### `callback` function
This has the function signature `callback(identifier, playername, itemstring)`.
This has the function signature `callback(playername, dialogname, itemstring)`.
* `identifier` is the identifier of the item formspec
* `playername` is the name of the player who selected the item,
* `dialogname` is the dialog identifier of the used item selection dialog
* `itemstring` is the itemstring of the chosen item or `nil` if aborted
Normally, if the player pushes any button, the formspec is closed.
But if you return `false` in this callback, the formspec is *not*
Use this when you run into problems.
## Examples
Display all items from Creative inventory to player 1:
```
select_item.show_dialog("Player 1", select_item.filters.creative)
select_item.show_dialog("Player 1", "example:creative", select_item.filters.creative)
```
Display all flammable to Player 1:
```
select_item.show_dialog("Player 1", function(itemstring)
select_item.show_dialog("Player 1", "example:flammable", function(itemstring)
if minetest.get_item_group(itemstring), "flammable") >= 1 then
return true
else
@ -74,10 +73,17 @@ select_item.show_dialog("Player 1", function(itemstring)
end
```
Adding a selected to the player's inventory:
Note the different values for `dialogname`.
Adding a selected to the player's inventory after player selected item in the “Creative” dialog
above:
```
select_item.register_on_select_item(function(playername, itemstring)
local inv = minetest.get_inventory({type="player" location=playername})
inv:add_item("main", ItemStack(itemstring))
select_item.register_on_select_item(function(playername, dialogname, itemstring)
-- Check for the dialog type you care about. This check should almost always be done
-- to ensure interopability with other mods.
if dialogname == "example:creative" then
local inv = minetest.get_inventory({type="player" location=playername})
inv:add_item("main", ItemStack(itemstring))
end
end)
```