Multiple AJAX calls in For Loop sub events

Get help using Construct 2

Post » Wed Dec 14, 2011 2:41 am

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 http://pennyhaynes.com/testgames/Array/Array.capx

It's probably something simple I am unable to see, but I am stuck.
B
3
G
1
Posts: 13
Reputation: 526

Post » Wed Dec 14, 2011 2:42 am

The For Loop should be nested under the Start of Layout above - it is in the capx.
B
3
G
1
Posts: 13
Reputation: 526

Post » Wed Dec 14, 2011 3:24 am

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:
[code]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[/code]
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)Yann2011-12-14 03:27:16
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Dec 14, 2011 8:56 am

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...)
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Wed Dec 14, 2011 2:07 pm

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.
B
3
G
1
Posts: 13
Reputation: 526

Post » Wed Dec 14, 2011 2:38 pm

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.Kyatric2011-12-14 14:40:08
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Wed Dec 14, 2011 5:23 pm

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.
B
3
G
1
Posts: 13
Reputation: 526

Post » Wed Dec 14, 2011 5:48 pm

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.Kyatric2011-12-14 17:49:14
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Wed Dec 14, 2011 5:49 pm

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 requestsvtrix2011-12-14 18:00:39
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Sat Dec 17, 2011 2:56 am

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. :-)
B
3
G
1
Posts: 13
Reputation: 526

Next

Return to How do I....?

Who is online

Users browsing this forum: austinblackbelt and 37 guests