Saving A Dictionary within Dictionary

0 favourites
  • 10 posts
From the Asset Store
Match the similar cards with each other and free all animals!
  • Hi everyone,

    Would it be feasible to save a dictionary(with changing values) "AsJson" as values for keys of another dictionary.

    I am wanting to do this so that i would finally get a single dictionary to ultimately save & Load with Local Storage.

  • Yup this is totally possible. You can add multiple Dictionary, Array, Binary Data and JSON objects to a project. You can use these in any combination to compose complex types through serialization.

  • Thanks for replying, i tried it and yes it is possible but what about feasibility of this ... Lets say i save a 1000 Dictionary(s) data AsJson as Values for keys(1 to 1000) of a single dictionary... is it possible that it could crash the game?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's unlikely to, but that of course depends on how much data each dictionary contained. 1000 items isn't very much though.

  • It's unlikely to, but that of course depends on how much data each dictionary contained. 1000 items isn't very much though.

    Great! Thanks for clearing that up!

    Also can you please help me with another problem i am facing with dictionary and local storage...

    https://drive.google.com/file/d/1Bg8D-LAd1-kS0h_2xFzgYUorpdJuBV-Y/view?usp=sharing

    the problem is that if i

    1. take a canvas snapshot
    2. save it as a value for a key in a dictionary
    3. then use get image url action on a sprite with value dictionary.get("Key")

    it loads the image successfully into the sprite

    but if i do 1 and 2 then save the dictionary as a local storage key,

    1. then restart
    2. load dictionary from that local storage key,
    3. then get image url action on a sprite with value dictionary.get("Key"),

    it doesn't work.

    Can you tell why?

  • The CanvasSnapshot expression returns a Blob URL for the snapshot. Blob URLs only work as long the blob they are associated, when the preview is refreshed the Blob for the snapshot is deleted. So all you are actually storing is an ID number for a Blob that no longer exists.

    If you wish to store it I would recommend using the BinaryData plugin to convert that Blob URL into a Base64 string. Base64 is a text based encoding of the binary data itself, it is not as efficient as a Blob URL but can be serialized. I updated your example to do this:

    dropbox.com/s/g8p8xii1k4aavls/SaveImageTest.c3p

  • Got it! Thanks.

  • Base64 strings can use up substantial memory, particularly if you have a lot of them. It's worth considering this ahead of time in case it will cause issues.

    Base64 uses ~33% more memory than equivalent binary data. Additionally as it's been placed in a JS string it will use twice that again ( JS strings use a 16 bit character encoding, even though base64 values only use 7 bits ). So for instance if you have 30 bytes of binary data, it will take 40 bytes to store it as base64 and a JS string of that base64 data will use 80 bytes.

  • Thanks for that crucial info,

    Just to be clear though, Lets say an image's actual size is 100kb... does that means when i am saving it to local storage through the code you gave... it is taking up local storage of 100*(80/30)kb ~ 260kb ?

    Edit: Or does it have to do anything with dimension of image?

  • Shubi that's an interesting question.

    I'll answer your second question first. While the dimensions of the image does affect the size once encoded, I think it is being stored as a PNG file. PNG files deal well with repeated data, so large blocks of colour and small palettes can massively reduce the file size. This can mean an image with large dimensions can take up less space than a smaller one.

    Now for the original question it's important to consider that you can store things in 2 ways with the local storage plugin. A piece of binary data ( which is basically what an image is ) can be stored as an a array of bytes ( N ) or a Base64 String ( N * 1.33 * Bytes per character ). Note that the latter is multiplied by the bytes per character, so is dependent on what character encoding is being used underneath. I don't actually know what is used in this situation, and it might vary between browsers.

    The best solution is to probably just store the binary data for the image in local storage without converting it to anything, as you know exactly how many bytes will be stored.

    The convoluted option would be to do the text encoding yourself with the BinaryData plugin, like so: BinaryData.SetFromText(BinaryData.GetBase64()) and stash it. As BinaryData uses UTF-8 for text you know it would only use 1 byte per character.

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