How do i create a JSON string from my variables

From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • I have recently uploaded my game to kongregate, but i am having a lot of users have problems with saving/loading issues.

    I have the rex.Json plugin installed and JSONmanager installed but i can't seem to work out how to write my variables to a JSON file which exports so you can copy and paste it and store it in say a text document, then if people lose saves they can just copy and paste their JSON text into the game and i can import it.

    This obviously is quite common in a lot of games and it's more safer than the browser save.

    Any ideas how to do this? thanks

  • What are you saving/loading exactly? If it's just variables then you could just use a dictionary object and set a value for each variable and get the json with the .asJSON expression. Loading would be done by using the "load from json" action and setting the variables from each of the values in the dictionary.

    on function "save"
    --- dictionary: set key "health" to player.health
    --- dictionary: set key "x" to player.x
    --- dictionary: set key "y" to player.y
    --- function: set return value to dictionary.asjson
    
    on function "load"
    --- dictionary: load from json function.parameter(0)
    --- player: set health to dictionary.at("health")
    --- player: set x to dictionary.at("x")
    --- player: set y to dictionary.at("y")
    
    start of layout
    --- Editbox: set text to function.call("save")
    --- function: call "load" (Editbox.text)
    

    It's basically the same for any data object. You could even just do your own save format, it's not hard.

  • Thanks! this looks like what i am looking for but how are you creating player.health? you can't create a variable called player.health or object with that name

  • Sure you can. Rename a sprite to player, and add an instance variable to it called health.

  • All i get what's in the editbox is just {"c2dictionary":true,"data":{}}

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Probably need to use “add key” instead of “set key”. There are things that will silently fail so it’s best to test things as you go.

  • Ahh thanks that is working now! but i think i asked the wrong question :( but this works, i probably didn't want JSON because that is easy to edit and for people to cheat their values, how do i export it so it's like unreadable? i mean i see their export text and it's like "hkef3dsA" etc

  • A few ideas come to mind: encryption, compression, or even adding a checksum to the end to make sure it wasn’t modified.

    There may be plugins for all of them. I haven’t checked. I don’t use many plugins.

    There are probably other ways.

  • But i need it to convert my variables into a encrypted text, so if it's modified probably it won't load the save, i am thinking maybe i have to set my own one up? but it seems overly complicated, so in essence it's not a save function but more like, click this sprite and your variables will go to a textbox encrypted.

  • You’d do it as the last step. Like you’d get the json, then encrypt it. Then when you load you’d decrypt it first before loading the json.

  • so how would you encrypt it? their are no plugins in for it

  • Wikipedia is your friend for the various methods and how they are done.

    Apart from utilizing JavaScript you can do something by looping over the characters and changing them to something else.

    The simplist would be something like replace

    0 with v

    1 with 7

    2 with ;

    ...

    And so on.

    Decoding would be the reverse.

    It’s as tedius as it sounds. Athough there are shortcuts.

    Anyways I’m going to have to defer to someone else to finish answering this one.

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