edit global variable using Execute javascript action(SOLVED)

Get help using Construct 2

Post » Mon Mar 03, 2014 3:56 am

@Yann
I appreciate the input Yann, it was in-depth. The reason I didn't want to make a plugin was mainly because I had all the other features in the sdk working without a plugin, so making a plugin just to run that one function is kinda over the top. I know other c2 users have started to make their own fgl plugin for the sdk (there's about 3 by now ) but it seems none of them have this properly working yet either.

I appreciate that you put effort into actually explaining why its more or less not possible this way. I guess the method I was thinking of was rather inefficient. And for optimization issues I think I may avoid executing javascript from the browser object for this function.

I have read some guides on making plugins and I may just release one, but as of right now I'm a bit busy with my first year of university. If I notice that fgl is unable to make there own and C2 users aren't able to get one working with all features, then I'll eventually make one. But this of course is when I can actually find some spare time.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Mon Mar 03, 2014 6:03 am

You can create a var in JavaScript with for example execute js( 'this.premium=1337‘ ) and just set your global with an event with eval js( 'this.premium'). That will work with minifying as well. As a rule of thumb it's not reliable to access the runtime stuff unless you use the sdk with a plugin.
B
94
S
33
G
118
Posts: 5,396
Reputation: 75,853

Post » Mon Mar 03, 2014 7:19 pm

Would indeed be a simpler approach than my bloated unreliable example =)
B
68
S
22
G
14
Posts: 1,485
Reputation: 16,561

Post » Mon Mar 03, 2014 7:35 pm

I tried to use C2 programs, inserted using userscripts, inside a page I didn't host, to add functionality to a browser game. I had to trade some data between C2 and the webpage. I used a "notification system" to dialog between the two, meaning :
- each time the JS needed to send data to C2, it would store a request (see below) in an array, using a function "sendNotification".
- a request was a string with a C2 function name (function meaning the Function plugin), and arguments, separated with a "__".
- each tick, C2 would execute a code "getNotification()" with execJS, and would be returned the string of the first waiting request, with a "this.lastRequest" evaluation like R0j0 explained. The request was then analysed and the correct function was called with the correct arguments in C2, handling the request (depending on type of request, like update life, ...)
- the other way was quite simple, as you can just execJS the functions you want in the webpage from C2.

This was for intensive exchanges, but the method can be used in other ways, including simple ones. (2 C2 games running on the same page, chatting with each other ?)
B
17
S
8
G
4
Posts: 473
Reputation: 6,221

Post » Tue Mar 04, 2014 6:42 am

@R0J0hound

Much appreciated Rojo, this is what I was looking for. I'm gonna try it later this week.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sat Mar 08, 2014 8:30 am

@Aphrodite
I really would prefer the method that you mentioned since it seems to be the most cleanest way of doing what I'm trying to accomplish. But it seems I can't call the function on C2 using the execute javascript(), or I think I'm doing it wrong, which is probably the case if it is possible.

Here's an image of what I just tried:
[img]deadlink[/img]
EDIT***** I did put a semicolon in the execute javascript command for the function, I foregot to update image.

I think this is what the function is doing:
does a function call to receive two functions, "success and failure" which is linked to two buttons in the sdk ("yes button" and "no button". When yes is clicked it does a call to the success function. The same success function sent to the fgl.inApp.initiateUnlockFunction(). When no is clicked it does a call to the failure function.

Here's the pseudo:
fgl.inApp.initiateUnlockFunction(function onSuccess,function onFailure)

@R0J0hound
It does work in general but I can't seem to get it working with fgl's function calls. Could be due to my lack of javascript knowledge. Which wasn't that great to begin with.

Here's an image of what I just tried:
[img]deadlink[/img]

Is it not working because it can't find this.premium? since it was executing from fgl's sdk? I could post the error I get from google chrome from both examples, if it helps anyone understand the problem.
Last edited by retrodude on Sat Mar 08, 2014 8:17 pm, edited 1 time in total.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sat Mar 08, 2014 2:30 pm

retrodude wrote:@Aphrodite
I really would prefer the method that you mentioned since it seems to be the most cleanest way of doing what I'm trying to accomplish. But it seems I can't call the function on C2 using the execute javascript(), or I think I'm doing it wrong, which is probably the case if it is possible.

Here's an image of what I just tried:
Image
EDIT***** I did put a semicolon in the execute javascript command for the function, I foregot to update image.

I think this is what the function is doing:
does a function call to receive two functions, "success and failure" which is linked to two buttons in the sdk ("yes button" and "no button". When yes is clicked it does a call to the success function. The same success function sent to the fgl.inApp.initiateUnlockFunction(). When no is clicked it does a call to the failure function.

Here's the pseudo:
fgl.inApp.initiateUnlockFunction(function onSuccess,function onFailure)

@R0J0hound
It does work in general but I can't seem to get it working with fgl's function calls. Could be due to my lack of javascript knowledge. Which wasn't that great to begin with.

Here's an image of what I just tried:
Image

Is it not working because it can't find this.premium? since it was executing from fgl's sdk? I could post the error I get from google chrome from both examples, if it helps anyone understand the problem.


I am wrong I think concerning the function calling, It doesn t seems to be as easy as that(we would have to know if it is possible to call a function in execute javascript), but I still think that if it is possible, it would be the best way to do it (it could help in that kind of case when people have to use a execute javascript for the API)

Sadly I don t know how to do this
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
54
S
22
G
18
Posts: 2,123
Reputation: 17,150

Post » Sat Mar 08, 2014 4:08 pm

@Aphrodite
"Sadly I don t know how to do this"

That's alright, I appreciate the input though. I'm trying to edit the javascript variable through a function in the script tag, hopefully that works.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sat Mar 08, 2014 8:16 pm

Got it working using a script tag so this thread can die peacefully. I appreciate all the help and suggestions.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Previous

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 21 guests