First, we need to check whether the player provided an init function and execute it. We'll do it in the Scene::start() function:
for(int team = 0; team < TEAM_COUNT; team++) { QJSValue script = m_teamScripts.value(team); if (script.isUndefined()) { continue; } if (!script.hasProperty("init")) { continue; } m_jsEngine.globalObject().setProperty("field", m_sceneProxyValue); QJSValue scriptOutput = script.property("init").call(); if (scriptOutput.isError()) { qDebug() << "script error: " << scriptOutput.toString(); continue; } }
In this code, we use isUndefined() to check whether the code was provided and parsed successfully. Next, we use hasProperty() to check whether the returned object contains ...