Skip to content

Callbacks

Callbacks are how your script hears about the song. They're functions the game calls when something happens: a beat, a hit, a miss, the chart reaching your event. You just write the ones you care about and leave out the rest - a file can have any mix of them.

function onBeat(b)
    camera.bump += 0.02      -- runs on every beat of the song
end
When Callbacks
The song onStart · onCountdown · onSongStart · onEvent · onEnd · onFail
The music's time onBeat · onStep · onSection · onFrame
Notes onHit · onMiss · onOpponentHit
Keys onKeyPress · onKeyRelease · onGhostTap
Pause onPause · onResume

Every callback runs for every loaded event

onBeat in your file runs on every beat - whether or not the chart has used your event yet. That's why a file with only onBeat / onHit and no onEvent works as a song script, and why an event that switches a mode on checks its own active variable first.

Parameters are optional and you name them yourself: function onBeat() and function onBeat(b) both work. Just don't call them beat, step or time - those are built-in values, so the name is taken (function onBeat(b) still reads the current beat as beat).


The song

onStart

function onStart()

The song is set up, before the countdown - and again on every restart. Set up your pictures, texts and starting values here.

function onStart()
    strums.alpha = 0
    tween(strums.alpha, 1, beats(4))     -- the lanes fade in during the countdown
end

Psych: onCreate · Kade: start(song)

onCountdown

function onCountdown(n)

Each step of the 3-2-1-Go countdown. Gets n: 3, 2, 1, then 0 for "go".

function onCountdown(n)
    text[3].value = n == 0 ? "GO!" : str(n)
    text[3].alpha = 1
    tween(text[3].alpha, 0, beats(0.9))
end

Psych: onCountdownTick(counter)

onSongStart

function onSongStart()

The music starts, right after the countdown.

function onSongStart()
    showStrums(beats(1))     -- the lanes come in with the music
end

Psych: onSongStart

onEvent

function onEvent(p)

The song reaches a chart event of this file's name (or alias). Gets p: the event's parameters by name (p.speed, p.tint, ...) plus the raw p.value1 and p.value2.

params {
    tint = color { from = "value1", default = #FFFFFF, label = "Colour" },
}

function onEvent(p)
    flash(p.tint, beats(1))
end

Psych: onEvent(name, value1, value2) in custom_events/

onEnd

function onEnd()

The song was played to its end. Not when it was failed, left, or a fight was decided early.

function onEnd()
    text[0].value = "CLEAR!"
end

Psych: onEndSong

onFail

function onFail()

Your health ran out - just before the song ends as failed. Only where there's health: in fights (practice has none).

function onFail()
    fadeOut(#000000, beats(1))
end

Psych: onGameOver


The music's time

Beats and steps ride the same beat as the camera bop, so everything on the beat lands together. A slow frame never skips one: if the game hitches over a few steps, each still gets its onStep (and onBeat, onSection) in order - you can hang a whole song's timeline on step numbers.

onBeat

function onBeat(b)

A new beat starts. Gets b: the beat number (0, 1, 2, ...).

function onBeat(b)
    if b % 4 == 0 then camera.bump += 0.03 end   -- a pop on the first beat of every bar
end

Psych: onBeatHit + curBeat · Kade: beatHit(beat)

onStep

function onStep(n)

A new step starts - a sixteenth note, four per beat. Gets n: the step number.

function onStep(n)
    if n == 256 then flash() end      -- an exact moment of the song, by step
end

Psych: onStepHit + curStep · Kade: stepHit(step)

onSection

function onSection(n)

Every 4 beats (beats 0, 4, 8, ...), right after that beat's onBeat. Gets n: the section number (0, 1, 2, ...).

function onSection(n)
    zoom(n % 2 == 0 ? 1.05 : 1, beats(1))   -- in and out, bar by bar
end

Psych: onSectionHit + curSection

onFrame

function onFrame(dt)

Every rendered frame. Gets dt: milliseconds since the last frame. Use it for anything that moves all the time - work positions out from time (or beat), so it looks the same at any frame rate.

function onFrame(dt)
    hud.angle = sin(time / 500) * 1.5      -- a slow sway
end

Psych: onUpdate(elapsed) · Kade: update(elapsed)


Notes

The three note callbacks get the note:

Field
note.lane 0-3: left, down, up, right
note.judgment "sick", "good", "bad", "shit" or "miss"
note.offset how early (negative) or late it was hit, in ms; 0 for misses and the opponent
note.hold whether it's a hold note

note or n?

Inside function onHit(note) the name note is that one note - the notes on their way (note[0], note[1], ...) are out of reach there. If you need both, call it n: function onHit(n).

onHit

function onHit(note)

You hit a note.

function onHit(note)
    if note.hold then return end
    strum[note.lane].scale = 1.15
    tween(strum[note.lane].scale, 1, beats(0.5), "backOut")
end

Psych: goodNoteHit · Kade: playerOneSing

onMiss

function onMiss(note)

You missed: a note went by, you pressed with nothing to hit (ghost tapping off), or let go of a hold too early.

function onMiss(note)
    flash(#FF3040, beats(0.5))
end

Psych: noteMiss + noteMissPress · Kade: playerOneMiss

onOpponentHit

function onOpponentHit(note)

The opponent's side plays a note - the chart's opponent notes as written, in every mode.

function onOpponentHit(note)
    strum[note.lane].wobbleY = -8           -- your lane twitches when the opponent sings it
    tween(strum[note.lane].wobbleY, 0, beats(0.5), "elasticOut")
end

Psych: opponentNoteHit · Kade: playerTwoSing


Keys

onKeyPress

function onKeyPress(lane)

One of your lane keys goes down - note or not. Gets lane: 0-3.

function onKeyPress(lane)
    strum[lane].scale = 0.9
end

Psych: onKeyPress(key) · Kade: keyPressed(key)

onKeyRelease

function onKeyRelease(lane)

One of your lane keys comes up. Gets lane: 0-3.

function onKeyRelease(lane)
    tween(strum[lane].scale, 1, beats(0.25))
end

Psych: onKeyRelease(key)

onGhostTap

function onGhostTap(lane)

A press with no note to hit. With ghost tapping off, onMiss comes too. Gets lane: 0-3.

function onGhostTap(lane)
    strum[lane].wobbleX = 6
    tween(strum[lane].wobbleX, 0, beats(0.5), "elasticOut")
end

Psych: onGhostTap(key)


Pause

While the game is paused or counting down, only onCountdown, onPause and onResume are called. Tweens and after / every timers run on the song's time, so they wait while it's paused.

onPause

function onPause()

The song is paused.

Psych: onPause

onResume

function onResume()

The song goes on after a pause.

Psych: onResume


In what order

Within one frame, for every loaded event - the built-ins first, then the song's own files in file-name order: the steps, beats and sections that started, then onFrame. After that come the waiting after / every functions whose time has come. Two scripts writing the same property in the same frame: the later one wins (and the event log names both).

Your own functions

You can write your own functions next to the callbacks. They take parameters, return a value, and are called like any function:

local function pop(amount)
    camera.bump += amount
end

function onHit(note)
    pop(note.judgment == "sick" ? 0.03 : 0.01)
end

More on that in Functions and values.