Unity3D + Amazon GameLift: Focus on the Realtime Script

Write your own script and take control over the game server!

Alexandre Bruffa
Better Programming

--

In a previous article, I showed how to build an entire real-time multiplayer game with Amazon GameLift and Unity3D. The process implicates creating game sessions and player sessions with Lambda, guest access to the services with Cognito, and a game server with GameLift.

We could also find out that the game server’s behavior is defined by a script written in Javascript: the Realtime Script. This article will detail how this script works and how to create game server events.

Note: You can also check out the video version of this blog post

Overview of The Realtime Script

The Realtime Script is a collection of callback functions the game server calls during its lifetime. In other words, we can implement custom behavior for each of these functions. All the callback functions are described in the GameLift documentation; here are the main ones:

  • onStartGameSession(): Called when a new game session is started. In the callback function, we can store relevant information as the game server start time.
  • onPlayerAccepted(): Called when a player is accepted into the game. In the callback function, we can notify the other players that a new player has just joined the game.
  • onPlayerDisconnect(): Called when a player has left or been forcibly terminated. In the same way, we can notify the other players that the player disconnected in the callback function.
  • onMessage(): Called when a player sends a message to the server (i.e., action performed, position, etc.). In the callback function, we can send the new data to the other players.

Furthermore, the game session provides interesting functions such as:

  • getPlayers(): Retrieves a list of players currently connected to the game session.
  • newTextGameMessage(): Creates a new message containing text.
  • sendReliableMessage(): Sends a message to a player.
  • processEnding(): Terminates the game server.

All the game session functions are available in the GameLift documentation.

Creating a Game Server Event

In my previous article, I provided a very basic script making the game server act as a simple “dispatcher”. The game server notifies the players if someone joins or disconnects and resends a message sent by a player to the other players (i.e., action, life update, etc. ).

This time, we will create a non-related player event: a meteorite will fall on the map from time to time. No matter how many players are on the map or what they are doing, this event is 100% managed by the game server.

Our script contains a loop function used to check if the game session should be ended. We will integrate our new server event in the loop function:

We modify the script, adding the new feature in the loop function:

Notes:

You can find the entire code in the resources section of this article ⬇️

The Unity Client

In the Unity client, we implement the new event sent by the game server with a countdown and cool VFXs:

It looks awesome! For the meteorite and the explosion, I used assets from the Voxel Arsenal assets. I managed the explosion force field with a Sphere Collider in trigger mode:

Explosion force field

I used the OnTriggerEnter and the CompareTag functions to detect this force on the player. Then, I normalized the differential vector and applied a ratio to it. I found that a ratio of 200 creates a cool visual effect. Finally, I applied the AddForce function to the player’s Rigidbody:

Final Thoughts

At first glance, the Realtime Script can be complex and challenging to understand, but once you master it, you have total control over the game server’s behavior and can do absolutely what you want! Meteorites, snow, rain, sunshine, real-time updating environment, non-player characters, etc. The only limit is your imagination!

In the resources section, I provide a Unity package of the game I showed in the demo. The meteorite and explosion assets are not included because of the license type.

You are free to use all the resources I provided in this article. Please ping me if you use them to build your game; I would be delighted to see the result!

Resources

Here are all the resources mentioned in the article:

For the demo, I’m using the Voxel Arsenal assets.

--

--