Dictionary For Each Key order question

0 favourites
  • 6 posts
From the Asset Store
Random Maze Generator with Door & Key System - tutorial capx
  • What exactly determines the order when using For Each Key with a dictionary object? It's not chronological or key based as far as I can tell.

    Currently using a dictionary to store the chat text coming from the server in my MMO with the server's chat message ID as the key to prevent accidentally duplicating lines in the chat display.

    I had thought that it was either chronological or based off the key since the chat dictionary starts with a "system" message with index 0, and all the messages that the server sends come through with non-zero indexes. When I populate my text object with the information returned from the For Each Key event, they are in the exact order the server sent them, with the system message at the top. So far, so good.

    For record keeping and to cut down on information passed from the server to the client the client stores the last message ID it received, and uses that to prompt the server to check for any messages that have a higher ID so that only fresh messages are sent on a chat update.

    I have recently added a system alert type message when the server recognizes I've uploaded a new version of the client. When the condition is right, it increments the LastChatID by .01 (giving a potential 100 messages without risk of duplicating the next ChatID's key), then inserts a new chat message using the same method I use to insert the first system message and the new incoming chat messages.

    At first glance, it works exactly as planned...the system message shows up at the bottom of the scrolling chat when it's received. However, the next time it grabs fresh chat messages, instead of the System message being between what was the previous last chat message and the new chat messages, it is instead pushed to the bottom of the text object. This means that even though it chronologically was not the last added to the dictionary nor is it's key the highest number (or string...tried both ways. And the string "100.001" evaluates to more than "100" and less than "101" in javascript and C2...I even verified this.) that system message still is the last key returned during the for each key loop.

    I'm stumped as to exactly how the order is determined. It's not a major issue at the moment, since the system message can only be generated once when I upload a new client, but I have plans for more system messages to be injected as things need to be brought to the player's attention. Fairly soon, they will have a full screen of nothing but system messages and have to keep scrolling up to see the chat or refresh their browser.

    I'm loath to use a full array for this, since it's overkill for the minimal information I store and would take a considerable rewrite of the chat handling routine.

  • Pitty. Was hoping either someone would know or a developer would comment. I am going to rewrite the whole thing anyway, but this is something that I would like to have the working knowledge to fill in gaps.

  • Yeah I'd like an answer here too. Makes sense to ask.

  • For me it just starts from the top, from the first entry and goes down. Hope this helps three years ago person

  • Well you can bet that it's just a json key pair, so it's going to be indexed by the order it was added.

    Given the complexity of the json structure I can't think of a reason why it would get reorganised for any reason at some other time.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • My guess is it would be random. Dictionaries in most languages are.

    You can confirm this by reading about JavaScript dictionaries. Depending on the implementation they could be ordered by when they were added.

    If you want it ordered you could add add the keys to an array, sort that, and loop over that.

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