How do I call a dictionary by calling its key as global var?

  • So I have a dictionary for each level and the level's name. For example, key 0 has a value of "Jungle," key 1 has a value of "Castle," etc. I want to display both of these things in a textbox on-screen, so I use this:

    (LevelNumber is a global variable)

    For textbox, set text to:

    "Level" & LevelNumber & "-" & dictLevels.Get("LevelNumber")

    This works fine except that the "Get" portion just returns 0 (because there is no key with the name "LevelNumber," I understand this part). I want the "Get" value to choose the value for the key that is currently equal to the global variable LevelNumber, but because the key is a string, I'm having trouble figuring out if there is a way using str() or int() to get this done.

    Long story short, when it's Level 3, for example, the global var "LevelNumber" will always be 3, and I want the text to "Get" the value for Key entry #3 and display that too.

    Thank you!

  • As long as the key is "3", then str(LevelNumber) should work.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • OK, that should be good to go, but I now think the true problem lies with importing the dictionary from a JSON string. I'm having trouble successfully loading the dictionary. As other threads have concluded, "Load from JSON string" requires double quotes.

    This topic in particular:

    The dictionary string is currently entered like this (this is exactly how it is written into the dialogue box for "Parameters for dictLevelNames: Load")

    "

    {""c2dictionary"":true,""data"":

    {

    ""0"":""zero"",

    ""1"":""one"",

    ""2"":""Tower",

    ""3"":""Cave""

    }

    }

    "

    but when I check the dictionary in Debug, it still shows as having 0 keys and being empty.

  • [quote:3q53lt20]this is exactly how it is written

    ""2"" : ""Tower",

    ""2"" : ""Tower"",

  • Ah-ha! Well, I'm sorry to have taken your time, but it's finally working. I could swear that second " was there the whole time. Anyway, thank you!

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