How do I Set function return value to Ajax.LastData

0 favourites
  • 6 posts
From the Asset Store
10 Beautiful Pokemon-like Building Pixel Art Sprite Sets
  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Store it in a global?

  • Is that the only way it can be done then?

  • 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.

  • 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.

  • 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.

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