A lamp anyone can switch
Updated
A point light shared with a Networkable and a point-light trait: the switch takes ownership with requestOwnership, then turns the light on or off for everyone.
Press Lamp Switch to turn the lamp off and on. In a published world the change reaches every peer.
A lamp anyone can switch
#pragma lifecycle(startup)
let lamp;let light;let net;
function startup() { lamp = findEntity('Lamp'); light = lamp.getComponent(PointLightComponent); net = lamp.getComponent(NetworkableComponent);
on('lamp/switch', async (event) => { if (event.eventType !== EventType.UIButton.Press) return; // Only the owner's changes travel: take the lamp first. if (!(await net.requestOwnership())) { log('Someone else holds the lamp'); return; } light.setProps({ intensity: light.props.intensity > 0 ? 0 : 2 }); });}How it works
Section titled “How it works”The script is explained step by step in A lamp anyone can switch.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
SharedLamp.xrproj