@@ -1579,6 +1579,8 @@ macro_rules! array {
1579
1579
/// To create a typed `Array` with a single element type, see the [`array!`] macro.
1580
1580
///
1581
1581
/// For dictionaries, a similar macro [`dict!`] exists.
1582
+ ///
1583
+ /// To construct slices of variants, use [`vslice!`].
1582
1584
#[ macro_export]
1583
1585
macro_rules! varray {
1584
1586
// Note: use to_variant() and not Variant::from(), as that works with both references and values
@@ -1594,6 +1596,49 @@ macro_rules! varray {
1594
1596
} ;
1595
1597
}
1596
1598
1599
+ /// Constructs a slice of [`Variant`] literals, useful for passing to vararg functions.
1600
+ ///
1601
+ /// Many APIs in Godot have variable-length arguments. GDScript can call such functions by simply passing more arguments, but in Rust,
1602
+ /// the parameter type `&[Variant]` is used.
1603
+ ///
1604
+ /// This macro creates a [slice](https://doc.rust-lang.org/std/primitive.slice.html) of `Variant` values.
1605
+ ///
1606
+ /// # Examples
1607
+ /// Variable number of arguments:
1608
+ /// ```no_run
1609
+ /// # use godot::prelude::*;
1610
+ /// let slice: &[Variant] = vslice![42, "hello", true];
1611
+ ///
1612
+ /// let concat: GString = godot::global::str(slice);
1613
+ /// ```
1614
+ /// _(In practice, you might want to use [`godot_str!`][crate::global::godot_str] instead of `str()`.)_
1615
+ ///
1616
+ /// Dynamic function call via reflection. NIL can still be passed inside `vslice!`, just use `Variant::nil()`.
1617
+ /// ```no_run
1618
+ /// # use godot::prelude::*;
1619
+ /// # fn some_object() -> Gd<Object> { unimplemented!() }
1620
+ /// let mut obj: Gd<Object> = some_object();
1621
+ /// obj.call("some_method", vslice![Vector2i::new(1, 2), Variant::nil()]);
1622
+ /// ```
1623
+ ///
1624
+ /// # See also
1625
+ /// To create typed and untyped `Array`s, use the [`array!`] and [`varray!`] macros respectively.
1626
+ ///
1627
+ /// For dictionaries, a similar macro [`dict!`] exists.
1628
+ #[ macro_export]
1629
+ macro_rules! vslice {
1630
+ // Note: use to_variant() and not Variant::from(), as that works with both references and values
1631
+ ( $( $elements: expr) ,* $( , ) ?) => {
1632
+ {
1633
+ use $crate:: meta:: ToGodot as _;
1634
+ let mut array = $crate:: builtin:: VariantArray :: default ( ) ;
1635
+ & [
1636
+ $( $elements. to_variant( ) , ) *
1637
+ ]
1638
+ }
1639
+ } ;
1640
+ }
1641
+
1597
1642
// ----------------------------------------------------------------------------------------------------------------------------------------------
1598
1643
1599
1644
#[ cfg( feature = "serde" ) ]
0 commit comments