Writing a JSON object or string in a C2 text field

0 favourites
  • 8 posts
From the Asset Store
A very simple to set up ZElevation based depth of field system for your game. Fully commented.
  • Hello,

    I've been amending the socket.io plugin so I can receive JSON and pick a value and also emit it back.

    I've been successful implementing a new LastDataAtVar("variable") expression which can pick a variable from incoming JSON object.

    e.g. server does:

    this.broadcast.emit("new player", {id: newPlayer.id, x: newPlayer.getX(), y: newPlayer.getY()});

    I can handpick the values.

    However, now I'd like do something like:

    Socket On Event "move player" : Emit '{"x":"' & MyPlayer.x & '","y":"' & MyPlayer.y & '"}'

    The problem is Construct 2 does not allow me to write strings in this way because of the error checking...although it is a string.

    If I could do this, I could then just JSON.parse() it. Even better would be to write JSON directly into the field without it being a string.

    Any ideas on getting around this?

    Cheers

  • aceofpack

    You only could use string then transfer to JSON object since the parameter of expression only accepts string or number type.

    Or add some actions to create JSON object by "key:value" pair.

  • rexrainbow

    Thanks for your response.

    The problem is that the string: '{"x":"' & MyPlayer.x & '","y":"' & MyPlayer.y & '"}'

    ..is not seen as a string - CS2 doesn't like strings started with ' or using an escape character \.

    I guess i'll have to try and implement the later method you describe.

    Cheers

  • Can you not just escape the double quotes in the string?

  • No, it doesn't like the \ either.

  • "{\"y\":\"" & int(MyPlayer.Y) & "\"}" <- gives me syntax error unknown character (pointing to the \).

  • Try Double "" in string to replace single "

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Great :)

    That works:

    "{""y"":""" & int(MyPlayer.Y) & """}"

    A bit of an eyesore but it works!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)