What are the Trade-offs/Pros-Cons of using a Dictionary vs Instance Variables for enemy attributes

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hey Everyone,

    I have a very broad question about best practices for storing enemy attributes or other sprite instance object. I am making my first game and my Family "Enemies", which contains all my various enemy object types, has gotten up to 31 attributes that I'm tracking in the Family instance-variables. I've been reading about Dictionaries, wondering if they were a better way to store all the enemy attributes and figured I should make that decision sooner rather than later. So with that in mind, I'm open to any suggestions and/or explanation, but my main questions are:

    • If I have lot of enemy types with lots of differing attributes and I have an "Enemy" Family containing all of them. Is there any reason NOT to put all the enemy attributes in the "Enemy" Instance Variables, vs using the instance variables on each individual enemy object type? Does it make a difference if many of my enemy types will use unique (or only in a small subset) attributes that won't be used at all by the other enemy types?
    • Is the Dictionary vs Instance decision based on the number of variables? At what number of variables would you switch?
    • In general, what advantages do Dictionaries offer over Instance Variables?

    Thanks for your time if you respond to this.

  • That sounds messy to have all the possible enemy attributes as family instance variables. You probably just want attributes common to all enemies to be in the family and the attributes specific to certain enemies as instance variables of that type. Instance variables overall are the cleanest way to store values per instance.

    When you add a dictionary you only get one. To be interchangeable with instance variables you’d want a dictionary per instance, but that’s not super straightforward to do and can make events look more busy.

    You can put an object type and a dictionary in a container to have one per instance, but you can’t create a container with a family. Containers let events automatically pick the paired dictionary when you pick a sprite.

    Another more manual way is to create a dictionary whenever you create a sprite and store the dictionary’s uid in an instance variable of the sprite. Then you’ll need to pick the dictionary whenever you want to access it in events. You’ll probably want to pick and destroy the dictionary when you destroy the sprite too. This is useful in some situations but generally it makes events harder to read.

    So apart from having values per instance.

    Variables are nice since they are named and let you catch typos as you write them. They are also cleaner looking to use over dictionary keys.

    Dictionaries are accessed by a text string so it’s up to you to avoid typos. Accessing a value with a name you didn’t add to the dictionary will just silently give you a zero which can be a tricky to notice. With a dictionary you can also add things on the fly but that has limited usefulness. It also lets you access values by name if that’s important.

    Overall using variables or instance variables are the way to go, especially if you have values per instance.

  • It really depends on the game. Sometimes instance variables will be better, sometimes a dictionary, sometimes a combination of both.

    In terms of performance dictionaries are very fast and you won't notice any difference.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks guys, it sounds like maybe I'm overthinking it and should use the feature natively put there for that purpose until I find a reason not to. dop2000, by coincidence I happened to be reading a comment of yours from a few years ago where you said something like "Dictionaries have the benefit that you can cycle through them, which you can't do with Instance Variables". I would have to search around to find the exact quote, but do you remember what you were referring to and explain it? I didn't understand that statement at all.

  • You can use "Dictionary For Every Key" event to go through all keys. Say, to check if the character is wearing any armor:

    Dictionary For Every Key
    find(Dictionary.CurrentKey, "armor")>=0
    Dictionary.CurrentValue>0
    
  • Thanks for the explanation!

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