How do I access JS object in C2?

Get help using Construct 2

Post » Sat Mar 05, 2016 5:15 pm

Is it possible to access raw JS object properties in C2?

I've fetched the object from AirConsole message. The object ATM is hardcoded and looks like below
Code: Select all
{ touch: 999 }


I'm trying to fetch this property but can't really find the way. So I tried to convert it to string so I could parse it, but it's also not that easy. Does C2 have some security parsing before? Is it possible to access/strinfigy JS object anyhow?

This is the latest thing I've tried, but it retuns 0;
Image
ImageImage
B
27
S
16
G
68
Posts: 946
Reputation: 38,601

Post » Sat Mar 05, 2016 7:29 pm

If AirConsole.Message is "{ touch: 999 }" then you could do this the get the value of "touch"
Browser.ExecJS("var a="&AirConsole.Message&";a.touch")
B
91
S
31
G
103
Posts: 5,238
Reputation: 67,762

Post » Sat Mar 05, 2016 7:56 pm

I just described the object's body. But it's not a string. It's an object. That's the point...

EDIT: ah wait... right... I'll try that. Thanks
ImageImage
B
27
S
16
G
68
Posts: 946
Reputation: 38,601

Post » Sat Mar 05, 2016 8:38 pm

Meh this is frustrating...

When AC sends this
Code: Select all
airconsole.message(AirConsole.SCREEN, {
            "message": "start"
          });


I can easily read the value ("start") just by reading AirConsole.Message in C2.

But when AC sends this
Code: Select all
airconsole.message(AirConsole.SCREEN, {
            "message": {
              touch: 999
            }


... then AirConsole.Message returns "[object Object]", which is fine, but can't fetch the properties.

Browser.ExecJS("var a="&AirConsole.Message&"; a.touch") and all other ways I have tried always return "0".

Any other suggestions? I'm kinda out of ideas.... thanks
ImageImage
B
27
S
16
G
68
Posts: 946
Reputation: 38,601

Post » Sat Mar 05, 2016 9:32 pm

You'll probably want to make a plugin to do this, or at least modify the one you use. C2 only handles strings or numbers, so the expression as is wont do it.
B
91
S
31
G
103
Posts: 5,238
Reputation: 67,762

Post » Sun Mar 06, 2016 12:21 am

Right... it handles only string or numbers... I don't really have time to study C2 plugin SDK now. I code controller manually so I figured out that the best way would be to always send just a text which will be simply C2 dictionary JSON.

Thanks for clarification @R0J0hound ;)
ImageImage
B
27
S
16
G
68
Posts: 946
Reputation: 38,601


Return to How do I....?

Who is online

Users browsing this forum: HenryPK, Syazanie Amirin and 7 guests