Construct 2 LocalStorage saves variables in a similar way to the Construct 2 Dictionary. (See my Construct 2 Dictionary Tutorial if you are not familiar with Dictionaries.) The two big differences between LocalStorage and Dictionary are that LocalStorage doesn’t go away when the web browser restarts, and LocalStorage is asynchronous, not synchronous like Dictionary.
If that didn’t all make perfect sense to you, don’t worry. That’s the whole point of this tutorial. This tutorial teaches the basics of using Construct 2’s LocalStorage to store information when the game is not running. We’ll make a simple app that uses LocalStorage to store variables when the web browser is not running.
Construct 2 dictionaries store data in key-value pairs. Think of a Dictionary as a table with one column for names of values and one column for values associated with the names. If you want a value from a Construct 2 Dictionary, you request the value associated with a key (keys are sometimes called “names”). You can also have Construct 2 get all the key-value pairs, one pair at a time, and do things with them, such as print out high scores.
If you know one or more programming languages, this may all sound familiar to you. The Construct 2 Dictionary is similar to associative arrays and hashmaps found in other languages.
Names (called keys) cannot be duplicated in a Construct 2 Dictionary. For example, if you have a Dictionary of high scores, you could store key-value pairs of Doug-35, Bob-99, and John-42, but you could not have two scores named “John” in the list. For example, Doug-35, John-42, and John-33 would not work because the key “John” is used twice.
Note to Advanced Programmers: You can get around the no-duplicate-keys limitation by having multiple minion-style Construct 2 Dictionaries keyed off values from a master Dictionary. It would work like foreign keys in relational databases. I won’t go into that now, but remember, having no duplicate keys in a table is not a limiting factor. It just requires some extra planning and work.
Dictionaries are extremely useful for storing lots of related data. Dictionary use is easy for old-time programmers, but dictionaries need explaining in more detail for newer Construct 2 game developers. So, here’s a Construct 2 project example that shows the basics of Construct 2 Dictionary use.