Dynamically chosen globals?

Get help using Construct 2

Post » Fri Feb 12, 2016 2:25 pm

My layouts are all named, "lev1", "lev2", "lev3", etc and I have global variables named "boardlev1", "boardlev2", "boardlev3" which consist of text values. How can I select a global variable based on the layout name? I managed to use %layoutname to build a string for naming for a localstorage file, but I'm not sure how to select an existing global variable with a string.
B
39
S
12
G
14
Posts: 1,110
Reputation: 11,184

Post » Fri Feb 12, 2016 3:43 pm

You should be using a Dictionary, or an Array.
ImageImageImage
B
71
S
19
G
179
Posts: 3,473
Reputation: 102,662

Post » Fri Feb 12, 2016 4:00 pm

blackhornet wrote:You should be using a Dictionary, or an Array.

I've used arrays before, not dictionaries, how exactly would I use one of those in this context?
B
39
S
12
G
14
Posts: 1,110
Reputation: 11,184

Post » Fri Feb 12, 2016 4:15 pm

Your layout number (X) is the index into the Array, or the name "boardlevX" would be the key for your data in the Dictionary.
ImageImageImage
B
71
S
19
G
179
Posts: 3,473
Reputation: 102,662

Post » Fri Feb 12, 2016 4:54 pm

blackhornet wrote:Your layout number (X) is the index into the Array, or the name "boardlevX" would be the key for your data in the Dictionary.

So, if i use "Dictionary.Get" to grab a key relative to the LayoutName, how is that worded in the expression editor?
B
39
S
12
G
14
Posts: 1,110
Reputation: 11,184

Post » Fri Feb 12, 2016 5:16 pm

Actually you don't even need a different name, just use the Layout name.
www.blackhornettechnologies.com/Constru ... tName.capx
ImageImageImage
B
71
S
19
G
179
Posts: 3,473
Reputation: 102,662

Post » Fri Feb 12, 2016 6:23 pm

blackhornet wrote:Actually you don't even need a different name, just use the Layout name.
http://www.blackhornettechnologies.com/ ... tName.capx

Okay, for a test, I'm trying to set some text to the value of a key that is the same name as my layout name. I tried str(Dictionary.Get(LayoutName)) and I tried Dictionary.Get(LayoutName) and it didn't work. What should I use for this?
B
39
S
12
G
14
Posts: 1,110
Reputation: 11,184

Post » Fri Feb 12, 2016 6:27 pm

Did you use Add key?
ImageImageImage
B
71
S
19
G
179
Posts: 3,473
Reputation: 102,662

Post » Fri Feb 12, 2016 6:31 pm

blackhornet wrote:Did you use Add key?

I used set key.
B
39
S
12
G
14
Posts: 1,110
Reputation: 11,184

Post » Fri Feb 12, 2016 6:33 pm

Add key seems to work, thanks!! :D
B
39
S
12
G
14
Posts: 1,110
Reputation: 11,184


Return to How do I....?

Who is online

Users browsing this forum: Greaver, luckyrawatlucky and 10 guests