Arrays v. Dictionaries
-
I understand what both of them do vey well.
But why would I use a dictionary over an array? (generally speaking)
-
@GameCRAZY you could use it for a shop. The key being the item and the value bing any info(like price) it has.
The key is the keys!
-
@iTap-Development Thanks!
-
The key advantage (please excuse that VERY lazy pun) is the difference in the method of indexing, and the manner in which they hold and store.
The disadvantage (sometimes) of arrays is that they're VERY particular about the order things are stored within them, and where you put new content and remove old content from.
You must address existing locations in an array, or very deliberately insert new items in specific locations, or absolutely replace existing items in the array. Arrays are VERY strict about how they use the memory of the computer, essentially because they're very simple.
eg. If you have an array of four x (screen) positions, they are stored at index positions 0, 1, 2, 3:
0: 240 1: 480 2: 720 3: 960
You can't add an item at index location 5. It's simply impossible. You can't even add one at index 4, because that doesn't yet exist. But you can ~
append
~ item to the array. This extends out the array adding more space for itself, and makes an extra index. Now your new object is stored at index 4.And arrays are super sensitive to the order in which things are stored, being absolute sticklers for this detail is both their massive advantage (reliability and predictability) and their short coming, because re-ordering or shuffling an array requires a lot of operations. The order of objects in an array gives it structure and meaning, as you can always be sure that what you want is where you put it, in the order you put everything in, so you can always know what's next... and so can your CPU and memory system.
Modern computers do an enormous amount of "predictive" processing, and iterating over an array (let's say, adding a 10 to every item in an array) is something that they're VERY good at, as they'll quickly figure out the space between each item and that the iteration is happening, and grab whole chunks of the array early, before instructions come in, and start working on them before being explicitly asked, presuming the results are going to be needed.
This is of enormous benefits to games, where every frame requires exactly this sort of operation to move all the objects in a game world.
So if you ever get the chance to use arrays for positions, rotations, interactions, etc... then do. Use them as much as you can.
In a Dictionary, there is no sense of an index, so objects have no absolute position relative to any other object in the memory or any other form of association. The "location" that each object is stored at only has one rule, that it must be differently named to all the other locations in a dictionary. So you can't have this, for example, a Dictionary with two key names the same:
A_Key_For: Wooden Door A_Key_For: Steal Door
That simply will never work because the Dictionary is absolutely dependent on each key being unique.
So, instead, you'd name the Dictionary
Door Keys
and use a Boolean for whether or not you have this key type:Wooden_Door_Key: Yes Steal_Door_Key: No Bank_Door_Key: No
The biggest advantage of a Dictionary is this key, that it can be something meaningful to you and what you're storing. As iTap has said, one of those uses is things that have a volume. So you might have a Dictionary named:
Soft_Drink_Stock_Dictionary
Sprite: 3 Gatorade: 10 RedBull: 7 Coke: 5 Fanta: 6
And you can easily update the number of bottles/cans you have by their key value, and get that amount by using that key, too. Which you can think of as:
Get Number of Coke in Soft_Drink_Stock_Dictionary
But it's much more powerful than that simple example.
You can associate two different types of objects, however you need them to be related, and then filter based on their type whenever you need to do an operation. This is maybe somewhat similar to how tags work in @hamed's programming of the background of hyperPad, from a usage point of view.
A Grocery Store Dictionary of Product Categories might look like:
Orange: Fruit Apple: Fruit Carrot: Vegetable Banana: Fruit Chicken: Meat Salmon: Fish
And the order doesn't matter, at all.
Plus you can throw anything in there, at any time, so long as the new key (which you can choose however you like) is unique.
And you can delete items from a dictionary and not worry about the order or having an empty location.
For most everything you need Dictionaries for in a known, static state of data within hyperPad, you can use tags.
For everything that's changing during gameplay, (running out of whisky) then you're better off using Dictionaries and hand coding their changing state/volume.
-
A lazy visual metaphor...
This is an Array:
And this is a Dictionary:
-
@Deeeds Thank you so much! That is very helpful!