How do I compress a string?

0 favourites
  • 10 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • In my game, players can make levels. The levels are then stored into a really long string.

    What's the easiest way to compress the string so that it could be sent across a server?

  • How large is your string in bytes? How large is it after compression with a standard zip archive? By what method are you interfacing with the server? I would find it unlikely that compression should be necessary, since this is not synchronous data transmission.

    If your file size is really large enough to matter, then you could also probably optimize the data being sent in the first place by only storing the necessary information.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I also want people to be able to post the string over discord without it being so long that it goes past the preview limit, so there's another reason than just sending it over the server. In bytes, the average string is about 30k large, but it can become even bigger.

    Please help me figure out how to compress the string.

  • It depends on how levels are built in your game and what information you need to store in the string. For example, if there are three object types players can place on a 50x30 grid, then you can encode the entire grid into a string 1500 characters long, where letters A-C represent an object and a dot represents an empty cell:

    A.A...BAC.....A.B..CBB.ABC.CCB....

    Another totally different approach is to upload the string to an online service like Pastebin.com

    When you upload a string, you get a unique URL, which can be used to download this string. So people can share and exchange URLs in Discord, instead of full strings.

    Both uploading and downloading from Pastebin can be done with AJAX.

  • I'd really just like to know how to compress strings please.

  • What does the 30k consist of? Before even going the compression route there may be ways to be more terse with the text generated.

    Like dop said there are ways to represent some data in a more efficient way even before compression.

    Some simple compression could be done in events with algorithms like rle and lzw.

    Or you could find a JavaScript compression library and use that with scripting.

  • A lot of it is actually backslashes. I am using dictionaries.AsJSON inside of arrays, and then the arrays are stored as JSON inside another array, so all of the "s have backslashes in order to properly store them. I don't know how I can make that any better.

    Is there any documentation on how to import a JS library into construct? I've been searching all day and haven't found anything.

  • Did you try looking in the manual?

    construct.net/en/make-games/manuals/construct-3/scripting/using-scripting/script-files

    Adding script files

    Script files can be added in the Scripts folder of the Project Bar. Existing JavaScript files (.js) can also be imported using the Import scripts option instead.

    Factorio uses zlib for compression of json data, similar to what you're trying to achieve.

    A blueprint string is a JSON representation of the blueprint, compressed with zlib deflate using compression level 9 and then encoded using base64 with a version byte in front of the encoded string. The version byte is currently 0 (for all Factorio versions through 1.1). So to get the JSON representation of a blueprint from a blueprint string, skip the first byte, base64 decode the string, and finally decompress using zlib inflate.

    Here is a javascript implementation of zlib

    github.com/imaya/zlib.js/blob/develop/README.en.md

    I think you're better off using pastebin like what dop suggested or trimming the fat out of the data as rojo said, basically make your own file format. Doing so really depends on the amount and type of data specific to your game though.

  • There's a lot of files in the link you posted (zlib). How do I know which ones to import?

  • I am using dictionaries.AsJSON inside of arrays, and then the arrays are stored as JSON inside another array

    It's a very inefficient way to store data, like you said escaping " with backslashes is probably making 80% of the entire string. I don't think compressing the string with zlib will magically reduce its size from 30KB to 1KB. I suggest you change the way the data is stored. Use just one array or JSON. Don't nest JSONs inside of JSONs inside of JSONs.

    In this game many thousands of objects are stored in a single JSON file. I'm using a string like this, which is pretty compact and easy to read:

    {
     "island1": {
     "objects": [
     {"o": "Tree,animationname","x": 100,"y": 200},
     {"o": "Rock,animationname","x": 405,"y": 112}
     ]
     },
     "island2": {...}
    }
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)