0 Favourites

Getting a Dictionary value using an instance variable as key

  • I created a capx as an example of the question I have:

    [attachment=0:21gwrk6q][/attachment:21gwrk6q]

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

    Dictionary.Get("")

    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:

    Dictionary.Get("bunny.type")

    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:

    bunny.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:

    Dictionary.Get("thetypeofbunny")

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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 eyeforz

    ***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 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)