Helper Function Heap
As discussed in Lua: Abridged for XDRV, helper functions can make programming mods in XDRV a lot easier and much more succinct. If you have one or two functions that are generally useful for a lot of modfiles, you may find yourself copying and pasting the functions from file to file. If you don’t have any functions like that yet, maybe you’ll be able to find one here!
This page contains several helper functions by various XDRV charters. All helper functions in the repository can be viewed as separate files on this page. If you would prefer to read these helper functions from the GitHub itself, you can do so by going to the code directory of the GitHub repository. I recommend looking at the all.lua file in each subdirectory, which combines all functions and function documentation from the individual files in that folder.
Pulse Functions
Section titled “Pulse Functions”Author: TheBlockiest
Events AlphaThese functions create a “pulse” effect in the background, increasing the background’s alpha value to one value, holding it for some (or no) time, and then decreasing it to another value. Pulses can be great for representing percussive elements like kicks, snares, and crashes. These pulse function use linear easing for alpha only.
--[[pulse(pulseAlpha, endAlpha, startBeat, beatDurations)Pulses the lighting of the background by increasing and decreasing PathAlpha
Parameters:- pulseAlpha: The alpha value of the peak of the pulse- endAlpha: The alpha value of the end of the pulse- startBeat: The beat to start the pulse on- beatDurations: A table of three duration values: {inDuration, holdDuration, outDuration} - Over inDuration, the alpha eases from its last set value to pulseAlpha - Over holdDuration, the alpha stays at pulseAlpha - Over outDuration, the alpha eases from pulseAlpha to endAlpha - All duration values are stored in beats]]local function pulse(pulseAlpha, endAlpha, startBeat, beatDurations) local inDuration = beatDurations[1] local holdDuration = beatDurations[2] local outDuration = beatDurations[3]
if inDuration == 0 then xdrv.RunEvent("SetPathAlpha","beat",startBeat,pulseAlpha) else xdrv.RunEvent("EasePathAlpha","beat",startBeat,pulseAlpha,inDuration) end
local outStartBeat = startBeat + inDuration + holdDuration
xdrv.RunEvent("EasePathAlpha","beat",outStartBeat,endAlpha, outDuration)end
pulse(0.9,0.3,12,{0.25,0,0.75})--[[pulseLeft(pulseAlpha, endAlpha, startBeat, beatDurations)Pulses the lighting of the left side of the background by increasing and decreasing PathAlpha
Parameters:- pulseAlpha: The alpha value of the peak of the pulse- endAlpha: The alpha value of the end of the pulse- startBeat: The beat to start the pulse on- beatDurations: A table of three duration values: {inDuration, holdDuration, outDuration} - Over inDuration, the alpha eases from its last set value to pulseAlpha - Over holdDuration, the alpha stays at pulseAlpha - Over outDuration, the alpha eases from pulseAlpha to endAlpha - All duration values are stored in beats]]local function pulseLeft(pulseAlpha, endAlpha, startBeat, beatDurations) local inDuration = beatDurations[1] local holdDuration = beatDurations[2] local outDuration = beatDurations[3]
if inDuration == 0 then xdrv.RunEvent("SetLeftPathAlpha","beat",startBeat,pulseAlpha) else xdrv.RunEvent("EaseLeftPathAlpha","beat",startBeat,pulseAlpha,inDuration) end
local outStartBeat = startBeat + inDuration + holdDuration
xdrv.RunEvent("EaseLeftPathAlpha","beat",outStartBeat,endAlpha, outDuration)end
pulseLeft(0.9,0.3,12,{0.25,0,0.75})--[[pulseRight(pulseAlpha, endAlpha, startBeat, beatDurations)Pulses the lighting of the right side of the background by increasing and decreasing PathAlpha
Parameters:- pulseAlpha: The alpha value of the peak of the pulse- endAlpha: The alpha value of the end of the pulse- startBeat: The beat to start the pulse on- beatDurations: A table of three duration values: {inDuration, holdDuration, outDuration} - Over inDuration, the alpha eases from its last set value to pulseAlpha - Over holdDuration, the alpha stays at pulseAlpha - Over outDuration, the alpha eases from pulseAlpha to endAlpha - All duration values are stored in beats]]local function pulseRight(pulseAlpha, endAlpha, startBeat, beatDurations) local inDuration = beatDurations[1] local holdDuration = beatDurations[2] local outDuration = beatDurations[3]
if inDuration == 0 then xdrv.RunEvent("SetRightPathAlpha","beat",startBeat,pulseAlpha) else xdrv.RunEvent("EaseRightPathAlpha","beat",startBeat,pulseAlpha,inDuration) end
local outStartBeat = startBeat + inDuration + holdDuration
xdrv.RunEvent("EaseRightPathAlpha","beat",outStartBeat,endAlpha, outDuration)end
pulseRight(0.9,0.3,12,{0.25,0,0.75})--[[pulseBoth(pulseAlpha, endAlpha, startBeat, beatDurations)Pulses the lighting of the background by increasing and decreasing PathAlphaEases the left side and right side's alpha separately
Implementation:Requires pulseLeft and pulseRight to function
Parameters:- pulseAlpha: The alpha value of the peak of the pulse- endAlpha: The alpha value of the end of the pulse- startBeat: The beat to start the pulse on- beatDurations: A table of three duration values: {inDuration, holdDuration, outDuration} - Over inDuration, the alpha eases from its last set value to pulseAlpha - Over holdDuration, the alpha stays at pulseAlpha - Over outDuration, the alpha eases from pulseAlpha to endAlpha - All duration values are stored in beats]]local function pulseBoth(pulseAlpha, endAlpha, startBeat, beatDurations) PulseLeft(pulseAlpha, endAlpha, startBeat, beatDurations) PulseRight(pulseAlpha, endAlpha, startBeat, beatDurations)end
pulseBoth(0.9,0.3,12,{0.25,0,0.75})Split Alpha Functions
Section titled “Split Alpha Functions”Author: TheBlockiest
Events AlphaThese functions swap the values of PathAlpha with the values of LeftPathAlpha and RightPathAlpha. This allows you to seamlessly swap whether alpha values are controlled together or individually.
--[[splitAlpha(beat, alpha)Sets PathAlpha to 1Also sets the alpha of the left and right sides to a given valueEnsures that LeftPathAlpha and RightPathAlpha are not blended with PathAlpha
Parameters:- beat: The beat to split the background alphas at- alpha: The alpha to set the left and right sides of the background to]]local function splitAlpha(beat, alpha) xdrv.RunEvent("SetPathAlpha","beat",beat,1)
xdrv.RunEvent("SetLeftPathAlpha","beat",beat,alpha) xdrv.RunEvent("SetRightPathAlpha","beat",beat,alpha)end
splitAlpha(8,0.5)--[[mergeAlpha(beat, alpha)Sets LeftPathAlpha and RightPathAlpha to 1Also sets the alpha of background to a given valuesEnsures that PathAlpha is not blended with LeftPathAlpha and RightPathAlpha
Parameters:- beat: The beat to split the background alphas at- alpha: The alpha to set the left and right sides of the background to]]local function mergeAlpha(beat, alpha) xdrv.RunEvent("SetPathAlpha","beat",beat,alpha)
xdrv.RunEvent("SetLeftPathAlpha","beat",beat,1) xdrv.RunEvent("SetRightPathAlpha","beat",beat,1)end
mergeAlpha(16,0.5)Check Table Functions
Section titled “Check Table Functions”Author: TheBlockiest
DataThese functions check for a value within a list (a sequentially & numerically-indexed table) or a table (any table). This functions can be useful if you want to exclude specific beats from a repeated mod.
--[[checkList(elt, list)Checks if an element is within a listNot guaranteed to work with non-list tables
Parameters:- elt: The element to check the list for- list: A numerically and sequentially indexed table - Ex. {4,11,2,0.5}
Returns:- true if the element is found in the list- false otherwise]]local function checkList(elt, list) for _, v in ipairs(list) do if v == elt then return true end end return falseend
local list = {1,3,4,6}local hasElt = checkList(3,list)-- hasElt = truelocal hasElt2 = checkList(5,list)-- hasElt2 = false--[[checkTable(elt, t)Checks if an element is within a tableAlso works for lists, but is less optimal
Parameters:- elt: The element to check the table for- t: Any table
Returns:- true if the element is found in the table- false otherwise]]local function checkTable(elt, t) for _, v in pairs(t) do if v == elt then return true end end return falseend
local table = {["X"]=1,["D"]=3,["R"]=4,["V"]=6}local hasElt = checkTable(3,table)-- hasElt = truelocal hasElt2 = checkTable(5,table)-- hasElt2 = falseThat’s all the helper functions I can compile so far, but I hope to add more in the future. Remember, take any helper functions from this repository that you wish to use.