Skip to main content
Time to complete: 40 minutes In this guide we will go through the process of integrating WebGL in a game, leveraging tools from the Sequence Stack to earn achievements and use custom ERC1155’s to play in-game.
You can play a live version of the game hereWhere the full code to this game can be found hereAnd the full template code we’ll be using for the guide can be found here
The tools will enable you to perform:
  1. Project Setup With Webpack: Enable a project structure with WebGL to be compiled by Webpack
  2. Integrate Web SDK: Allow all EOAs and Sequence Wallet to authenticate the user
  3. Deploy a Collectibles Contract: Create your own collectible contract
  4. Deploy a Remote Minter and Mint In-game Tokens: Perform gasless relayed transactions with Cloudflare workers
  5. Leverage Items In-game: Integrate collectibles in the game using the Sequence Indexer
  6. Burn In-game Achievement tokens: Burn game achievements with wagmi
  7. (Optional) Integrate Embedded Wallet Into Web SDK: Enable smooth UX without the use of signer signed messages

1. Project setup with webpack

Clone Repo

We’ll first start by cloning down a template project, which has a few WebGL based components created using three, all compiled using webpack Template WebGL JS Web SDK Starter Clone the above repo down, cd into the repo with cd template-webgl-js-sequence-kit-starter

Update .env

Create a .env file (using the .env.example) with the environment variables
And, run the following commands to run the app
Great, you should see a plane flying over water

2. Integrate Web SDK

Now that we have a project structure set up, we can integrate Web SDK

Setup App.jsx Component

Create a folder within the src folder called react and create 2 files: App.jsx and Login.jsx In App.jsx include the following code
Then, in the Login.jsx file add the following code in order to create a button at the top of the screen to login to the application

Render Component In Javascript index.js

Finally, add in the index.js import the App.jsx component, and render it to be appended to the root id element in index.html
Create a Click Handler To Call The Login Modal
Add the following code to Login.jsx component
And the following click handler code to index.js
And add these elements to your index.html
Great, now you’ll have a button that makes a modal appear

3. Deploy a Collectibles Contract

You’ll need to create a collectible from the Sequence Builder which can be accomplished with the following guide We should create 2 collections: 1 for achievement tokens and the other for the planes

4. Deploy a Remote Minter & Mint In-game Achievement Tokens

Then, in order to send transactions to the blockchain in a seamless fashion that are gasless, implement a Cloudflare Worker to mint items from a contract deployed in the previous step, ensuring that the transactions API contract address is inputed as a Minter Role We will allow there to be multiple paths to mint collectibles: a plane collectible and an achievement collectible. This is accomplished in the code by adding a key/value of isPlane to the normal cloudflare request body, and creating an additional if/else in the cloudflare worker. You can view the code for this in this github repository For this guide, we will be running all cloudflare code in a local dev envivronment, which can be accomplished by starting the cloudflare worker in the named project folder, with:

5. Leverage Items In-game

This section will be broken into 2 implementations of updating UI with in-game asset ownership changes:
  • Displaying Plane changes based on Wallet assets
  • Displaying UI changes based on Wallet assets

Displaying Plane changes based on Wallet assets

To implement changes to the game based on what the wallet asset owns, you can implement a button that mints a token, then on the response, checks for indexer changes In the index.js we include a button attached to the onclick attribute of the element in index.html
Where callContract takes care of the minting by calling a fetch that is wrapped in a mutex to ensure 1 mint happens at a time, to prevent click mashers, added to the SequenceController class in /API/SequenceController.js
and fetchPlaneTokens will poll on the result until there’s an asset in your wallet, updating the plane color to represent a different plane. fetchPlaneTokens is implemented with the following code, where the balance conditional check is greater than 1, and the tokenID is equal to the searched for id. This UI conditional logic would change based on your application

Displaying UI Changes Based on Wallet Assets

Next, we implement a UI change where we add a burn achievement button, based on if the user has an achievement or not First, implement the similiar html/js click handler logic like before where this time, the isPlane value of callContract is set to false
Note: in a real game, this minting of achievement token would happen based on some trigger event in the game, for simplicity we’ve included of button
This time, we call fetchTokensFromAchievementMint which is added to the SequenceController
This makes it so that only if there’s a balance returned from the indexer, does the display attribute make the button appear

6. Burn In-Game Achievement Tokens

Finally, to burn the achievement token, we can no longer use a cloduflare worker for actions sent to the blockchain, because when the minting was performed ‘on behalf of’ the address using the transactions API (making the msg.sender in the contract one of the relayer addresses) for this, we want to make sure the msg.sender in the contract proves ownership of the token, and is sent directly from the user. We’ll use wagmi frontend functions as well as some class composition to accomplish this.
Where burnToken is a passed in function from our react component that uses the similiar pattern of using mutexes, and we send the transaction using sendTransaction from the wagmi package, and wait for a transaction hash update to return the callback
And in our SequenceController, call the sendBurnToken function wrapped in burnToken to make the react function accessible to the rest of the application
Then, to make the burned token have an affect on the UI, we hide the button used to burn the token in the initial place, accomplished with the following code in the SequenceController
And you’re done, you can view a full example of the code here

7. (Optional) Integrate Embedded Wallet Into Web SDK

If you’d like to smooth the user journey to allow no user transaction signing across the board, you can enable an Embedded Wallet by updating your configuration of your Web SDK react component. By accomplishing this, we reduce a pop-up when burning tokens with wagmi, since rewarding achievement tokens and minting collectibles are completed using a cloudflare worker for gasless transactions. This can be accomplished by adding a few environment variables, and switching the type of connector we use. First update your .env file with the following environment secrets
Then pass these variables to your Web SDK connector in App.jsx
And that’s it, no more integration is required for transaction flows to complete
To learn more about In-Game Wallets, see here