Getting Started with Basement.fun in Construct

This tutorial will guide you through integrating your Construct game with the Basement.fun platform using web APIs.

You can use either events in Construct or Javascript to interact with the B3 API endpoints.

Events

Construct has a great GUI that enables developers to create games with minimal to no code. The following docs will review how to connect your game to Basement.fun using Construct's event sheet.

Key Components

You will need to add a few objects to your project to enable your Construct game to interact with the Basement.fun platform. To add objects to your project go to the Layout View, right-click in the layout, select Insert New Object, and then choose the object you want to insert into your project. After adding the object, it will be available in your event sheet.

Browser Object

The Browser object enables you to write to the console. It's not necessary, but it will come in handy when verifying data, and debugging your code.

AJAX Object

The AJAX object enables your game to interact with B3 APIs.

POST requests require you to create separate actions for each header parameter and one post to URL action for all the body parameters.

GET requests require you to combine all the parameters and values you need into a single URL to send the request to the API.

The API responses can be referenced using AJAX.LastData.

JSON Object

The JSON object enables your game to handle JSON responses. You will need to parse the JSON strings to make use of the response data.

Sample Event

This sample event will cover the POST Set Scores request detailed in the BSMT API Specs

Sample Request

  1. Right-click anywhere on the event sheet, create a function, and name it SetScore.

  2. Click add action, select AJAX, select Set request header. In the header field enter "X-Service-Method" and in the value field enter "setUserScore".

  3. Add another AJAX action and select Set request header again. In the header field enter "Authorization" and in the value field enter "`Bearer <game secret>`".

  4. Add another AJAX action but this time, select Post to URL. Enter the following

    • Tag: "setUserScore"

    • URL: "https://api.basement.fun/launcher"

    • Data: "{""launcherJwt"": ""string"", ""nonce"": ""string"", ""score"": 0}"

    • Method: "POST"

Your event should look like the image below.

Now your function is accessible as an action that can be triggered by events in your game. This is a sample request but be sure to replace your values with variables that are set by events in your game.

Sample Response

The sample response will look something like this.

{
    "success": true | false,
    "error"?: "error string",
    "newScore"?: {
        "_id": "unique id",
        "nonce": "nonce",
        "updatedAt": 23151264, //unix,
        "score": 100.235,
        "gameId": "game uuid",
        "normalizedAddress": "user lowercase address"
    }
}

Retrieving Data

In Construct, let's retrieve the nonce from the response, so we can use it to retrieve the score at a later time.

  1. First right-click anywhere on the event sheet and add a global variable named Nonce.
  2. Add an event that is triggered by your game. In my case I want to capture the user's score when they crash their bike into another bike. I added an on collision with another object condition to the biker and set the object to biker.
  3. Next add the add action, select functions, and select SetScore.
  4. Then add a JSON action, select parse, and enter AJAX.LastData in the JSON string field. This will grab the response from our SetScore request.
  5. Lastly, add a system action, select set value, choose the Nonce variable, and enter JSON.Get("newScore.nonce").
Now your Nonce variable is set to the nonce returned by the API response and your event should look something like this.

Following the same steps, you can create events for each API endpoint by reviewing all the parameters, and responses.

© 2024 Player1 Foundation

Discover B3 - the Based L3

© 2024 Player1 Foundation

Discover B3 - the Based L3

© 2024 Player1 Foundation

Discover B3 - the Based L3