three.js Storage minimal example

Learn how to extend our three.js driving project with the VIVERSE Storage SDK to add persistent data

This guide extends the official VIVERSE three.js Login & Auth minimal example by adding persistent data storage functionality using the VIVERSE Storage SDK. This can be used to track any number of gameplay elements, from experience points, to resources, to equip-able items. In this demo, we'll use it to track the user's score across sessions, and automatically save the score every time it's incremented.

Pre-requisite:

Before starting this guide, you should have completed the VIVERSE three.js Login & Auth minimal example which includes features such as:

  1. ✅ A working three.js vehicle controller featuring rapier.js physics

  2. ✅ A collectible pickup and scoring system

  3. ✅ Basic VIVERSE login and authentication features

Step 1: Add Storage SDK Variables

Add these variables alongside your existing authentication variables:

Step 2: Update VIVERSE Client Initialization

Modify your existing initializeViverseClient() function to also initialize the storage client:

Step 3: Update Authentication Check

Modify your existing checkAuthentication() function to capture the access token and initialize cloud save:

Step 4: Add Save Score Function

Now that the client has been initialized, and a score checked for, let's add a save score function:

Step 5: Add Load Score Function

Add this function after the save function:

Step 6: Add Save Status UI Function

Add this function to handle UI status updates:

Step 7: Update Score UI with Save/Load Buttons

Replace your existing score UI div with this enhanced version:

Step 8: Add Auto-Save to Collectible Pickup

Modify your existing collectPickup() function to automatically save when score increases:

Features Added

✅ Persistent Score Storage: Scores are automatically saved to VIVERSE cloud storage ✅ Auto-Save: Score automatically saves when collecting items and before reset ✅ Manual Save/Load: Players can manually save and load scores using buttons ✅ High Score Protection: Only loads saved scores if they're higher than current score ✅ Visual Feedback: Clear status messages show save/load operations ✅ Error Handling: Graceful error handling with user feedback ✅ Non-blocking Operations: Save operations don't interrupt gameplay

Step 9: Build and Publish to VIVERSE

That's it! That should fulfil our minimal requirements create a small mini game and track persistent scores for logged in users over time. Now we just need to build and publish.

Whatever your build process, simply zip the final build folder to prepare to upload. In the demo project we're using Vite.js, so just run npm run build to engage that. This will create all necessary static build files in the /dist folder of your project.

Once the build .zip is ready, navigate to VIVERSE Studio's Upload section, click "Manage Content" next to the app you created earlier, and use the "Upload Content" section to select the .zip.

From there, you can preview your build, or submit it for content review and approval. This process is explored in great detail in the VIVERSE Studio section of our docs if you have further questions.

Final Build

That's it! The build is ready. Here are the relevant links:

three.js example project source code:

41MB
Open

Demo scene live on VIVERSE: https://worlds.viverse.com/uSmtGgV

Last updated

Was this helpful?