Edit variables from exported script - Construct 2

1.5k Views Asked by At

I would like to access or edit game variables from a script which was designed in construct 2 and exported to html5 game

When exported it creates data.js & c2runtime.js

data.js consists of game code and variables.

function getGlobalObject(name) {
var runtime = document.getElementById('c2canvas').c2runtime;
for (var p in runtime) {
    if(Object.prototype.hasOwnProperty.call(runtime,p)) {
        var prop = runtime[p];
        if(prop === undefined) continue;
        if(prop === null) continue;
        if(typeof prop !== 'object') continue;
        if(prop.length === undefined) continue;

        for(var i = 0; i < prop.length; i++) {
            if(prop[i].parent !== undefined &&
               prop[i].data !== undefined   &&
               prop[i].sheet !== undefined  &&
               prop[i].name !== undefined) {
                // probably the global var array
                if(prop[i].name === name) {
                    // that one!
                    return prop[i];
                }
            } else {
                // no need to loop if not global var array
                break;
            }
        }
    }
}
return null;
}
function setGlobalVar(name,value) 
{
    var g = getGlobalObject(name);
    if(g === null) return;
    g.data = value;
}
function getGlobalVar(name) 
{
    var g = getGlobalObject(name);
    if(g === null) return 0;
    return g.data;
}
setGlobalVar('myGlobal',4); // here is where you set your global

based on above code I was able to access global variables using below code (thanks to Yann at scirra) but unable to edit other values which are nested. like player.hp or player.money

0

There are 0 best solutions below