A visit counter
Updated
A Persistable adds one to a stored count each time the scene starts, with inc, and a UI Label shows the total, or the last known value when the write fails.
Read the label, then run the example again. Without the persistence service the write fails and the label shows the offline value.
A visit counter
#pragma lifecycle(startup)
async function startup() { const store = findEntity('Visits').getComponent(PersistableComponent); const label = findEntity('Visit Count').getComponent(UILabelComponent);
try { const state = await store.inc('count', 1); label.setProps({ text: `Visit number ${state.count}` }); } catch (error) { const count = await store.get('count', 0); label.setProps({ text: `Visits: ${count} (offline)` }); }}How it works
Section titled “How it works”The script is explained step by step in A visit counter.
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.
VisitCounter.xrproj