Adding W4 to the project

Now we got a good sense of the project, let's get coding and make it work with W4 cloud.

Over the next few pages, we will:

  1. Install the W4 addon.

  2. Authenticate users.

  3. Use the W4 ORM (Object-Relational Mapping) to add a Profile table linked to users.

  4. Set up matchmaking using lobbies.

  5. Set up the game server.

  6. Set up the clients so they can join.

  7. Upload a build on W4 cloud so we can finally play.

We will mostly be working in the autoloads/game_state.gd script. This script is registered as an autoload in the start project, so it is globally available.

It serves as a mediator between W4 and our different game systems and keeps the flow of the code linear to help you follow along in this series.

../../_images/040-game-state-autoload.png

Note

If you are ever confused, you can compare your code with the file autoloads/game_state-final.gd. It contains the final code we'll have at the end of the series.

It will take some time to get results. There's quite a bit of setup we need to do before we can play the game online, as always with online multiplayer. Hang in there, and good luck!

Open the file autoloads/game_state.gd and let's dig in.

Creating a W4 Account

Follow the instructions here to create your account and request a workspace. Once that's done, you will have access to the workspace dashboard where you can set up and monitor server data:

../../_images/040.workspace-dashboard.png

Take note of the following information:

  1. Your unique url. It looks like <your-domain>.w4games.cloud/dashboard

  2. Your W4 Anonymous API Key. You can find it on the API page and save it within your game project's configuration.

  3. Your W4 Service API Key. It's also found on the API page. Be sure to keep it secret and never share it, as it gives access to all of your server's data.

We will need those pieces of information later at different steps in this tutorial. When we need them, we'll tell you where to find them.

Installing the W4 addon

Navigate to the W4 Cloud add-on releases on GitLab or locate the W4GD plugin in the Godot asset library.

If you downloaded the addon from GitHub, extract the contents of the archive file in your Godot project. You should end up with an addons/ directory and a w4gd/ subdirectory inside.

../../_images/w4-addon-location.png

To activate the plugin:

  1. Head to Project > Project Settings...

  2. Click the Plugins tab.

  3. Click the "enable" checkbox to the right of the W4GD plugin to enable it.

../../_images/w4-addon-activate.png

After closing the Project Settings window, you should see a new "W4 Cloud" screen at the top of the editor:

../../_images/w4-workspace-settings.png

Navigate to the W4 Cloud screen. This is where we will enter our base W4 information to connect to our online dashboard.

Click the lock paddle icon to allow editing. The icon should be open at the top.

../../_images/w4-addon-key-lock.png

Then, enter your information:

  • Url: your W4 url, without the /dashboard part. For example, if your dashboard is located at https://amazing-dev.w4games.cloud/dashboard, then your url is https://amazing-dev.w4games.cloud/.

  • Key: you will find this as the Anonymous API Key. To access it, from your W4 dashboard, click API in the menu on the left. The anonymous API key gives the addon access to your server's public data.

Warning: For the key, do not use the Service API Key, as it can be used to access your server's private data. Storing the service key in Godot could expose you to data breaches and allow hackers to access your server.

../../_images/w4-addon-key-location.png

Restart the Godot editor for your changes to take effect. You can use the Project -> Reload Current Project menu option to quickly restart Godot.

../../_images/godot-editor-reload-project.png

If Godot asks you if you want to save modified resources, click yes.

After restarting, head to Project -> Project Settings….

Make sure to turn on Advanced Settings in the top-right of the window. In the list on the left, scroll down to W4 Games > Game Servers, and check the Enabled box to enable W4's authoritative servers.

../../_images/godot-editor-project-settings-enable-servers.png

You're set! Next, we will add code to authenticate players.