TokenAt for

Get help using Construct 2

Post » Thu Aug 29, 2013 12:25 am

I'm trying to use TokenAt/TokenCount to check if the player has cleared all my levels, instead of using a variable for each level.

The thing is, I have a variable called "levelCleared" that's set to "0,0,0,0,0,0,0,0,0,0,0,0", where each 0 represents a level. When the player clears the level, it changes to it to 1.

I thought about using a "For 0 to Tokencount(levelCleared,",")-1" like I saw here http://www.scirra.com/forum/for-each-tokenat_topic71638.html to check for it, but then how can I check if EVERY number equals to 1?

Sorry if this is a dumb question.

Thanks!
B
125
S
43
G
41
Posts: 849
Reputation: 28,260

Post » Thu Aug 29, 2013 12:39 am

I do something very similar with my apps for levels. I use the string of levelCleared for my webstorage. I create and pick apart that string with arrays.

In other words, I have an array called arCleared, I populate that array with the values from the LevelCleared String.

For Each X of arCleared
------Set Value at arCleared.curX to tokenat(LevelCleared, arCleared.CurX, ",")

Now you have an array populated with your levelcleared data.

For each X of arCleared
     If arCleared.at(arCleared.curx) = 1 then add 1 to a variable

Then just compare that variable to the number you want for all levels to be complete.

Might be a simpler way, but I like the versatility of arrays and having all my level info stored in one :).
B
101
S
32
G
11
Posts: 1,546
Reputation: 21,727

Post » Thu Aug 29, 2013 12:42 am

I love TokenAt, but this really looks like it would work better in an array.

Still, you could loop through the token count and add up the 1s, you'd know if you were short if the total wasn't equal to the number of levels. Or break the loop at the first 0, if you are looking to find one.
B
240
S
62
G
33
Posts: 903
Reputation: 40,589

Post » Thu Aug 29, 2013 12:48 am

Thanks for the replies!

I don't get it, why do you have an array for it if you can just use the TokenAt directly? You could use a for to check which levels have been cleared without a problem, I think.

In my case I need to check if every level has already been cleared, so the player can be awarded an achievment..

In any case, let's say I used an array to sort the variable. Still, how do I check that every item in the array is "1"?

edit: I found a way to do it that's really simple, just not very pretty. I can manually compare the variable to "1,1,1,1,1,1,1,1,1,1,1,1", and if it is, the player is awarded the achievment.

That bad part is that if I ever add more level, I'll have to change this.andreyin2013-08-29 00:50:26
B
125
S
43
G
41
Posts: 849
Reputation: 28,260

Post » Thu Aug 29, 2013 12:51 am

Well, you know how many levels there are right? Say there are 20. So when the variable = 20 you know all levels are complete.

My approach is a bit long winded :). There is a lot of flexibility with arrays that is why I use them. Like I said, might be more complex than you need :).
B
101
S
32
G
11
Posts: 1,546
Reputation: 21,727

Post » Thu Aug 29, 2013 12:53 am

Just create a global for levelsCleared and CompleteCheck, then all you need to do is edit the global variable if you add levels.

B
101
S
32
G
11
Posts: 1,546
Reputation: 21,727

Post » Thu Aug 29, 2013 1:08 am

Ah, I think I finally understand how TokenAt works. I thought I would be able to change a value in a variable directly using it but it seems that's not the case, that's why you guys were telling me to use arrays.

Well, I think I've got it figured out now.. thanks!

andreyin2013-08-29 01:08:23
B
125
S
43
G
41
Posts: 849
Reputation: 28,260

Post » Thu Aug 29, 2013 1:29 am

Oh wow, nevermind. This is so confusing!

I thought it would be a good idea to have a single variable "0,0,0,0..." and then use TokenAt to modify/retrieve a single digit, but I really don't think this works, does it?

I mean, I have this variable, then I had to create another variable and then an array only to change anything in the first variable.

I decided to give this up and use a single "levelCleared" array that has width=12. Then I can just save this array directly to WebStorage and if I add it all together like @ArcadEd said I'll have my number of levels cleared.

Edit: I found a really easy solution to check if the player has cleared all the levels; instead of checking every array slot then adding it all together, I can just check the whole array for "0" and if it has no 0's, the player receives the achievment.andreyin2013-08-29 02:02:04
B
125
S
43
G
41
Posts: 849
Reputation: 28,260

Post » Thu Aug 29, 2013 3:25 am

Nice work, always more than one way to get the job done :).
B
101
S
32
G
11
Posts: 1,546
Reputation: 21,727

Post » Thu Aug 29, 2013 3:28 am

@ArcadEd thanks, and thanks for the help! :)
B
125
S
43
G
41
Posts: 849
Reputation: 28,260

Next

Return to How do I....?

Who is online

Users browsing this forum: jezjones24 and 29 guests