How could I read strings as a value?

Get help using Construct 2

Post » Sun Jul 01, 2012 12:43 pm

If I have global variables Unlock_Level_1(1),(Unlock_Level_2(0) ,Unlock_Level_3(0)

Then how could I use a string as a value?

Set "Unlock_" & "LayoutName to 1

I tried to use int("Unlock_"&"LayoutName") but it just won't read it as a value for a variable.
B
40
S
5
G
5
Posts: 405
Reputation: 5,657

Post » Sun Jul 01, 2012 1:09 pm

Yeah, afaik you can't use a string to refer to a variable name like you can with a layout name (e.g. Go to layout "Layout " & variable).

Was looking to do that myself a while back, and the best solution I found was adding the variables to an array. (thanks to Joe7 for the suggestion)

[QUOTE=Joe7]What also should work is - if you have "levelnames" - is storing at the beginning all names in a global 1d-Array eg:

myLevelNames_Array= ['The first one','Second','Final Stage']

and load the right value on start of each layout to the current Layout:

LevelLabel- set Text -> myLevelNames_Array.At(current_level-1)

[/QUOTE]

Would that do the trick in your case?Nimtrix2012-07-01 13:13:32
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Sun Jul 01, 2012 1:21 pm

You could also use a global Dictionary object, which allows you to set and get keys by a string expression, so you can use the expression you gave above.
Scirra Founder
B
359
S
214
G
72
Posts: 22,951
Reputation: 178,578

Post » Sun Jul 01, 2012 1:28 pm

Ah, of course. I forgot about the dictionary plugin, wasn't added yet when I was looking to do this.
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Sun Jul 01, 2012 2:45 pm

Thanks @Ashley and @Nimtrix. The dictionary worked perfectly and will save a lot of work from me. nemo2012-07-01 14:46:14
B
40
S
5
G
5
Posts: 405
Reputation: 5,657

Post » Sun Jul 01, 2012 2:58 pm

what does dictionary used for? i mean examples because i really didn't know what does string mean :


i know it has keys and values but what the different between it and the global variables or arrays
B
48
S
15
G
6
Posts: 795
Reputation: 12,035

Post » Sun Jul 01, 2012 3:14 pm

With this dictionary you can create/destroy "Global variables" called "keys" on the fly and you can name them as you like.

My problem was that the global variables I had created came out as a readable text and I was unable to use them as a value to compare with anything else than other readable text. With this dictionary I'm able to store values with names and call or compare them whenever I like. I was now also able to show text 'new high score' to the player if the score was better than the previous one stored into my Dictionary "key" "HighScore_"&LayoutNamenemo2012-07-01 15:15:33
B
40
S
5
G
5
Posts: 405
Reputation: 5,657

Post » Sun Jul 01, 2012 3:21 pm

@zsangerous - did you read the Dictionary manual entry which has a description? If you ever wonder what a feature is for or what it does, looking it up in the manual should be the first thing you do.
Scirra Founder
B
359
S
214
G
72
Posts: 22,951
Reputation: 178,578


Return to How do I....?

Who is online

Users browsing this forum: Magistross, Yahoo [Bot], zatyka and 24 guests