How does loop index work with a dictionary object key loop?

0 favourites
  • 11 posts
From the Asset Store
112 High-Quality destruction sounds for videogames
  • Hi,

    I think in construct 2 I can do the "Loop index" check and it will properly return the loop index of a loop I set with the Dictionary Object/For Each loop per key action, but I can't get this to work in Construct 3.

    I think this is due to the syntax change in C3.

    C3 Seems to force additional syntax with parentheses and "..." which I assume is to be able to enter the name of a specific loop to text for that loop, but you cant name a Dictionary object for each key loop and when I delete the parentheses so there's no name it acts as though there's no syntax problem but the check fails... it does not successfully check the loop index.

    Any ideas?

    Thanks.

  • I'm not sure if the loopindex expression works with the Dictionary.ForEachKey condition.

    Within a dictionary loop you can use Dictionary.CurrentKey and Dictionary.CurrentValue expression to access the current key/value pair for the the loop. If you want to nest loops then perhaps use local variables to hold the values of the parent loop.

  • The problem is I can retrieve the name of the key or the actual data in the key, BUT there's no way to get what loop index. :( This is the info I need.

    Is the only solution really to make a value and manually increment it in the loop?

  • An index doesn't have much meaning in a dictionary. The order of the entries is not guaranteed so you can't get/set values at an index.

    What are you using the index for? Might help come up with an alternative for you if we could know how you are using the dictionary.

  • I'm loading tile map data from a file into a dictionary object, so that I can load the data per dictionary key (as json) into each specific tile-map object (one per layer of scrolling)

    So, if I could have kept track of the loop index of the fore each key loop that would have been very clean and simple.

    The simplest solution is a variable to keep track of the loop index, which is a shame, but not the end of the world.

  • Well there's 2 options you can go for here, provided you are the one creating the tilemap data.

    The first is that you could use an array instead, with only 2 columns. One for the key and another for the value. Although this obviously makes it harder for you read a value for a specific key.

    The other option is that you can use a JSON file like so:

    {
    	"key": [ tileindex, tileid ] 
    }
    
  • That won't work for my needs, BUT can you please explain the "(...)" that is forced at the end of LoopIdex in Construct 3? I can't find any documentation of it and it's completely crippling my progress. Is it for a name of a loop index? What about all the types of loops you can't name like the For Each loop for an object?

  • It isn't forced, "loopindex" is a variadic expression ( meaning it can take any number of parameters ). Any expression that takes 0 parameters can omit the calling brackets. While it's classed as variadic it only really makes sense to use it with 0 or 1 parameters.

    I tried just typing loopindex into an expression and had no issues with autocomplete or similar?

  • So in human English you're saying I can delete the "(...)" part and it should work if there is a loop currently looping? ;)

    What do you mean by "0 or 1 parameter"? What parameter? Is there documentation? What goes there? The name of a loop or what? if so, what's the syntax?

  • loopindex only works for system loops like repeats and for loops.

    The loopindex parameter lets you specify the name of a for loop.

    loopindex does not work for Array "For each xy" and Dictionary "For each key". Each object has its own way of getting the current index by expression.

    It worked this way in Construct 2 as well.

    For your use case, an array seems more suitable.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If using a dictionary is your only solution, you might want your dictionary keys to convey useful additional information.

    You could have keys like "key_0" or "key_something" where "key" is your actual key name and the "_" suffix is some additional information that helps you identify what tilemap the data should correspond to. You can then parse the key name to retrieve the info.

    Note that this is much slower than all the other solutions proposed.

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