useScene
useScene provides scene navigation and status. It reads from the scene store managed by SceneManager.
import { useScene } from "@carverjs/core/hooks";Quick Start #
import { useScene } from "@carverjs/core/hooks";
function MenuScene() {
const { go } = useScene();
return (
<World>
<Actor
type="primitive" shape="box" color="#facc15"
onClick={() => go("gameplay", { level: 1 }, { type: "fade", duration: 0.8 })}
/>
</World>
);
}Return Value (UseSceneReturn) #
| Property | Type | Description |
|---|---|---|
go(name, data?, transition?) | (string, unknown?, TransitionConfig?) => void | Navigate to a scene, clearing the stack |
push(name, data?, transition?) | (string, unknown?, TransitionConfig?) => void | Push a scene onto the stack (current scene sleeps) |
pop(transition?) | (TransitionConfig?) => void | Pop the top scene, return to previous |
replace(name, data?, transition?) | (string, unknown?, TransitionConfig?) => void | Replace the current scene |
current | string | null | Name of the current active scene |
stack | readonly string[] | Full navigation stack (last = current) |
isTransitioning | boolean | Whether a transition is in progress |
Navigation Patterns #
Go (clear stack) #
const { go } = useScene();
go("gameplay", { level: 1 });Destroys all scenes in the stack (unless persistent), navigates to the target.
Push (overlay) #
const { push } = useScene();
push("pause");Current scene sleeps, new scene pushes on top. Use for pause menus, inventories, dialogs.
Pop (return) #
const { pop } = useScene();
pop({ type: "fade", duration: 0.3 });Destroys the top scene, wakes the previous one.
Replace (swap) #
const { replace } = useScene();
replace("level2", { score: 500 });Replaces the current scene without changing stack depth. Use for level progression.
Transition Config #
All navigation methods accept an optional TransitionConfig:
| Field | Type | Default | Description |
|---|---|---|---|
type | "none" | "fade" | "custom" | "none" | Transition type |
duration | number | 0.5 | Duration in seconds |
color | string | "#000000" | Fade color (for "fade" type) |
fragmentShader | string | — | Custom GLSL fragment shader (for "custom" type) |
vertexShader | string | — | Custom vertex shader (for "custom" type) |
uniforms | Record<string, { value: unknown }> | — | Additional shader uniforms (for "custom" type) |
useSceneData
Access the data passed to the current scene and cross-scene shared data.
import { useSceneData } from "@carverjs/core/hooks";Return Value (UseSceneDataReturn<T>) #
| Property | Type | Description |
|---|---|---|
data | T | undefined | Data passed to the current scene via navigation |
setShared(key, value) | (string, unknown) => void | Set cross-scene shared data |
getShared<V>(key) | (string) => V | undefined | Get cross-scene shared data |
Usage #
function GameplayScene({ data }: { data?: { level: number } }) {
const { setShared, getShared } = useSceneData<{ level: number }>();
const highScore = getShared<number>("highScore") ?? 0;
const handleWin = (score: number) => {
if (score > highScore) setShared("highScore", score);
};
return <World>{/* ... */}</World>;
}useSceneTransition
Read-only access to the current transition state. Use for UI effects like dimming during transitions.
import { useSceneTransition } from "@carverjs/core/hooks";Return Value (UseSceneTransitionReturn) #
| Property | Type | Description |
|---|---|---|
active | boolean | Whether a transition is in progress |
progress | number | Transition progress (0-1) |
from | string | null | Source scene name |
to | string | null | Target scene name |
useSceneLifecycle
Register lifecycle callbacks for a specific scene. Uses the callback-ref pattern so the latest closures are always called.
import { useSceneLifecycle } from "@carverjs/core/hooks";Signature #
function useSceneLifecycle(
sceneName: string,
callbacks: SceneLifecycleCallbacks
): void;Callbacks #
| Callback | When |
|---|---|
onEnter | Scene transitions to "running" for the first time. Return a cleanup function. |
onExit | Scene leaves "running" (sleep, pause, or destroy) |
onSleep | Scene transitions to "sleeping" |
onWake | Scene transitions from "sleeping" to "running" |
onPause | Scene transitions to "paused" |
onResume | Scene transitions from "paused" to "running" |
Usage #
function GameplayScene() {
useSceneLifecycle("gameplay", {
onEnter: () => {
console.log("Gameplay started");
return () => console.log("Cleanup");
},
onSleep: () => console.log("Game paused/sleeping"),
onWake: () => console.log("Game resumed"),
});
return <World>{/* ... */}</World>;
}Type Definitions #
See Types for UseSceneReturn, UseSceneDataReturn, UseSceneTransitionReturn, SceneLifecycleCallbacks, TransitionConfig, TransitionType, and SceneStatus.