How do I Set function return value to Ajax.LastData

Get help using Construct 2

Post » Tue Apr 26, 2016 12:20 pm

I've been able to use the AJAX object quite successfully so far, but this specific scenario seems impossible.

I have a URL endpoint that can retrieve a number from my server.

Is there a way for me to create a function with an ajax call inside of it for that number, then set the return value of the function to Ajax.LastData?

The problem is that a function and the ajax On completed conditions are both triggers, so I can't exactly nest the on completed inside of the function.

This prevents me from calling a function as an expression and putting the return value into a variable.

Any and all help is most appreciated, thanks.
B
21
S
4
Posts: 4
Reputation: 1,019

Post » Tue Apr 26, 2016 12:31 pm

Store it in a global?
Image ImageImage
B
169
S
50
G
170
Posts: 8,291
Reputation: 108,726

Post » Tue Apr 26, 2016 12:36 pm

Is that the only way it can be done then?
B
21
S
4
Posts: 4
Reputation: 1,019

Post » Tue Apr 26, 2016 1:47 pm

No, you can create a stack in an array, put it in a dictionary, etc.
You can even chain the triggers storing the ajax return in a function parameter.
On ajax tag completed, call function myfunction with peram ajax.lastdata.

Of course that begs the question of why you need the function in the first place, since other events can run in the ajax trigger.
Image ImageImage
B
169
S
50
G
170
Posts: 8,291
Reputation: 108,726

Post » Wed Apr 27, 2016 12:57 pm

I'e thought of those things before, however I wanted to have a more self contained solution.

I want to be able to call a function specifying the data field I want to retrieve, and be able to return that value, enabling me to push that value to any variable I wish.

Right now I'm limited in the sense that I must always push that Ajax.LastData into a fixed variable or dictionary.

So I'm i'm making an online game where a player's score is kept, everytime I call "get_score()" I would have to make sure that at the end of the process the data gotten via ajax gets put into a variable, say "payload".

So that means if I want to set the variable "player_score" I'd have to first call "get_score()" then I have to immediately call "set player_score to payload".

This just makes it very difficult to use in expressions.
B
21
S
4
Posts: 4
Reputation: 1,019

Post » Wed Apr 27, 2016 2:15 pm

Well you could store it in local storage. That way you wouldn't have to redownload an unchanged value in a different session.
Laughs maniacally.
Image ImageImage
B
169
S
50
G
170
Posts: 8,291
Reputation: 108,726


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, jorgmaquoi and 25 guests