JSON "Has key" + "Else" not working right?

0 favourites
  • 15 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • For some reason, putting "Else" after "Has key" will cause both "Has key" and "Else" to fire, if "Has key" points to an object that is one level down from the path's root.

    See example:

    However, putting the two events inside another "Has key"-condition that checks the path's root will cause only the "Has key"-element to fire.

    See example:

    I'm having a hard time seeing the logic behind both conditions firing unless they're placed inside another condition. I feel like the loop should iterate through them the same way in both scenarios since the "Has key"- and "Else"-events are siblings.

    Can someone shed some light on this?

    Thanks in advance.

    EDIT:

    For clarification, the JSON data is loaded in many seconds before these events are run, so I don't think it's a lag- or synchrony-related issue.

  • Did you try to invert that condition?

  • Did you try to invert that condition?

    Why would I do that?

  • as far as I know the "Has Key" is basically a loop, so the else will almost always get triggered.

    by inverting the first condition you get the "else" effect when the key is not present, and the the actual else will trigger properly when the key is present...

    I haven't actually tested that.

  • as far as I know the "Has Key" is basically a loop, so the else will almost always get triggered.

    by inverting the first condition you get the "else" effect when the key is not present, and the the actual else will trigger properly when the key is present...

    I haven't actually tested that.

    I'm not sure I'm following the logic.

    Regardless of whether it's inverted, it should be either hard true or hard false, unless something is happening in the application loop that causes some loops to return true and some to return false (for instance in case "Has Key" is asynchronous), but as far as I know that's not the case here.

  • quackgyver Could you share a sample project or JSON string? I tested this just now and it works correctly - only one event fires.

  • quackgyver Could you share a sample project or JSON string? I tested this just now and it works correctly - only one event fires.

    You need to remove "Append JSON.Get("books.title") (i.e. line #3) for the "Else"-case (i.e. line #4) to fire.

    I think if you give that a try, you'll be able to reproduce the issue.

  • This is possibly a bug with the JSON object.

    If you run in to a bug or issue in Construct 3, please post it to the GitHub issue tracker here:

    github.com/Scirra/Construct-3-bugs

    You must follow the bug report guidelines or your issue will be closed without investigation.

  • You need to remove "Append JSON.Get("books.title") (i.e. line #3) for the "Else"-case (i.e. line #4) to fire.

    Nope, without this action it still works correctly for me. The Else event is not fired if the key exists in JSON.

    dropbox.com/s/2x9jtliyiud772n/JSON_HasKey.c3p

  • This is possibly a bug with the JSON object.

    If you run in to a bug or issue in Construct 3, please post it to the GitHub issue tracker here:

    https://github.com/Scirra/Construct-3-bugs

    You must follow the bug report guidelines or your issue will be closed without investigation.

    Construct is a great product, but as far as the bug reporting goes the customer experience is not so great.

    Customers shouldn't be expected to deal with issue creation, refinement, maintenance and demanding responses from engineers who think that issues needs further refinement or be split up into additional issues.

    And like, threatening with "having my issue closed without investigation" unless I jump through all these hoops is not exactly going to entice me to want to submit bugs.

    > You need to remove "Append JSON.Get("books.title") (i.e. line #3) for the "Else"-case (i.e. line #4) to fire.

    Nope, without this action it still works correctly for me. The Else event is not fired if the key exists in JSON.

    https://www.dropbox.com/s/2x9jtliyiud772n/JSON_HasKey.c3p?dl=0

    Sorry, I just noticed that you had an AJAX-related trigger. It needs to be a continuous loop that checks for "Has key" on every loop.

    If you put your two sub-conditions on root level so that they run on every loop then it should work. :-)

    Thank you very much for the help so far!

  • That's why I asked you to share your project!

    If the events are at root level, they are executed on every tick. And if you are getting JSON data from a file or Internet, then it may not be ready during the first couple of ticks. And that's why the Else part is triggered.

  • If the events are at root level, they are executed on every tick.

    Why are they excluded?

    And if you are getting JSON data from a file or Internet, then it may not be ready during the first couple of ticks. And that's why the Else part is triggered.

    It's all local, so nothing is fetched remotely. All I'm doing is loading in a local JSON file that is embedded with the project.

    The reason why I posted this topic is because I'd like to understand how Construct loops through JSON. As far as I can tell, this information doesn't seem to be available anywhere.

    I keep running into strange scenarios like the one outlined in this thread, as well as a new one that I encountered just now where:

    Will not fire at all, but:

    Will set the global variable "debugger" to "background".

    The fact that you can't really troubleshoot JSON with breakpoints makes the whole thing challenging:

    Is there no information anywhere on how Construct loops through JSON objects?

  • > If the events are at root level, they are executed on every tick.

    Why are they excluded?

    "Executed", not "excluded". Because that's how Construct works. All events are executed on every tick, unless they are triggers or functions.

    It's all local, so nothing is fetched remotely. All I'm doing is loading in a local JSON file that is embedded with the project.

    It still may require 1-2 ticks to be fetched, or maybe even longer on a slower machine. So you should not start checking JSON keys until it finished loading!

    Try this code:

    JSON has key "maps.test" -> Browser Log "HAS key, "&tickcount
    Else -> Browser Log "NO key, "&tickcount
    

    Open browser console and check the messages. You will probably see a couple of "NO key" before "HAS key" appear.

    I keep running into strange scenarios...

    JSON.CurrentKey works only inside "JSON For Each" loop. If you need to get current path outside the loop, use JSON.path expression.

  • "Executed", not "excluded". Because that's how Construct works. All events are executed on every tick, unless they are triggers or functions.

    Sorry, I misread.

    I expect them to be executed on every loop, yeah, so that's fine by me.

    It still may require 1-2 ticks to be fetched, or maybe even longer on a slower machine. So you should not start checking JSON keys until it finished loading!

    Try this code:

    > JSON has key "maps.test" -> Browser Log "HAS key, "&tickcount
    Else -> Browser Log "NO key, "&tickcount
    

    Open browser console and check the messages. You will probably see a couple of "NO key" before "HAS key" appear.

    I actually can't get that to work at all:

    I've tried with single quotes, double quotes as well as loading the data from a separate JSON-file, but this always returns "no test".

    JSON.CurrentKey works only inside "JSON For Each" loop. If you need to get current path outside the loop, use JSON.path expression.

    Yes, I'm doing this as part of iterating through a foreach-loop.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I would not recommend using dot in the key name.

    Also, it's easier to put JSON string into a variable, then you won't have to deal with escaping all quotation marks:

    String variable j={"key": "foo"}
    
    On start of layout 
     JSON Parse j
    

    Yes, I'm doing this as part of iterating through a foreach-loop.

    You need to post your project file or at least a screenshot of the full event sheet. When you show just one or two events, it's not possible to guess what might be wrong in the rest of your code.

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