PlayCanvas Leaderboard minimal example

Learn how to combine the VIVERSE Leaderboard SDK with PlayCanvas UI then publish the project via VIVERSE Studio

Leaderboards allow players to compare their performance against others, increasing engagement time and replayability. VIVERSE's Leaderboard SDK can be combined with PlayCanvas' powerful screen- and screen-space Element UI systems to provide an end-to-end solution for this in your game.

Pre-requisite #1: Create a World, App ID and Leaderboard in VIVERSE Studio

All SDK usage requires an App ID tied to a specific VIVERSE World, which can be created via VIVERSE Studio. This process is described in detail in our documentation on VIVERSE Studio.

NOTE: because VIVERSE SDKs require an App ID, this means VIVERSE SDKs cannot be used with projects published via the PlayCanvas Create SDK extension, which do not have App IDs.

After creating your world, navigate to its "SDK Settings" tab, and click Add New Leaderboard. In the Leaderboard Configuration section, define the necessary leaderboard parameters for this world.

Pre-requisite #2: Clone the PlayCanvas Scrolling UI tutorial

PlayCanvas already maintains many great tutorials for their engine, including a scrolling UI system with dynamic sizing. We'll click the 🍴 Fork button to clone this project to use as a starting point and ensure our UI practices are standard for the engine. This already has a few scripts for adding UI entries to a list based on a PlayCanvas template.

Step 1: Add the VIVERSE SDK as an external script

Once the project is forked, go to the new project Settings in the PlayCanvas editor, and in the EXTERNAL SCRIPTS menu, add one URL entry, and point to https://www.viverse.com/static-assets/viverse-sdk/index.umd.cjs as in this screenshot. This will ensure the VIVERSE SDK is loaded first and that your PlayCanvas logic has full access to its global methods.

Initially, we'll also increase the width of the UI column to allow for displaying names, and I'll replace the "Add Entry" UI button on top with a simple label that says "High Scores".

Step 2: Login and Auth

The player must be logged into VIVERSE to either request leaderboard data or to submit it. So this example first checks if the user is logged in, and if not, forces login through VIVERSE's "single sign-on" (SSO) redirect loop.

We'll add a new script called leaderboard.mjs to manage the VIVERSE SDK services and handle authentication. These code comments also explain the significance of each line, but this process is described in detail in our documentation, Login & Authentication for the SDK, as well.

Step 3: Set up the VIVERSE `gameDashboardClient`

Now we should have valid credentials to make requests to the Leaderboard SDK. Let's add the gameDashboardClient using our access token, then write a function to fetch high scores from the Leaderboard SDK.

Step 4: Fetch high scores and tie response to PlayCanvas UI elements

Now we should call getLeaderboard() with a valid config object, get a Leaderboard Response back, then tie that data into our PlayCanvas UI. One we have a response, the Dynamic UI Scrolling View tutorial already contains UI elements for a scrollable list of score values of arbitrary length, including an event to spawn a new instance of a "score" value with template UI. I modified the base template to include a left-aligned text element for the player's name, and a right-aligned text element for score, and fired the addScoreEntry event containing that data.

To summarize, we fire this.getHighScores() on button click, and reference the button by attribute as this.getLeaderboardButton. this.scrollControllerEntity is another attribute reference set in the editor, responsible for spawning new score entries. And this.contentEntity is one of its children that will contain all UI template instances. We reference each of these in our scripting and in the editor like so:

Then add the click callback in the editor, anywhere within the initialize() lifecycle hook, to run the this.getHighScores() function we defined above.

With those references set, we're getting leaderboard data on click, then firing the addScoreEntry event on every ranked score returned in the Leaderboard Response according to the request configuration!

The PlayCanvas demo we forked already includes an event listener that spawns a new UI element template, which we can modify slightly to handle our leaderboard data structure:

We'll leave one instance of the score entry UI template in the scene on initialization, and change its text value from blank to "Fetching scores..." — this entry will be cleared as soon as actual scores are returned from the SDK. We can add a button to run this getHighScores() function on click, as well.

Step 5: Submit a score

That should function well, but until we submit some high scores, the list will return empty and nothing will display! Let's add another button to "Submit Score," reference it with an attribute in our script, and set a click callback.

This requires adding two more attribute references and setting them in the editor:

Then add another click callback in leaderboard.mjs — and again, it can be anywhere within the initialize() lifecycle hook.

Step 6: Export and Publish to VIVERSE

That's it! That should fulfil our minimal requirements to use the VIVERSE Leaderboard SDK along with PlayCanvas UI. Now we just need to export and publish.

In PlayCanvas' Publish/Download menus, choose the "DOWNLOAD ZIP," then set your export options and click the first grey "DOWNLOAD," which will prepare you a build, exposing the final orange "DOWNLOAD" button after a few seconds.

Once the build .zip is downloaded, 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

The build is ready. Here are the relevant links:

Public PlayCanvas project: https://playcanvas.com/project/1373864/overview/viverse-leaderboard--playcanvas

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

Last updated

Was this helpful?