Wiring Rive CharacterViewModelMachine Mood States into a React AI Assistant
By the Charmotion team · Updated · How we write guides

Connect your React chat state to the mood number on the character's Rive state machine. Create the instance with useRive from @rive-app/react-canvas, read the mood number input on CharacterStateMachine with useStateMachineInput, and set it from one function that maps each AI assistant state to a mood: 0 idle, 3 thinking, 7 talking, 4 for errors.
Key takeaways
- Your chat code produces one status name, and one function maps that name to a fixed mood number.
- The mood number goes into the number input named mood on CharacterStateMachine, using @rive-app/react-canvas.
- Charmotion characters have no one-shot triggers, so show success with happy and errors with sad.
- Set mood changes on state changes, not on every streamed token, because Rive blends between moods.
- Test all ten mood numbers in a small panel before shipping, and pin your Rive package version.
Already have a character or logo? See examples in real app screens: splash, onboarding, loading and empty-state screens, with the animation that plays in each.
How does a React AI assistant drive a Rive mood?
This guide shows how to wire Rive CharacterViewModelMachine mood states into a React AI assistant, one link at a time. The chain has four links. Your chat code knows whether a request is being sent, streaming, running a tool or failed. A small function turns that into a mood name such as thinking or talking. A React effect writes the matching number into the mood input on the character's state machine, and the Rive runtime blends the character into that mood.
Charmotion's character file already contains the pieces this needs: a CharacterStateMachine with a number input named mood, and a CharacterViewModelMachine whose number property is also called mood. Each mood is a fixed integer, so your code only sends numbers. If you have not exported a file yet, how it works lists the animations you can pick before you download the .riv.
Keep the logic in your app, not in the animation. The character should show what the assistant is doing, and the assistant's code should decide when that changes. For a broader view of the same pattern, see animating an AI assistant.
Which mood number goes with which assistant state?
Use these numbers exactly as Charmotion defines them. They are fixed in every character file.
| Mood | Number | Use it for |
|---|---|---|
| idle | 0 | Waiting for input, no request running |
| happy | 1 | A completed task, shown as success |
| wave | 2 | Greeting when a new conversation opens |
| thinking | 3 | Request sent, no tokens back yet |
| sad | 4 | A failed request, shown as an error |
| loading | 5 | A tool call or retrieval step is running |
| listening | 6 | The user is typing or speaking |
| talking | 7 | The reply is streaming in |
| sleeping | 8 | Long inactivity |
| attention | 9 | The assistant needs the user, such as a confirmation |
Characters have no one-shot triggers for success or error, so the mapping uses happy for success and sad for errors. Your code also decides when to leave a mood. Set idle again when a reply finishes, because nothing in the file does that for you.
Some moods show small accents: Z z z while sleeping, thinking dots, an exclamation mark for attention and sparkles when happy. They are on by default. The boolean input named accents hides them, which helps when the chat UI already has its own indicators.
How do you load the Rive file and read the mood input?
Install Rive's React runtime. Rive's React overview recommends @rive-app/react-canvas for most apps, and its renderer guide suggests the Canvas2D package for pages with several Rive instances. Choose the WebGL variant only if you need that renderer.
npm install @rive-app/react-canvasThe component below loads the file, finds the mood input and sends the mood number whenever the status prop changes. The state machine name must match the file exactly.
import { useEffect } from 'react';
import { useRive, useStateMachineInput } from '@rive-app/react-canvas';
const MOOD = { idle: 0, happy: 1, wave: 2, thinking: 3, sad: 4, loading: 5, listening: 6, talking: 7, sleeping: 8, attention: 9 };
export function AssistantMascot({ status }) {
const { rive, RiveComponent } = useRive({
src: '/character.riv',
stateMachines: 'CharacterStateMachine',
autoplay: true,
});
const mood = useStateMachineInput(rive, 'CharacterStateMachine', 'mood');
useEffect(() => {
if (mood) mood.value = MOOD[status] ?? 0;
}, [mood, status]);
return <RiveComponent className='mascot' />
}Two details matter. useRive returns the Rive instance and a component that draws it. useStateMachineInput returns null until the Rive object is ready and the named input is found, so the guard if (mood) is required.
Rive's React docs still describe useStateMachineInput, while some release notes mark it deprecated in favour of data binding hooks. Pin your package version, read the changelog and check the current reference before you upgrade. The package was at 4.41.0 in late 2026 listings, so check npm for anything newer.
You can also bind the ViewModel instead of the input. Rive's Web runtime exposes a viewModelInstance getter and a viewModel(path) method, and number properties are one of the supported property types. The current data binding hook names for React are in Rive's React reference, so confirm them there. This guide uses the state machine input because its hook is documented on the parameters page.
How do you map chat states to moods in code?
Put the mapping in one pure function. That keeps the rules in a single place and makes them easy to test. The example below uses field names for a generic chat object, so rename them to match the state your chat hook exposes.
function statusFor(chat) {
if (chat.error) return 'sad';
if (chat.toolRunning) return 'loading';
if (chat.streaming && chat.replyLength === 0) return 'thinking';
if (chat.streaming) return 'talking';
if (chat.userActive) return 'listening';
return 'idle';
}Then pass the result into the component from the previous section:
<AssistantMascot status={statusFor(chat)} />Order matters. An error must win over every other state, and a running tool should show loading even while text is also streaming. For a new conversation, send wave once when the chat opens, then let the function take over. For streamed replies, the path from thinking to talking is covered in animating a chat avatar from streaming events.
If you use voice, map the speech states the same way. The event mapping for a realtime voice session is in voice agent avatar event mapping, and a lip-sync approach for talking is compared in lip-sync vs amplitude.
Should mood changes switch instantly?
Rive blends between moods, so the transition is already smooth. The problem is the input. If your code writes a new mood on every token or every keystroke, the character will look restless even though each blend is clean. Treat the mood as a state, not a stream.
- Change mood on state changes only. Move from thinking to talking once, when the first token arrives, and stay in talking until the reply ends.
- Wait for typing to stop. Switch to listening after the user has been idle for a short pause, and switch back to idle when they send.
- Let errors win, then settle. Show sad for the failed request, and return to idle when the user retries or dismisses the message.
Use the same rule for every mood: one change per real event. The result feels calm and makes it easier to see in logs why the character is doing what it does.
How do you test the wiring and fix common failures?
Add a temporary panel with ten buttons, one per mood number, and click each one before you connect the chat. Confirm the character changes and returns to idle without errors in the console. This catches a wrong input name or a wrong state machine name in a minute.
Check the names in the file. CharacterStateMachine and mood must match exactly, including capitals. You can list them in the Rive editor or with a file inspector such as the Rive file inspector.
- The mood never changes. The input is still null. Confirm the file path, the state machine name and that the guard runs before you set
value. - The page errors during server rendering. Rive draws in the browser. In Next.js, render the component from a file marked 'use client', or load it with a dynamic import that turns server rendering off. The Next.js steps are in how to use Rive in React and Next.js.
- The character looks wrong after an upgrade. Compare the React package version against the changelog and test all ten moods again.
- Hooks from older examples fail. Some tutorials predate the current data binding hooks. Use the reference for the version you installed.
For the state model behind these moods, read Rive state machine inputs. If you do not have a character file yet, the character options on the AI chatbot avatar page show the animations that map to these moods.
Wire Rive mood states into a React AI assistant
- Check the character file. Open the .riv and confirm CharacterStateMachine has a number input named mood and that the mood numbers match idle 0 through attention 9.
- Install the React runtime. Run npm install @rive-app/react-canvas in your React or Next.js project.
- Create the mascot component. Call useRive with the file path and stateMachines set to CharacterStateMachine, then call useStateMachineInput for the mood input and guard for null.
- Map chat states to moods. Write one function that turns request, streaming, tool and error states into mood names, with errors checked first.
- Set the mood on change and test. Set mood.value in a useEffect when the status changes, then click through all ten moods in a test panel before shipping.
Frequently asked questions
Do I need a paid Rive plan to run this in my app?
Playback is separate from editing. Rive's runtimes are open source and play any .riv file, including the React package used here. Exported files from Rive's Free plan show a Rive splash screen, while paid plans export without it. For current plan details, see /guides/is-rive-free-pricing.
Should I use the LaunchStateMachine for the mood?
No. The LaunchStateMachine exists only when launch was picked, and it plays the launch animation once before returning to idle. Mood numbers are sent to CharacterStateMachine, which is present in every character file.
Can the same .riv file drive a SwiftUI or Flutter assistant?
Yes, in principle. Rive's runtimes for Apple platforms, Flutter, Android and React Native all play .riv files and expose state machine inputs and data binding. The mood names and numbers stay the same, but each platform has its own setup code, so check that runtime's reference.
Why does my assistant show thinking for a long time?
Thinking is set when a request is sent and no tokens have arrived. If it stays on, check that the streaming flag clears when the first token arrives and that the replyLength check in your function updates. A stuck flag in your chat code is the most common cause, not the animation.
Sources
- Rive React runtime overview, Rive
- Rive React parameters and return values, Rive
- Choose a Rive renderer, Rive
- Rive Web runtime parameters, Rive
- Rive data binding property types, Rive
- How it works, Charmotion