O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – Exposing the game state to the JS engine

Before we move on to executing the players' scripts, we need to create a QJSEngine and insert some information into its global object. The scripts will use this information to decide the optimal move.

First, we add the QJSEngine m_jsEngine private field to the Scene class. Next, we create a new SceneProxy class and derive it from QObject. This class will expose the permitted API of Scene to the scripts. We pass a pointer to the Scene object to the constructor of the SceneProxy object and store it in a private variable:

SceneProxy::SceneProxy(Scene *scene) :
    QObject(scene), m_scene(scene)
{
}

Add two invokable methods to the class declaration:

Q_INVOKABLE QSize size() const; Q_INVOKABLE ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required