What just happened?

First, we ask the JS engine to create a new JavaScript array object. Then, we iterate over all entities and skip entities that were already killed. We use QJSValue::setProperty to add the proxy object of each entity to the array. We need to specify the index of the new array item, so we create the arrayIndex counter and increment it after each insertion. Finally, we return the array.

This function completes the SceneProxy class implementation. We just need to create a proxy object and add it to the JS engine in the constructor of the Scene class:

SceneProxy *sceneProxy = new SceneProxy(this);
m_sceneProxyValue = m_jsEngine.newQObject(sceneProxy);

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.