Random Item#
Collections / Random Item
Can Read
Cannot Write
Description#
Picks a random item of a collection and reads it as a number
Parameters#
| Name | Description |
|---|---|
| Collection | The collection an item is picked from |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
using Random = UnityEngine.Random;
namespace VisualScript.Runtime
{
[Get]
[Title("Random Item")]
[Category("Collections/Random Item")]
[Description("Picks a random item of a collection and reads it as a number")]
[Image(typeof(IconDice), ColorTheme.Type.Teal, typeof(BadgeList))]
[Parameter("Collection", "The collection an item is picked from")]
[Keywords("Dice", "Pick", "Choose", "Any", "List", "Array")]
[Serializable]
public class TypeNumberCollectionRandom : TypeNumber
{
[SerializeField] private GetCollection m_Collection = new GetCollection();
public override double Get(Args args)
{
using Args scoped = Args.Get(args);
int count = this.m_Collection.Capture(scoped);
if (count == 0) return 0f;
int index = Random.Range(0, count);
return ArgsCollectionParameters.TryGetItem(this.m_Collection, scoped, index, out double item)
? item
: 0f;
}
public override string ToString()
{
return $"Random of {this.m_Collection}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnUpdate;
}
}