0

im trying to do a http request in gamemaker 2, but i dont undersand how it works! actual code:

  var map = ds_map_create();
    ds_map_add(map, "Authorization", "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    
    var url = "https://api.leref.ga/" //example api
    
http_request(url, "GET", map, "");
json = async_load[? "result"];
show_debug_message(json)

I checked the gml manual, I even did it exactly as explained, but I always get a same error:

1 Answers1

2

If I understood your sample correctly, you are accessing async_load right after sending the http request.

The issue is that async_load doesn't exist at that moment and you can't make it yourself. You need to use the HTTP Async Event and read the data from async_load there.

The engine will handle triggering the event, but it will trigger it for every object that has that event defined, so you need to store the return value from http_request and compare it to the "id" value of async_load in the async event.

I also noticed that you have your code in Step Event. This will cause your request to be called several times per second, so I recommend putting the code in the Start, an Alarm or a Key/Mouse Press Event for testing.

Cofeiini
  • 21
  • 2
  • 1
    Some useful over view advice here. It can be helpful if you could edit your Question and show some pseudo-code to show the layout of how you describe the event train should work? thanks – Martin Aug 05 '21 at 21:13
  • 1
    Pseudo-code would do more harm than good. The root of this issue is in the official documentation, which is objectively terrible and has been like that for years. I was tempted to add some sample code, but my day job and commute consumes all free time I have and writing even remotely acceptable code in GML takes more time than I can spare. – Cofeiini Aug 06 '21 at 23:27