good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to another game maker tutorial in this tutorial I'm going to show you guys how to make an HTTP request in game maker and access an API you may not know what some of some of those words mean so let me explain first um come to this website uin names.com now this is a website where you can just press space and it gives you a random name within the United States right pretty cool the nice thing about this website is that
it has an API and if you click on the options here you can see they've got an API API just means application program interface and it's just a way for an outside application to access functionality of this web we site I guess so we can use this to actually get a random name you can see right here we could [Music] do if you just grab this URL right here api. names.com and you post it into your browser it's going to make a request to that API which is going to return this code right here this
section of code now this section of code right here is a Json object now a Json object is just stands for JavaScript object notation and it's kind of like a DS map in game maker where it's just a set of key value pairs we have a name and then here's the name we have a surname and then here's the surname a gender there's the gender a country here's the country so if you want access one of those you could just do uh whatever object. name and that would give you Sarah so let's open up our
uh game maker program project got two here because I've got one that is a reference project so first thing we're going to do is we're going to create a little uh way that we can access the API so let's call this Sprite button and I'm going to edit this let's make it width of 128 and height of 64 not 16 not 16 we want 64 H 48 there that's pretty good okay and I'm just going to draw this make it black maybe an outline I guess get this color and do this and this color do
this there we go we' got a button we can press I'm going to center it okay let's create an object now we'll just call this object button and we'll grab that Sprite and we'll give it a mouse left not released change a mouse left pressed event so just got a normal button with a mouse left pressed event we're going to grab a section of code here and we're going to write one little simple line of code which is make the HT the okay make the HTTP request and we're just going to do HTTP get and
then we're going to paste that URL that we have had but we're going to surround the URL with quotes there we go we've got quotes around this URL now we've made the request but since the request is going to go out over the Internet it's not going this is just the request so we actually have to wait and get the receive um whatever we receive from it and the way we do that is we add event and go to asynchronous events and there's one for HTTP now this event is going to fire when or happen
I guess I should say when we get the when we get the answer so we've sent out a question basically with the request and this is going to fire when we get the answer so and it's going to give us access to the result of the answer and I'll show you how we can get that so get the answer and we're going to create a temporary object called Json because it returns a Json right so we're just going to create this variable called Json and that's going to be equal to async load question mark result
the async load this is a DS map that is only available inside of the asynchronous events and if we do async load result this is going to get the result of our request which is probably which is going to be ajacent object okay now we want to we can't really do much with a Json object because we're not in JavaScript we're in game maker so we need to convert it convert the Json to a DS map and Game Maker has a function for doing that we'll call this our map equals Json decode and just pass
in the Json that we got so that will convert the Json into a DS map something we can actually work with we need to check to make sure that we didn't get invalid an invalid Json if we did then it will return this function right here will return a negative one so we can do if map equals -1 exit so we'll just exit out of the event it was not the correct uh format in fact what we might want to do is we might want to show a message here to say show message invalid result
that's pretty vague but we just know something broke okay now that we've done that we can we want to make sure that the DS I'm just going to double check make sure that the DS map exists we're going to do if DS map exists no there we go exists map um this checks to make sure that a certain key is inside of our DS map so the one we're going to use is name because it's going to return a name right so let's make sure that the name exists inside of our DS map if it
does show message we'll show a message the random name is and then we'll just add name except we don't have access to that yet so we need to grab it so if the map exists we'll create name equals uh map name so if the map that we got back has a name in it basically then we'll assign that string to this variable right here and we'll show that with our show message let's create a room and put this in our room just to see if things are going to work 640 by 360 room test we'll
throw our object in the room and I'm just going to give it you know some sort of a different background color so that there we go so we can see the button well I'm going save it real quick and Run the game now we should be able to click on it and get a random name let's see what happens the random name is Toby we can click on it again the random name is m I don't know how to say that these are getting names from different countries like that is not a name that I
know how to pronounce um so it's getting just random names from all these different countries well maybe you don't want that maybe you want a random name from a specific country so let me show you how you can do that as well the nice thing is with an API the URL right here that you that you use in the request is kind of like your function and there's a way to pass in parameters into that URL and so the first thing we want we may want to pass into it is uh let's add a um
let's add a question mark here because this this signifies okay we're going to be passing in some parameters the first parameter let's pass in let's pass in um gender equals female so we're we're only going to get female names and we can do an and sign country country equals uh Mexico let's do Mexico now we should be able to run our program again and now that we've passed in we want female names from Mexico we're going to get some names from Mexico so we've got that doesn't look like a name from Mexico to me that
does I maybe I just couldn't see the other one very well but Silvana Sana yeah see these look good viorica V vior maybe not sure where to put the accent on that one so yeah we've got some different names for Mexico uh if you want to do you may want to do United States or you may want to do a country with a space in it I don't know how many countries there are that have spaces in their name but United States is one of them so United and to do a space it's a percent
sign and then 20 states there we go united percent sign 20 States let's run it again and Just for kicks let's switch this let's take off the gender so we get both male and female names from our requests and Bianca man it seems that it's getting some names that are from different countries still to me even though we passed in United States today maybe I maybe I didn't pass in the API correctly I don't know yeah that's but it's still working you if you do something wrong normally it just blows up and says that didn't
work let's try Brazil I wonder if it's if I have to do it with the S or with the Z for their API to work I don't know that is not Brazil we're just getting random names still from every country so let's look at our API so it says we can pass in country country equals Germany did I spell country wrong possibly that could very much be the problem yep yep I did County Country it's really easy to confuse those two there we go now probably this will start working I was wondering why it wasn't
working at all so we've got Brazil now we should get some Brazilian names oh invalid result the random name is Douglas juli vtor Louise Victoria these look right Julio juvenal that's an interesting name I've never heard Lavinia Lavinia Lavinia interesting Sophia Lucas arur um I think these are working I wonder why we got an invalid result on the first time not sure why we got an invalid result on the first time but it looks like the country is working for Brazil United percent signed 20 States let's throw the United States back in there and we'll
see Shirley Billy Christina these are definitely United States names anyways there you go it's kind of fun I wonder if we click it twice really fast if we click it twice really fast we should get two results in a row the second one is invalid wonder why do we click it three times interesting oh well I uh was just messing around with this and thought I'd show you guys what I was messing around with it's not too bad to make a simple little HTTP request this is a get request there's other there's another function you
can uh if you want to dig a Little Deeper um there's there's a get there's a get file a post string and then there's also a HTTP request which lets you pass in the URL the method the header map and the body so uh there's some more that you can look into there but this shows you just the basics and maybe will get you a little interested in that and show you a little bit of how uh some of the asynchronous events work in game maker so thank you for watching this video be sure and
like favorite And subscribe um check out the description I'm almost finished with the hard copies from my Game Maker book a lot of you have been asking about them and wondering when they're going to come out so there's been kind of a uh it's been a lot of work but I redesigned the cover art for it and I think it looks really cool got a lot of positive feedback on that and so I'm super excited because I I'm one of the I guess test copies is uh being printed right now so I should be getting
that I'll probably do a video showing you guys what that looks like I'm excited for that so keep an eye out for that video and I will talk to you guys later