Double event trigger required to deduct numerical values in a dictionary

0 favourites
  • 6 posts
From the Asset Store
Game Triggers is as versatile as a Swiss army knife, and will be a great resource for any type of game you're creating.
  • Hi folks,

    Something rather strange happened to me. I'm trying to deduct 10 from a number stored in a dictionary object using the 'set-key' action.

    When I trigger this with a key press the first time nothing happens, on the second key press it works fine. (I've tested this in debug view many times)

    When triggered using a function it doesn't work at all.

    Anyone know what is going on here? Why would it need a double-input like that?

    using the int() is the only way I can get it to work at all.

    I've disabled all other events in the project to rule out anything else causing this, so I'm guessing I'm doing something wrong syntax-wise?

    Any help or general ideas on what might be going on would be much appreciated!

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's hard for me to say without seeing the project.

    I beleive Set Key requires the key to exist already, which can sometimes cause problems.

    Does this happen in a fresh project, with just the keyboard and dictionary objects?

    Lastly, it's a longshot, but do you have quotes around the number in the dictionary?

  • Hi, many thanks for taking the time to check out my issue :)

    I have managed to replicate the problem in a fresh blank project, using my original JSON file loaded into the dictionary via AJAX exactly as before - here is the google drive link for it: https://drive.google.com/file/d/1UBi2XNH8GLn-_oQmbOnOHvipaGOWKdf0/view?usp=sharing

    Something really odd happened further to this:

    I checked my JSON - the key exists and all quotation marks are there, but I noticed when viewing the JSON in C3's editor, there were none. I added some in the C3 editor view, but this returned a 0 when run. So I went back to C3's editor and removed the quotes I had just added - this then results in the code working as intended, no double trigger required! Is it just me or is that really quite odd?

  • Oh good! I'm glad you've discovered those quotes at the heart of it.

    I haven't learned to use JSON yet, but there are many here who can help with that. Hopefully someone else can chime in on that!

    But I have run into the quotes problem in other ways before. Dictionary values can be any type, and if non-numeric characters get mixed up with numeric entries, I think Construct treats it like a string (and the numeric value of a string is 0).

    There's a system condition called "Is value type" which could help check that, but the solution seems to be getting those JSON quotes sorted so your value are consistent wherever they are.

  • I've sorted it now and you were absolutely right, it's a quote thing, thanks for helping me figure it out :)

    The fix was:

    • In the JSON file use quotation marks only for the text values
    • For integer values don't use quotations marks on the numbers at all

    So all that needed doing was deleting a lot of quotation marks!

  • That's great news! And thanks for sharing your solution - we've both learned something new about json :) Good luck with the rest of the project!

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