map as list - always the same order?

KramerGames

  • Posts: 405
There is a block to put the keys or values of a map as a list, the documentation says:

Returns the list of [keys or values] for this map. No specific order is guaranteed. (In other words, do not count on it being the same order in which you added the entries.

Does that mean that each time I request an item it can be different? Or simply that each map has an order but I can't know which one it is before testing?
I want to use blocks like: get item #0 from keys of map as list, then get item #1 etc. to look through each key. I am worried about that checking item #1 and #2 etc will put out the same key.



A second, minor question would be why entering a value manually into the list does not see it as a number? If I make a list with key #1 (wood) and enter value 20, and then use the block "set key "wood" to "get key wood"+ 10, it will not return 30 but 2010. I have to use a block to enter the initial value as a number.
Parasites United  (Idle Parasite Game)

chrizt

  • Posts: 345
 "set key "wood" to "get key wood"+ 10 . did you use the + block for number for this ? or the & block for text
need help with behavior? game design? graphic design? just contact me
skype : christian.atin
email : christianatin27@gmail.com

KramerGames

  • Posts: 405
the number + , exactly like I would with items in a list.


This code only works if I specify the initial value with a block, not when I enter it under game attributes. Also tried addin the "as number" block but that didn't help.

« Last Edit: March 25, 2017, 10:13:43 am by KramerGames »
Parasites United  (Idle Parasite Game)

chrizt

  • Posts: 345
tried adding as number (value of oil) then + 10?
need help with behavior? game design? graphic design? just contact me
skype : christian.atin
email : christianatin27@gmail.com

Justin

  • *
  • Posts: 4716
If you iterate through the keys in a map, you're guaranteed to see each key once, but the order isn't specified. Instead of accessing them by index, you can put the "keys of map as list" block into a loop block. If you want an index as well, you can have an independently incrementing attribute for each loop iteration.

Code: [Select]
set [loop count] to 0
for each [item] in [keys of map as list]
    set [map value] to [get [key] from [map]]
    ... do something with "key", "map value", and "loop count"
    increment [loop count] by 1

Alternatively, if you call the "keys of map as list" block multiple times without making any changes to the map, I believe it will also return keys in the same order. Or you could store the result of that block in a list, which wouldn't change even if you modified the map.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

Justin

  • *
  • Posts: 4716
As for the second question, it will likely be evident if you check the code Stencyl is running for that set of blocks. You can right-click the "set" block, or the "add" block, and choose "view code" from the menu. If Stencyl is treating either part of the addition as a string, then it will treat the addition as a string addition.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

KramerGames

  • Posts: 405
Thanks a lot for your answers! I was using the conversion to list and then get the items, but it would be faster to just use the get item block. I've been using it for a bit now and so far the order has never changed within one session, but it is randomized when I compile (which is fine of course).

I didn't know about the option to right click a block to see the code, that is incredibly useful and will hopefully up my knowledge of Stencyl. The problem of entering a number and stencyl not treating it as one only seems to happen though when I enter the number directly in the game attribute window. The problem is that with maps there is no dropdwon box as with lists to choose if it is a number or text. Anyhow, I just use a scene behavior to add everything, that way I can use it to offer the player to reset the game too.
Parasites United  (Idle Parasite Game)