Tag Archives: GraphQL

MQTT for Gaming?

MQTT for Multiplayer Games? I am not a Game-Developer and I do not know how they create multiplayer internet games. But I know that MQTT can be used for that. Why?

# IoT turns to IoP

MQTT is used for connecting devices. Think about using MQTT for connecting players. “Internet of Things” (IoT) will turn to „Internet of Players“ (IoP).

# A Player is like a Device?

In a multiplayer game, each player is like a device. When they join the game, it’s like they’re saying “Hello, I’m here!” to everyone else – this is known as a “birth message” in MQTT terms.

# Sharing Information Seamlessly

As players move around in the game, they keep sending updates like their location, healthy state, collected goodies, …. MQTT acts like a messenger, picking up this information and delivering it to everyone else playing. 

# Handling Player Exits

What if a player leaves the game intentionally or unintentionally? MQTT has a smart feature called “last will message.” It’s like a goodbye note that tells other players someone has left the game. This way, everyone stays in the loop.

# Central Management 

A central game management connected to the central MQTT broker, written in any kind of language, could be used to observe and control the game and all the players.

# Why MQTT?

1. **Real-Time Updates**: It’s fast and perfect for real-time games.

2. **Reliable**: Even if a player’s connection is shaky, MQTT makes sure messages get through.

3. **Efficient**: It doesn’t eat up much data, so players won’t lag.

4. **Simple**: It’s not complicated to set up and to use.

# Conclusion

Using MQTT in multiplayer games is like having a super-efficient mailman who ensures everyone knows what’s happening as it happens.

For Unity there is a „Unity for MQTT“ and a “GraphQL for Unity Pro” asset. Easy to use and usable with all the main build targets, including WebGL!

See the game demo I made with MQTT.. I also integrated data from WinCC Unified in the demo, with the GraphQL for Unity Pro Asset.

You can try it out: https://server.rocworks.at/unity/game/ (could be already removed when you try to access it)

3D charts with Unity3D and WinCC Unified

With WinCC Unified V19 we got the feature to read history values of logged tags with GraphQL 👍 I have now added this functionality to the GraphQL for Unity Pro Asset 🤝 

🎥 With that done it is now also very easy to read the history of logged tags in Unity3D. See the video, there we read and visualize the history values of a tag when the application starts. And then the 3D chart gets updated with real-time values coming from a GraphQL tag subscription. 

ℹ️ The GraphQL for Unity Pro Asset has a WinCC Unified interface on top of GraphQL to make the access to WinCC Unified data in Unity even more easier, you do not need to implement the GraphQL queries by your own. 

👉 You can try the demo in the browser! This is possible because the asset als supports WebGL builds and because GraphQL is transported over HTTP&Websockets. Here it is. But it might happen that this link will not work anymore when you read this post.

Note: Use the PWA WebGL Template so that the application adjusts to the size of your browser window. Or adjust the settings in the generated index.html page:

<canvas id="unity-canvas" width=auto height=auto tabindex="-1" style="width: 100%; height: 100%; background: #231F20"></canvas>

💡 Get a copy of WinCC Unified, connect your machines to it and build your Unity3D application with the data from WinCC Unified! 

Example code how to read the last 100 values of a tag.

Connection.LoggedTagValues("PV-Vogler-PC::HMI_Tag_1:LoggingTag_1",
   endTime: DateTime.Now,
   maxNumberOfValues: 100
).ContinueWith((task) => {
   Debug.Log(task.Result.Count); 
}

GraphQL for Unity and how to set headers in code…

If you use the GraqhQL for Unity asset and if you want to set the headers on HTTP or Websockets in the code, then you must use the the GraphQLHttp or GraphQLWebsockets class. At the base class “GraphQL” there is no “Headers” property!

public GraphQLHttp MyGraphQL;

void Start()
{        
    if (MyGraphQL != null)
    {
        MyGraphQL.Headers = new List<Header>()
        {
            new Header() { Key = "token", Value = "secret"}
        };
        ...

Then you can also execute a GraphQL query in the code.

MyGraphQL.ExecuteQuery("query { test }", null, (message) =>
{
    if (message.Type == MessageType.GQL_DATA)
    {
        Debug.Log("Data: " + message.Result.ToString());
    }
    else
    {
        Debug.Log("Error: " + message.Type);
    }
});
      
   

GraphQL on Unity and Result Event for Data and Errors…

The GraphQL for Unity Asset can be used to execute GraphQL queries in Unity. The result is set on properties of Unity objects and there is also a Unity Event on the GraphQL Query Object where user-defined function of a GameObject can be triggered every time when a queries returns data or an error.

public class Sample1 : MonoBehaviour
{
    public void ResultEvent(GraphQLResult result)
    {
        if (result.Errors.Count==0)
        {
            Debug.Log("Data: " + result.Data.ToString());
        }
        else
        {
            Debug.Log("Error: " + result.Errors.ToString());
        }
    }
}

Create a GameObject with this class as component and then you can drag and drop this GameObject to the Query GameObject and select the “ResultEvent” method. Every time when the query is executed then this function will be called with the result data (or error data).

GraphQL on Unity & Newtonsoft 12.0.0.0 Reference Error

The GraphQL for Unity Asset uses the Newtonsoft version which is built in newer version of Unity (2020+). If you get an error like this:

Assembly 'Library/ScriptAssemblies/Assembly-CSharp.dll' will not be loaded due to errors: Reference has errors 'GraphQL'.
 
Assembly 'Assets/GraphQL/Libs/GraphQL.dll' will not be loaded due to errors: 

GraphQL references strong named Newtonsoft.Json Assembly references: 12.0.0.0 Found in project: 12.0.1.0.

Assembly Version Validation can be disabled in Player Settings "Assembly Version Validation"
  • Then you can try to remove NewtonSoft dependency from your project – if you have a dependency. And just use the NewtonSoft version which is already built in and shipped with Unity 2020+. If you use an older version of Unity then you can try to copy the NewtonSoft DLL from 2020 and use this in your older Unity version.
  • Or you can go to Project Settings -> Player -> Other Settings and deactivate “Assembly Version Validation”
  • Install the newtonsoft package from Unity. Go into the package manager and click “+” and then select “Add package from git URL” and fill in “com.unity.nuget.newtonsoft-json”.