edit global variable using Execute javascript action(SOLVED)

Get help using Construct 2

Post » Sun Mar 02, 2014 3:45 pm

Is it possible to change the value in the global variable using the execute JavaScript action? I'm trying to integrate fgl's sdk and I'm having issues with integrating their premium feature.

Here's what I have tried so far in pseudo-code:

global variable number premium = 0;

Following this format-->Event>Action

OnTouched premiumButton > Browser.Execute javascript "fgl.inApp.initiateUnlockFunction(function(){"&premium=1&"},function(){"&premium=2&"});"

Problem:
This doesn't seem to change the global variable in Construct2 when the function call is made. It does initially make the call and create the menu to accept or cancel it but once again it doesn't change the variable when the function is called. Is there another way to accomplish this?

Such as declaring a variable within the execute javascript command?
Last edited by retrodude on Sat Mar 08, 2014 8:15 pm, edited 1 time in total.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sun Mar 02, 2014 6:33 pm

Hey,

it's really dirty what you're doing there! You can't access the global variable, cause it's stored somewhere in the runtime.
What exactly you want to do?
B
14
S
3
G
1
Posts: 255
Reputation: 2,027

Post » Sun Mar 02, 2014 7:53 pm

You can still access global variables using the execute javascript command in construct2. I'm wondering how I can directly change the value of the global variable using the javascript browser command in the events.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sun Mar 02, 2014 8:02 pm

maybe you'll find them here: this.runtime.get_all_globals
B
14
S
3
G
1
Posts: 255
Reputation: 2,027

Post » Sun Mar 02, 2014 8:27 pm

I appreciate the input. But I read that there's some compatibility issues with that command. It doesn't seem to work on all devices. I guess i'll find a workaround then.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sun Mar 02, 2014 8:33 pm

I could be totally wrong, but couldn't you try to use a function to do this? like if you can call the function in execute JS you should be able to do this with the way C2 does it.

Aka: you just set the variable with a function, but call it with the execute javascript command
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Sun Mar 02, 2014 11:25 pm

@Aphrodite

Can the execute javascript command use system function expressions from C2? I tried creating a function called "p" which set's the sprite objects opacity to 100 when called. I also created a function called"q" which sets the objects opacity to 0.

But when I tried doing

Browser.Execute javascript "fgl.inApp.initiateUnlockFunction(function(){"&functon.call("p")&"},function(){""&functon.call("q")&"});"

It doesn't work. I don't even think it can be used this way, can it?
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sun Mar 02, 2014 11:50 pm

I am having trouble understanding the:

"fgl.inApp.initiateUnlockFunction(function(){"&premium=1&"},function(){"&premium=2&"});"

{"&premium=1&"} is interrupting the string of the execute javascript I think, as well as {"&premium=2&"} so I am missing totally the purpose of this. What this {"&premium=1&"} and {"&premium=2&"} are supposed to do?

If they are supposed to set the value, then you should do
on function "p": set premium value to 1
on function "q": set premium value to 1

you might want to set return values too, but I don't know exactly so..


also:
Browser.Execute javascript "fgl.inApp.initiateUnlockFunction(function(){"&functon.call("p")&"},function(){""&functon.call("q")&"});"

It is not functon.call but function.call (don't forget the "i"), maybe it was the error

Browser.Execute javascript "fgl.inApp.initiateUnlockFunction(function(){"&function.call("p")&"},function(){""&function.call("q")&"});"

EDIT: I think that won't work, since you are doing the function calls, they'll both be executed :/
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Mon Mar 03, 2014 12:21 am

@Aphrodite
"It is not functon.call but function.call (don't forget the "i")"

I misspelled it in the forums by accident, I didn't actually misspell it in the C2 editor.

This is the javascript code I'm trying to imitate using browser execute javascript (not including the comments):

fgl.inApp.initiateUnlockFunction(
function(){
// The 'FULL VERSION!' text appears:
this.updateMenu();
},

function(){
// The unlock was canceled, the user didn't pay, or some other
// failure happened. Let's just refresh the menu to be sure it's up
// to date:
this.updateMenu();
}
);


Instead of this.updateMenu(), I would do a function call to a function in the C2 editor functon.call("q") or change the value of a global variable.

If I could either one of those things then it would have worked out.

This is the javascript code with no comments, it might be easier to read:

fgl.inApp.initiateUnlockFunction(
function(){this.updateMenu();},
function(){this.updateMenu();}
);
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Mon Mar 03, 2014 2:40 am

Changing the value of a global variables is way more complex than what you think.
Global variables in construct2 aren't javascript variables, they are objects with, amongst other things, "name", "data", "initial" properties.
They are indeed in the all_global_vars array of the c2runtime.

But the big problem is that when you export your project, a lot of that stuff gets minified.

Nonetheless, it seems there's enough to write something that can parse the runtime and find the objects which "look like" global variables and then narrow down the search on the name.
Here is what you can put in the Browser Execute javascript:
Code: Select all
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

This code gets the runtime from the canvas (assuming it has the id "c2canvas")
Then go through all the properties looking for arrays (which are objects with a length property)
And then loop through all the element of the array to find objects with parent, data, sheet and name properties.
Those properties are hopefully not minified (at least for now) and it seems enough to narrow down the search
Then just checking for the name property gives you the proper global variable object you can get/set.

As a word of advice, don't do that everytick... I don't think it's super efficient. Also... You should probably make a plugin for that kind of stuff. Since the plugin code gets minified the same way, you get something more reliable.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Next

Return to How do I....?

Who is online

Users browsing this forum: David181, jorgmaquoi, unicornspitfire and 30 guests