TokenAt for

0 favourites
  • 11 posts
  • 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 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!

  • 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 :).

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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 :).

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

  • 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!

  • 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.

  • Nice work, always more than one way to get the job done :).

  • ArcadEd thanks, and thanks for the help! :)

  • Sorry I didn't get back soon enough to explain what it looks like you have found now.

    Basically, the array already has all the features built in.

    Like being able to just check for 0s, and changing a value.

    ...And I really do love Tokenat, I just answered a random loot table question with this solution:

    tokenat(Monsters.loottype,int(tokenat(Monsters.Chance,random(tokencount(Monsters.Chance,",")+1),",")),",")

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