Module for NumberSequence Reading

NumberSequence Reading Module

Hey! I have done a simple module to read (evaluate) a Number Sequence with a simple ‘t’ value (between 0 and 1). It’s support a simple envelope (with random(-env, env)).

It’s maybe not the best optimization, but you can edit and share your version of course :slight_smile:

module :

local module = {}

module.Remap = function(start1,stop1, start2,stop2, value)
	return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1))
end

module.EvaluateSequenceWithRemap = function(numberSequence:NumberSequence, t, nRangeY:NumberRange)
	return module.Remap(0, 1, nRangeY.Min, nRangeY.Max, module.EvaluateSequence(numberSequence, t))
end

module.EvaluateSequence = function(numberSequence:NumberSequence, t)
	local points = numberSequence.Keypoints
	local x = math.clamp(t,0,1)
	for i=1, #points-1 do
		local p0 = points[i]
		local p1 = points[i+1]
		if t >= p0.Time and t <= p1.Time then
			local a = ((t - p0.Time) / (p1.Time - p0.Time))
			local v =  p0.Value + a * (p1.Value-p0.Value)
			local e =  p0.Envelope + a * (p1.Envelope-p0.Envelope)
			return v + (math.random(-e*100,e*100)/100)
		end
	end
	return -1
end

return module

usage :

-- import the ModuleScript
local SequenceReader = require( script.SequenceReader ) -- set your path where the module is, into the 'require'

-- create a NumberSequence (or use an existing one)
local ns = NumberSequence.new{
    NumberSequenceKeypoint.new(0, 1),
    NumberSequenceKeypoint.new(1, 0),
}

-- create a NumberRange (or use an existing one)
local myRange = NumberRange.new(0,10)

-- and call 'EvaluateSequence'
local value = SequenceReader.EvaluateSequence(ns, 0.5)
print(value) --0.5

-- or call 'EvaluateSequenceWithRemap'
local remapedValue = SequenceReader.EvaluateSequenceWithRemap(ns, 0.5, myRange)
print(remapedValue) --5

Edit : I have updated the module, to add a function that read the sequence, and remap the output with a NumberRange !

3 Likes