Lamp
A switchable lamp with a native color picker, Warm/Blue/Pink presets, and saved light and glow.
What it demonstrates: Event inputs · held power · toggle controls · color picker · shared rows · lighting.
Run this example
All learning examples are available in your developer world on scape.wtf. To edit this example, clone the public Scape tools repository and run its project with Node.js 22+:
sh
git clone https://github.com/scape-wtf/scape-tools.git
cd scape-tools
npm install
cd examples/gizmos/lamp
npm run build
npm test
npm exec -- scape gizmo dev1
2
3
4
5
6
7
2
3
4
5
6
7
This connects to https://scape.wtf. Follow connection and approval. To make an independent project, copy this example directory outside the checkout, then run npm install, npm run build, npm test and npm exec -- scape gizmo dev there. Its manifest uses published SDK and CLI dependencies. The project's npm run dev script is a shortcut for scape gizmo dev.
Try changing it
Connect the Pressure pad to Toggle or Power. Choose a custom color in Configure, then return to Warm, Blue or Pink. The picker saves on change; the light and glow share the accepted color.
Source
This code is included directly from the working example when the site builds.
src/definition.ts
ts
/** A standalone SDK lamp. The package chooses the light; Scape renders it. */
import { defineObject, exactKeys, record, requirePayload, ObjectActionError } from '@scape-wtf/sdk';
const colors = {
warm: '#ffc078',
blue: '#6baaff',
pink: '#ff80ba',
} as const;
type Color = keyof typeof colors;
const validColor = (value: unknown): value is string =>
typeof value === 'string' &&
(Object.prototype.hasOwnProperty.call(colors, value) || /^#[0-9a-f]{6}$/.test(value));
const lightColor = (color: string): string => colors[color as Color] ?? color;
/** Shared, saved state; each placed lamp has its own switch and color. */
export interface LampState {
on: boolean;
/** Existing preset names or a lowercase six-digit hex color. */
color: string;
}
export const lamp = defineObject<LampState>({
type: 'example.lamp',
version: 1,
emoji: '🏮',
label: 'Lamp',
hint: 'switch the light on or choose a color',
initial: () => ({ on: true, color: 'warm' }),
valid: (state): state is LampState =>
record(state) &&
exactKeys(state, ['on', 'color']) &&
typeof state.on === 'boolean' &&
validColor(state.color),
inputs: {
toggle: { label: 'Toggle', phrase: 'toggle', kind: 'event', action: 'toggle' },
on: {
label: 'Turn on',
phrase: 'turn on',
kind: 'event',
action: 'power',
payload: { on: true },
},
off: {
label: 'Turn off',
phrase: 'turn off',
kind: 'event',
action: 'power',
payload: { on: false },
},
power: {
label: 'Power',
kind: 'boolean',
action: 'power',
value: 'on',
combine: 'any',
locks: ['toggle', 'power'],
},
},
outputs: { power: { label: 'Power', kind: 'boolean' } },
signals: state => ({ power: state.on }),
actions: {
power: {
permission: 'participant',
run: (state, payload) => {
requirePayload(payload, ['on']);
if (typeof payload.on !== 'boolean') throw new ObjectActionError(400, 'Choose on or off.');
return { ...state, on: payload.on };
},
},
toggle: {
permission: 'participant',
run: (state, payload) => {
requirePayload(payload, []);
return { ...state, on: !state.on };
},
},
color: {
permission: 'editor',
run: (state, payload) => {
requirePayload(payload, ['color']);
if (!validColor(payload.color)) {
throw new ObjectActionError(400, 'Choose a lamp color.');
}
const preset = (Object.keys(colors) as Color[]).find(key => colors[key] === payload.color);
return { ...state, color: preset ?? payload.color };
},
},
},
view: (state, viewer) => ({
title: 'Lamp',
description: '',
rows: viewer.canEdit
? [
{
id: 'color',
label: 'Lamp color',
sizing: 'content',
elements: [
{ kind: 'field', id: 'color' },
{ kind: 'control', id: 'warm' },
{ kind: 'control', id: 'blue' },
{ kind: 'control', id: 'pink' },
],
},
]
: [],
fields: viewer.canEdit
? [{ id: 'color', label: 'Custom color', kind: 'color', value: lightColor(state.color) }]
: [],
controls: [
{
id: 'toggle',
icon: 'toggle',
label: state.on ? 'Turn off' : 'Turn on',
pressed: state.on,
action: { name: 'toggle', payload: {} },
},
...(viewer.canEdit
? [
{
id: 'custom-color',
label: 'Set custom color',
fields: ['color'],
trigger: 'change' as const,
action: { name: 'color', payload: {} },
},
]
: []),
...Object.keys(colors).map(color => ({
id: color,
label: color[0].toUpperCase() + color.slice(1),
pressed: lightColor(state.color) === colors[color as Color],
disabled: !viewer.canEdit,
action: { name: 'color', payload: { color } },
})),
],
}),
// Missing effects mean off. This runs on state changes, never once per frame.
lighting: state =>
state.on
? {
light: {
radiusCells: 3,
color: lightColor(state.color),
intensity: 0.12,
basementIntensity: 0.25,
illumination: 0.7,
},
glow: {
color: lightColor(state.color),
size: 60,
pulse: {
speed: 0,
amount: 0,
phaseX: 0,
},
opacity: {
base: 0.2,
amount: 0,
speed: 0,
phaseY: 0,
},
},
}
: {},
});
export default lamp;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
src/project.ts
ts
/** Project entry: exports lamp to Scape's SDK loader. */
import { defineProject } from '@scape-wtf/sdk';
import { lamp } from './definition.js';
export default defineProject({
name: 'Lamp',
description:
'Light up your world with a switchable lamp. Choose a color or connect it to another gizmo.',
objects: [lamp],
});1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
