Skip to content

Why VideState ​

A Vide source holds one value, and Vide updates whatever is bound to it. That is all a simple component needs. A button that is only ever visible or hidden needs one source and nothing else.

Things get harder when a component has several states and every state changes several properties, and you want the change to animate. You write a tween for each property, keep track of which state the component is in, and decide what happens when a new state arrives while the last transition is still running. Then you write the same code again for the next component, and each copy drifts a little.

VideState was built to write that once. You describe the states of a component, and it moves the properties between them. I built it because I wanted my UI to feel consistent everywhere, and I did not want to rewrite the transition logic for every style of component.

When to use it ​

VideState is worth using when:

  • A component has several states that each set several properties, such as an idle, hovered, pressed, and disabled button.
  • You want those state changes to animate, with an easing and a duration that you set once and reuse.
  • You build extended components, where many components share the same properties and differ in their states. One rule can serve many looks, and rules.extendDynamic adds to it.
  • You are tired of writing the same transition logic in every component, and you want the handling of interrupted and paused transitions to be the same everywhere.

You use it through handler.onTransition, which gives you the tweened value of every field so you can set a property yourself, or handler.animate, which returns the same values as Vide sources that you bind to properties directly.

When plain Vide is enough ​

Do not add a rule and a handler for everything. Plain Vide sources are the better choice when:

  • A value changes on its own, such as a counter, a text box, or a health value. There is no state to name.
  • A component has one or two states, does not animate between them, and needs no checks. A handler would work, but a source is less to write.
  • You animate a value once, in one place. A rule has to be defined first, and for a single property that is more work than the animation itself.

VideState does not replace Vide sources. It sits on top of them: Vide still creates the Instances and owns the bindings, and VideState only manages the values that those bindings read. You can use both in the same project, and in the same component.

VideState works with Vide. It is not made for Roact or Fusion.

What it costs ​

Switching states costs little. Changing the state of a handler of a static rule takes about as long as writing the same Vide sources by hand, and reading a source through a handler takes a fraction of a microsecond more. Creating a handler takes a few microseconds, against about one for the bare sources, because it checks its states. The numbers are under Compared with plain Vide sources.

Animating costs more than a hand-written loop. A benchmark against a hand-written shared loop, which is the best case for hand-written code, found a VideState frame about 4 times as expensive at 1,000 components and about 8 to 13 times at 5,000 and 10,000. At 1,000 components animating at once, that is about 0.74 ms per frame, under 5% of a 60 fps frame. Most interfaces animate far fewer at once, so the cost is small in practice, but it is a real cost. The numbers are under Compared with hand-written code. They come from Roblox Studio only, and VideState has not been tested in production, so measure it in your own project if you will run many handlers or transitions at once.

You also have to define a rule for each kind of component. That is the price of having the states checked when the handler is created, and it is why the setup is more work than a single source.

What it handles for you ​

  • Consistent motion. Every handler tweens each field between states the same way, with a default duration of 0.25 seconds, so the motion looks the same across your interface. A connection can still set its own settings when it needs them.
  • Seamless state changes. Calling setState swaps the active state, updates the sources, and fires your change and transition listeners, so Vide reacts to the new values without extra wiring.
  • Inheritance-friendly. One rule can serve many looks, so you can use VideState in class-style code as well as in plain functional code. See Inheritance.
  • Mistakes found early. A rule checks the values of every state, and the error names the state and the field. With an annotated table of states, the analyzer reports a wrong value, a missing field, or a misspelt state before the game runs.
  • The same speed on every device. A transition takes the duration you set, even when the frames are slow.
  • No cleanup surprises. destroy stops a handler's transitions and removes its listeners, a handler made inside a component is destroyed with it, and a handler you let go of is collected. A callback that raises an error stops only its own connection.
  • Accessibility and tests. One call makes every state change instant for players who want reduced motion, and a manual clock lets a test check a transition at an exact time.

Compared to setting up animations by hand ​

By handWith VideState
Write a tween or a frame loop for every property of every componentBind handler.animate() sources to the properties, and VideState tweens them
Keep the current state in a variable and check it everywhereThe handler holds the state, and setState changes it
Work out what to do when a state change arrives mid-animationA new transition continues from the value the earlier one reached, so nothing jumps
Copy the duration and easing into each component, then edit each one to change themSet them once for a connection, or in one shared tween config
Hard-code a different duration for pressing than for hoveringGive particular state changes their own settings with tweenOverrides
Write your own pause, skip and instant-change logicpause, resume, scaleSpeed, finish, and setState with instant
Hope every state sets the same properties with valid valuesA rule validates every state when the handler is created
Disconnect every listener and loop yourself when a component is removeddestroy stops the transitions and removes the listeners
Start the items of a list one after another with your own timerseveryHandler.setState with stagger
Chain states with waits and callbacksplaySequence plays them in turn, and can loop
Add a reduced motion setting to every animationsetReducedMotion(true) makes every state change instant
Wait real time in tests and hope the values line upsetManualClock and advanceClock step transitions to exact times

Roblox's TweenService tweens the properties of an Instance, not Vide sources, so it does not fit a UI whose values come from Vide. VideState tweens the sources, so the animation follows the state and the Instance stays in Vide's hands.