Dictionary problem

1 favourites
  • 10 posts
  • Hey folks, so I am currently having some issues with the dictionary and I can't seem to find anyone else with this problem.

    Goal: I want it so that when you click on an object (objects I've grouped into a family called "Commentables" and that each have two variables: a string called DictionaryName and a number called MessageNumber), a text appears with a message from the dictionary with the key being a combination of DictionaryName and MessageNumber.

    IE: DictionaryName = TB_Hoop_, and MessageNumber = 0.

    When the dictionary goes to get the text (in the Value column), the MessageNumber increases by one, allowing for multiple clicks to provide different text to be shown about that object.

    So here's the sequence:

    Click on object

    Text becomes Dictionary.Get (DictionaryName+MessageNumber)

    Increase object's MessageNumber by 1 (for the next click)

    So my problem is this:

    For about 40% of the objects this works fine.

    For the other 60% percent, what happens is that the very first message plays fine, but upon the increase of MessageNumber, the text doesn't become the Value, it becomes "0".

    I have checked every name a hundred times and cleared any spaces or line breaks to ensure there is not an error in the Key getting mislabelled, I've even changed the Keys to other names to test it and those work fine, I've compared the objects between the ones that work 100% fine and the ones that don't and there's hardly any difference (outside of position).

    I just don't understand why my dictionary works for some and not others, even though when I change that Key to another name (that DIDN'T work before), it works fine.

    Any suggestions? I hate to jump to "It's a bug", but I just have run out of ideas on how to fix the error.

    (I'll post some screenshots to the code, too)

    Tagged:

  • Just to note, I'm also aware that both objects in the images are on different layers. I was trying to single out any changes. The layers do not change the outcome, however.

  • I'm not sure what's up there. The logic looks sound.

    You can do a check to see if the key exists and log an error if it doesn't.

    currentkey = commentables.dictionaryName & commentables.index
    [inverted] dictionary: has key currentkey
    -- set errorText to "key '" & currentKey & "' doesn't exist"

    Maybe some weird characters are getting in there? You could open your json in something like notepad++ and have it display all characters as a check too.

    It shouldn't matter but you can simplify the logic a bit too. However I don't think that has to do with the issue you're seeing.

    dropbox.com/s/ga5etfamg5en96g/dictionary_messages.capx

  • Hey bud! Thank you for the reply.

    I hadn't tried that yet, but I definitely will as soon as I can sit down with it and I will let you know how it goes. I'm a little bummed that there wasn't one of those "Did you check to make sure your computer was turned on?" type of things where I was just being silly and missing something, but I still really appreciate the response.

    Anywho, thank you again, R0J0hound! :)

  • Bummer, so I put in the check for the key, and the dictionary definitely isn't recognizing it. What's weird though is that if I go into the keys for TB_Hoop_0 and TB_Hoop_2 (the first one works, the second one doesn't) and I switch the numbers, it will still only play the message that was originally for TB_Hoop_0 (which now is TB_Hoop_2). This makes me think it's not the Key, because it CAN recognize both, just not both in the same series of moments.

    This sort of makes me think I'm not understanding some special rules about dictionaries or something, because the logic just doesn't make sense. And I've checked the value additions, there is no extra space being added there, as well as checking the Key names in Notepad++. No extra spaces or weird characters.

    The only other thing I can think of is that there might be something wrong with the message? Like, are there characters that are not allowed in the Value section of the dictionary that would make it unreadable or something?

    I'm sort of at a loss, and to be honest, I'm beginning to think I just need to add the text directly to the objects in an instance variable. I thought using the dictionary might be a cleaner method if I needed to change a lot of text quickly, but I suppose not.

  • Sorry, I know I'm being annoying about this, and at this point I just need to change tactics completely, but I'm fascinated by this error. I think I've narrowed it down to two things:

    1). Dictionaries do not allow certain characters in the Value section.

    or

    2). My dictionary name trigger isn't changing or it's checking it at a strange time between the value shift for the number and it's catching it when the name is something weird (in like, a millionth of a second or something).

    Another few things I tested (while also changing the Key name to match the new names):

    Changing the name from TB_Hoop_ to TB_Moop_ just to test the name. Didn't work.

    Changing the name from TB_Hoop_ to D just in case the underscore wasn't helping. Didn't work.

    Changing the name from TB_Hoop_ to nothing, so it was just going by the MessageNumber (0, 1, or 2). Also didn't work.

    All of these resulted in the same outcome: The first message is played, while the second and third are not recognized, then it plays the first message again.

    But then I tested the Values. I changed them to letters (Just 'D' or 'G') and that didn't work either. So I'm beginning to think if it's not an error with the Keys, and it's not an error with the Values, perhaps dictionaries do things based on position? Is it possible I'm somehow messing with how the dictionary loads Keys and Values, or that I'm just not understanding the data flow...?

    I think at this point, I'm more fascinated by what the heck I'm missing here than actually solving the problem. I think I may need to resort to just creating an instance variable for each item and reading from there, rather than using the Dictionary at all.

  • I could help you figure it out. Do you have a Discord?

  • I do, yeah. Although I always struggle adding people, lol. I just screenshotted my profile, so feel free to message, haha

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Breaking Update!

    R0J0Hound and Wilson Percivel you both helped me out a lot.

    So, when I said I used Notepad++, I think I used it wrong. Wilson took a screenshot of what you were supposed to do with it, and we found something interesting. I think it's still a bug somewhere, but I'll post it anyway for anyone who tries to do this in the future.

    Wilson suggested I replicate the problem in another project with just that code and as I was doing it, the code started to do the message thing again. 0 was popping up for seemingly random things. Using the Console commands and Notepad++, Wilson showed me that there is indeed a space in the Key section of the dictionary. However, it ONLY showed up if I had copied and pasted a key name, then edited it. I would copy and paste "TB_Hoop_" two times, then I would add the numbers after it for each message. Even though I would go into the Key and delete any spaces (there are NO spaces visible or detectable in the key), in the actual code, it reads "TB_Hoop_\n".

    My fix: If you click the Key and highlight it, completely delete it, and then click off so it deselects the Key, it resets it. Then, you just fully type in the name and it doesn't add the space.

    I still feel like that is a bug (I could be wrong), because I'd imagine if you have a huge dictionary, typing in every complex name, especially if it has redundant words in it, it would be easier to copy and paste. But when you do that, even if you remove the space, the code will still recognize that there is a space there.

    So R0J0Hound, I'm very sorry, I used Notepad++ wrong, and you were correct. There was something there I was missing. And Wilson, thank you so much for chatting me through the process! You all are hella smart and great people. Thank you!

  • Glad you were able to figure it out. Invisible trailing spaces and new lines are pretty hard to spot.

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