Getting a Dictionary value using an instance variable as key

0 favourites
  • 12 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I created a capx as an example of the question I have:


    I understand that you can get the key/value from a dictionary by using:


    I'm wondering if it is possible to get the key/value by using an instance variable value of an object. For example in the capx I posted I have a sprite that is named bunny. The bunny has 2 instance variables, one which is "type". Can you only use this method with a global/local variable or should you be able to use:


    Using the instance variable does not work in my example but I have been able to get it to work with a global variable.

  • Just remove the quotes around bunny.type

  • if you want to have the variable of an Instance than just type:


    If you want to store it in a Dictionary and get it out later, you need to create a key at first:

    Dictionary: Add key "thetypeofbunny" with value bunny.type (no quotes)

    If you want to call it you make:


  • codah I just tried that in the example I posted and it doesn't work. Can you try with my example capx and see?

  • codah I just tried that in the example I posted and it doesn't work. Can you try with my example capx and see?

    I assumed you knew how to do the rest in your actual code Have a look at the EyeForcz post. You probably don't want to add instance variables to your Dict?onary like that.. what are you trying to achieve?

    edit: oh ok, try this

  • jstar codahs methode dont work because you havent set a key for it. If you just want to call a instance variable you just type bunny,type and dont need a dictionary for that.

  • I've updated the capx, see if it's what you want.

    edit: note the 'add key' lines should be out of the loop Updated.

  • codah and

    ***EDIT*** I saw the capx you posted after I made this post and it makes sense now

    I had followed a bad example of how to properly use Dictionaries key/value pairs

  • Here is the working Capx, i just added a Text instance to show the result:

    Update: if you want to get the numbers out of the dictionary you use this methode:

    Dictionaty.dwarf without the get. Dictionary.Get("") is for keys the Dictionary.dwarf would be a Instance Variable they are 2 different things, you must be carefull with that.

  • my question was answered by codah

    Thanks to you both!!!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • try this, it now shows the values and types

  • I'm kind of working on a similiar problem. I just cant seem to get it to post the value from the dictionary. What I'm trying to do is use a dictionary with keys like "room1" -> "some text about that room" then using a global variable (text) "roomName" pass through the correct value Dictionary.Get("roomName")

    Every time i do this i get 0 as an output. I need help understanding why this is occurring.

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