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 BSMNT API Specs.

Sample Request

1

Create Function

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

Set Service Method Header

Click add action, select AJAX, select Set request header.
  • Header field: X-Service-Method
  • Value field: setUserScore
3

Set Authorization Header

Add another AJAX action and select Set request header again.
  • Header field: Authorization
  • Value field: Bearer <game secret>
Replace <game secret> with your actual game secret token.
4

Configure POST Request

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
This is a sample request - 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:
API Response
{
    "success": true | false,
    "error"?: "error string",
    "newScore"?: {
        "_id": "unique id",
        "nonce": "nonce",
        "updatedAt": 23151264, // unix timestamp
        "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

Create Global Variable

Right-click anywhere on the event sheet and add a global variable named Nonce.
2

Add Trigger Event

Add an event that is triggered by your game.Example: To capture the user’s score when they crash their bike into another bike, add an on collision with another object condition to the biker and set the object to biker.
3

Call SetScore Function

Add the action, select functions, and select SetScore.
4

Parse JSON Response

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

Extract Nonce Value

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!

Complete Integration

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

Available API Endpoints

Best Practices

Error Handling

Always check the success field in API responses and handle errors gracefully.

Variable Management

Use Construct’s global variables to store important data like JWT tokens and user scores.

Debug Console

Use the Browser object to log important information to the console during development.

API Rate Limits

Be mindful of API rate limits and avoid making too many requests in quick succession.

Next Steps

Troubleshooting