@@ -1094,6 +1094,63 @@ core.register_node("default:dirt_with_grass", {
10941094})
10951095```
10961096
1097+ Variants
1098+ --------
1099+
1100+ Items can have "variants", which are numbered states that can determine certain
1101+ properties. The number of variants is specified with the item definition field
1102+ ` variant_count ` . Each variant is numbered from 0 to ` variant_count ` - 1. Every
1103+ item has at least one variant (numbered 0). Nodes can specify a part of param2
1104+ to read as the variant number by setting the ` param2_variant ` field to a
1105+ ` BitField ` .
1106+
1107+ The currently supported variant properties are:
1108+ * ` tiles `
1109+ * ` overlay_tiles `
1110+ * ` special_tiles `
1111+ The ` variants ` table in the item definition is a mapping from
1112+ variant numbers to variant tables. These tables can include the aforementioned
1113+ fields to set the properties for particular variants. Variants not present in
1114+ the mapping default to the values of the aforementioned fields specified in the
1115+ item definition table. Old clients will receive only the variant 0 tiles.
1116+
1117+ Items with multiple variants can specify a variant number with the "variant" key
1118+ in their metadata. The absence of the key indicates a variant number of 0, and
1119+ this is the canonical representation of the 0 variant. The helper function
1120+ ` core.itemstring_with_variant ` is a shortcut for creating such itemstrings.
1121+ The variant is preserved when a node is placed or dug. Custom drops will inherit
1122+ the variant only if ` inherit_variant ` is set to ` true ` in their specification.
1123+
1124+ Example node with variants:
1125+
1126+ core.register_node("mod:grass", {
1127+ description = "Grass",
1128+ drawtype = "plantlike",
1129+ paramtype = "light",
1130+ sunlight_propagates = true,
1131+ walkable = false,
1132+ groups = {dig_immediate = 3},
1133+ -- There are 4 variants numbered 0 to 3.
1134+ variant_count = 4,
1135+ -- The lowest 2 bits store the variant number.
1136+ param2_variant = {width = 2, offset = 0},
1137+ -- These tiles will be used for variants not otherwise specified,
1138+ -- in this case variant 0.
1139+ tiles = {"mod_grass1.png"},
1140+ -- Tiles for variants 1, 2, and 3 are specified here.
1141+ variants = {
1142+ {tiles = {"mod_grass2.png"}},
1143+ {tiles = {"mod_grass3.png"}},
1144+ {tiles = {"mod_grass4.png"}},
1145+ },
1146+ drop = {
1147+ items = {
1148+ -- The seeds will inherit the variant of the grass.
1149+ {items = {"mod:seeds"}, inherit_variant = true},
1150+ }
1151+ }
1152+ })
1153+
10971154
10981155
10991156Sounds
@@ -1976,6 +2033,13 @@ Exact pointing location (currently only `Raycast` supports these fields):
19762033 For entities with rotated selection boxes, this will be rotated properly
19772034 by the entity's rotation - it will always be in absolute world space.
19782035
2036+ ` BitField `
2037+ ----------
2038+
2039+ A ` BitField ` specifies a part of an unsigned integer whose bits represent
2040+ another unsigned integer. A ` BitField ` is represented as follows:
2041+
2042+ {width = <integer bit width>, offset = <offset from least significant bit>}
19792043
19802044
19812045
@@ -2700,6 +2764,8 @@ Some of the values in the key-value store are handled specially:
27002764* ` color ` : A ` ColorString ` , which sets the stack's color.
27012765* ` palette_index ` : If the item has a palette, this is used to get the
27022766 current color from the palette.
2767+ * ` variant ` : If the item has more than one variant, this is the variant number.
2768+ The canonical form of variant 0 is the absence of this key.
27032769* ` count_meta ` : Replace the displayed count with any string.
27042770* ` count_alignment ` : Set the alignment of the displayed count value. This is an
27052771 int value. The lowest 2 bits specify the alignment in x-direction, the 3rd and
@@ -5750,6 +5816,8 @@ Utilities
57505816 particle_blend_clip = true ,
57515817 -- The `match_meta` optional parameter is available for `InvRef:remove_item()` (5.12.0)
57525818 remove_item_match_meta = true ,
5819+ -- Node/Item texture variants is supported (5.12.0)
5820+ texture_variants = true ,
57535821 }
57545822 ```
57555823
@@ -6901,6 +6969,12 @@ Item handling
69016969 given ` param2 ` value.
69026970 * Returns ` nil ` if the given ` paramtype2 ` does not contain color
69036971 information.
6972+ * ` core.strip_param2_variant(param2, def) `
6973+ * Returns the variant from ` param2 ` with the given node definition ` def ` .
6974+ * Always returns a non-negative integer less than ` def.variant_count ` .
6975+ * ` core.set_param2_variant(param2, variant, def) `
6976+ * Returns a modified ` param2 ` with the variant bitfield set to ` variant `
6977+ with the given node definition ` def ` .
69046978* ` core.get_node_drops(node, toolname) `
69056979 * Returns list of itemstrings that are dropped by ` node ` when dug
69066980 with the item ` toolname ` (not limited to tools).
@@ -6969,6 +7043,11 @@ Item handling
69697043 * ` item` : the item stack which becomes colored . Can be in string ,
69707044 table and native form .
69717045 * ` colorstring` : the new color of the item stack
7046+ * ` core.itemstring_with_variant(item, variant)` : returns an item string
7047+ * Creates an item string with an associated item variant .
7048+ * ` item` : the item stack which is given the variant . Can be in string ,
7049+ table or native form .
7050+ * ` variant` : the new variant of the item stack
69727051
69737052Rollback
69747053---- ----
@@ -9696,6 +9775,9 @@ Used by `core.register_node`, `core.register_craftitem`, and
96969775 -- {bendy = 2, snappy = 1},
96979776 -- {hard = 1, metal = 1, spikes = 1}
96989777
9778+ variant_count = 1 ,
9779+ -- The number item variants, a positive integer.
9780+
96999781 inventory_image = " " ,
97009782 -- Texture shown in the inventory GUI
97019783 -- Defaults to a 3D rendering of the node if left empty.
@@ -9920,6 +10002,9 @@ Used by `core.register_node`.
992010002{
992110003 -- <all fields allowed in item definitions>
992210004
10005+ param2_variant = BitField ,
10006+ -- The part of param2 from which to read the variant number.
10007+
992310008 drawtype = " normal" , -- See "Node drawtypes"
992410009
992510010 visual_scale = 1.0 ,
@@ -9933,17 +10018,45 @@ Used by `core.register_node`.
993310018 tiles = {tile definition 1 , def2 , def3 , def4 , def5 , def6 },
993410019 -- Textures of node; +Y, -Y, +X, -X, +Z, -Z
993510020 -- List can be shortened to needed length.
10021+ -- This field is also used for Variant number 0, see "Variants" for details.
993610022
993710023 overlay_tiles = {tile definition 1 , def2 , def3 , def4 , def5 , def6 },
993810024 -- Same as `tiles`, but these textures are drawn on top of the base
993910025 -- tiles. You can use this to colorize only specific parts of your
994010026 -- texture. If the texture name is an empty string, that overlay is not
994110027 -- drawn. Since such tiles are drawn twice, it is not recommended to use
994210028 -- overlays on very common nodes.
10029+ -- This field is also used for Variant number 0, see "Variants" for details.
994310030
994410031 special_tiles = {tile definition 1 , Tile definition 2 },
994510032 -- Special textures of node; used rarely.
994610033 -- List can be shortened to needed length.
10034+ -- This field is also used for Variant number 0, see "Variants" for details.
10035+
10036+ -- See "Variants"
10037+ -- This field is optional.
10038+ variants = {
10039+ -- Variant number 0 is created from fields
10040+ -- tiles, overlay_tiles and special_tiles
10041+ -- defined above (outside from variants table).
10042+ { -- Variant number 1.
10043+ -- this field is reused from above definition if is not specified.
10044+ tiles = {tile definition 1 , def2 , def3 , def4 , def5 , def6 },
10045+
10046+ -- this field is reused from above definition if is not specified.
10047+ overlay_tiles = {def1 , def2 , def3 , def4 , def5 , def6 },
10048+
10049+ -- this field is reused from above definition if is not specified.
10050+ special_tiles = {def1 , def2 },
10051+ },
10052+ { -- Variant number 2.
10053+ -- reuse tiles and special_tiles from variant number 0
10054+ -- no overlay_tiles
10055+ overlay_tiles = {},
10056+ ...
10057+ },
10058+ ...
10059+ },
994710060
994810061 color = ColorSpec ,
994910062 -- The node's original color will be multiplied with this color.
@@ -10221,6 +10334,9 @@ Used by `core.register_node`.
1022110334 -- hardware coloring palette color from the dug node.
1022210335 -- Default is 'false'.
1022310336 inherit_color = true ,
10337+ -- variant of the dug node.
10338+ -- Default is 'false'.
10339+ inherit_variant = true ,
1022410340 },
1022510341 {
1022610342 -- Only drop if using an item whose name contains
0 commit comments