Number Range#
Numbers / Number Range
Can Read
Cannot Write
Description#
A collection of numbers that goes from a first value to a last one using a step
Parameters#
| Name | Description |
|---|---|
| From | The first number of the collection |
| To | The last number of the collection. It is included when the step reaches it |
| Step | How much each number increases from the previous one. Never zero |
Code#
using System;
using System.Collections.Generic;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Number Range")]
[Category("Numbers/Number Range")]
[Description("A collection of numbers that goes from a first value to a last one using a step")]
[Image(typeof(IconNumber), ColorTheme.Type.Yellow, typeof(BadgeList))]
[Parameter("From", "The first number of the collection")]
[Parameter("To", "The last number of the collection. It is included when the step reaches it")]
[Parameter("Step", "How much each number increases from the previous one. Never zero")]
[Keywords("Sequence", "Series", "Count", "For", "Loop", "Iterate", "Index")]
[Serializable]
public class TypeCollectionNumberRange : TTypeCollection<double>
{
private const int MAX_ITEMS = 10000;
// EXPOSED MEMBERS: -----------------------------------------------------------------------
[SerializeField] private GetNumber m_From = new GetNumber(0f);
[SerializeField] private GetNumber m_To = new GetNumber(10f);
[SerializeField] private GetNumber m_Step = new GetNumber(1f);
// PUBLIC METHODS: ------------------------------------------------------------------------
public override string ToString()
{
return $"Range {this.m_From} to {this.m_To}";
}
// PROTECTED METHODS: ---------------------------------------------------------------------
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
protected override void OnCapture(Args args, List<double> items)
{
double from = this.m_From.Get(args);
double to = this.m_To.Get(args);
double stride = Math.Abs(this.m_Step.Get(args));
if (stride <= double.Epsilon) return;
double steps = Math.Floor(Math.Abs(to - from) / stride) + 1;
if (double.IsNaN(steps) || steps < 1) return;
int count = steps > MAX_ITEMS ? MAX_ITEMS : (int) steps;
double step = from > to ? -stride : stride;
for (int i = 0; i < count; ++i)
{
items.Add(from + step * i);
}
}
}
}