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

Get help using Construct 2

Post » Fri Nov 18, 2016 3:59 pm

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!
B
24
S
6
G
1
Posts: 47
Reputation: 1,894

Post » Fri Nov 18, 2016 4:42 pm

As long as the key is "3", then str(LevelNumber) should work.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Fri Nov 18, 2016 5:46 pm

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: load-json-string-into-dictionary_t72836

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.
B
24
S
6
G
1
Posts: 47
Reputation: 1,894

Post » Fri Nov 18, 2016 6:05 pm

this is exactly how it is written

""2"" : ""Tower",


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

Image
B
66
S
24
G
7
Posts: 1,515
Reputation: 11,041

Post » Fri Nov 18, 2016 8:04 pm

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!
B
24
S
6
G
1
Posts: 47
Reputation: 1,894


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, dand, tinhofiel, Yahoo [Bot] and 28 guests