Multiple AJAX calls in For Loop sub events

0 favourites
  • 13 posts
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • I understand that I must put For Loops as subevents so they do not work every tick. However, in my structure, I have

    On start of layout

    For loop from 1 to Couplets

         If find(RandomNumbers, SearchRN) > 0

              Set Duplicate to 1

              Add 1 to Couplets

         If Duplicate = 0

              AJAX: Request "http://...."

    Now, I want to get the data from the AJAX request, but it will not allow me to add that trigger anywhere within the above loop. It says it won't allow multiple triggers. It only allows me to add the AJAX "completed" condition outside the loop and outside the start of layout. So how do I get the results from multiple AJAX calls in a loop?

    Project is pennyhaynes.com/testgames/Array/Array.capx

    It's probably something simple I am unable to see, but I am stuck.

  • The For Loop should be nested under the Start of Layout above - it is in the capx.

  • I think you won't be able to do that because there's no way the answer to your AJAX calls will happen in the same tick you call them.

    And a loop is supposed to happen in one tick.

    So you should just throw your calls.

    And retrieve them afterward:

    Global variable inc=0
    AJAX: On "call" completed
        -> Array: set value at inc to AJAX.LastData
        -> System: add 1 to inc
    AJAX: On "call" error
        -> Array: set value at inc to "error"
        -> System: add 1 to inc

    Then you just have to read the answer in another loop

    (I never used the AJAX object but reading the Action/Condition/Expression I think it should work)

  • If remember well, jQuery's AJAX calls are asynchronous, so you are never sure when they return. Perhaps a plugin with synchronous AJAX calls is needed (but the bad thing is that's going to stall everything...)

  • Thanks, guys. Just wanted to make sure I wasn't going crazy. It is impossible to make multiple AJAX calls via loops then. OK. Will have to simply grab all the data at once on the php page and parse it via loop.

  • Normally you can do multiple calls. Use the "tag" property of the requests to differentiate them.

    Then you have to make a Ajax.OnComplete event (as Yann described) using once again the tag to differenciate and know what the data is supposed to be.

    ex: Ajax.OnComplete("authentification") would contain the return data sent by the server after it received a Ajax.Request("authentification").

    The OnComplete is to be out of the loop, since it's an event that will get triggered as soon as the server returns the result of the request.

    This should be documented in the manual by the end of the year.

  • Thanks for the reply, but if I can't catch the OnComplete during the loop, then what you are saying is that I have to create 30 different tag names for each call and 30 different OnCompletes outside of the loop?

    Just trying to clarify.

  • Basicly yes.

    But on the other hand, are you really going to have 30 requests ?

    Do you really need them ?

    You can concatenate datas on the server-side and serve several variable values in one request.

  • i think what there saying is, make a variable of the "tag" and create them in a loop and call for the data

    then you have all the tags and you know wich one is what:

    on "tag1" complete set data1 to lastdata

    on "tag2" complete set data2 to lastdata

    and maybe this plugin can help you...

    link

    yes this will work i think, you only need one 'oncomplete' for all the requests

  • Thank you. I will check out the plugin and consider the loop. I HAVE circumvented it by doing all of the searching in php right now and then parsing the data in the game's array. But I really want to learn everything that this can (and can't) do. :-)

  • ive implemented it and works but will redo it, its better and faster just having 1 call, now its slow (like 5-8 seconds), maybe its just a bad host i dont know...   

    but if you need help with the loop, just ask!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • on completed loop("tt") condition would be really nice, or some way to place events after a loop. Either one is really really really needed.

    My situation is I have an ajax call that runs a loop taking tokens and once the process is completed I want the game to start. But there doesn't seem to be a way to trigger this except to maybe do a hack where is the a token is found that equals "START" then start the game. This seems rather lame hack because it requires that the received data from the server is correctly containing "START" at the end or the game will fail.

    attached is a screen shot modified to show my fantasy completed loop event.

    <img src="http://seachawaii.com/wish1.png" border="0" />

  • seac: System conditions, actions and expressions are built-in C2.

    A custom plugin developper could do nothing about it.

    You can already executes events after a loop :

    <img src="http://dl.dropbox.com/u/36472942/construct/forumhelp/AfterLoop.JPG" border="0">

    No need for extra conditions, just remember event sheets gets executed from top to bottom.

    In my screenshot (strictly an example, this code as it is is pretty useless):

    When 0 = 0

    Runs a loop "tt" from 1 to 10

    Then will automaticly execute the actions from event 3 after the loop, as the blank subevent here, is at the same level as the loop.

    Strange thing is that you setted it in your screenshot.

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