Multiplayer Networking Tutorial (Part 1) [2020] GameMaker Studio 2 | Multiplayer Online Games

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on everybody in this video I'm gonna teach you how to make multiplayer online games in game maker studio 2 so this is a networking tutorial and hopefully I can teach you and put you on the right track to be making some awesome multiplayer online games or maybe even take some games you've already made convert those to where they work online so that you can play them with your friends this is one of my favorite things to do in game maker and I'm so excited for this video it's been a long time coming so just a disclaimer this is more intermediate to advanced stuff it would be helpful if you did already understand data structures especially DS Maps DS lists as well as have a little bit of knowledge about buffers because you're gonna be working a lot with buffers so it would be useful to understand those and other than that I'll do my best to explain things as I go I can't explain everything there's a lot of resources on the net however I think this is a very good tutorial for people who have never done this but you don't know where to start and I think that's that would be good a good mindset to have in approaching these tutorials so I also just realized there's more than one way to skin a cat so I'm not going to necessarily be showing you the best way the standard way of doing certain things I'm just giving you ideas and principles and you can figure out what works best for your game and the programming situations that you'll encounter so you can take what I teach you tweak it to make it work for you so let's go ahead and get into it basically we're gonna start with two project files we're gonna start with a project called our client where this is where our game world's gonna be our graphics or sprites everything the player is going to see is going to be in our client we're also gonna have another project filed called our server so this is what we see this is the server that we're gonna be running somewhere that the clients connect to you can make the client and server in one project depends on what kind of game you want to make if you want the players that have access to the server you're probably gonna want to build it all in one package for me most of my games I don't want the players to have access to the server or know what's going on there so I normally do two different project files so you could just do it all in one it's up to you as you're following the totes the tutorial just stick with me and do two in the next project you can do what you want okay so this is basically what we're gonna make I made this beautiful paint graphic here this box I did that myself by the way this is basically what we're gonna make we're gonna make a server as represented in the red box and we're gonna make clients which I can paste one right there basically this is how networking works so we have a client and they you know the player opens the game and then that client connects to the server so basically we can have let's just add in four clients here so this is basically what's going on so let's say that these clients have can establish the connection with this server and they do that by connecting to an IP address and a port more on that later so basically this this is how it works let's say client number one moves their character to the left so this guy just moved to the left basically what that what happens in networking is the client will send information to the server to say hey I just moved left that server will then send information to all the clients to say client number one move let moved left so basically on all of these clients screens they'll see that the guy moved left and that's basically how it works let's say client number two jumped well he just jumped he's gonna tell the server hey I just clicked the jump key the server's gonna tell all the clients hey client number two click the jump key so make him jump on everybody's screen that's about all there is to it that's how chat works someone types a chat message they tell the server what they said the server then sends what they said to all the clients and the clients display that information that's all there is really to it okay well I should save this and sell it on eBay in a frame but I'm actually gonna close it okay so sorry about that okay so let's go ahead and do a little bit of work first we're going to work on our server so I have an object called CEO and under / server I'm going to open this room and I'm gonna go ahead and throw the object into this room just so it's there I'm gonna close I might as well rename the room while I'm at it - room server very cool let me close that get it out of the way so the first thing we're gonna do is write some code to make it to where this server will be on line so we're gonna make a create event and delete that and this is basically all that we are going to do we're going to say let's see I have some notes over here cuz I really don't want to I really don't want to make any mistakes and have to fix it later in a video so here's what we're gonna first we're gonna set a port so we're gonna say port equals and then you can write in a port so if you don't know about port forwarding you're gonna want to go research that after this video because if you're using an internet router and a lot of Internet routers block all ports and you have to go into your router settings and unblock that port to be able to host the server otherwise the server will be online they just can't connect to that port so you can put in a port we can just throw in a number there 64,000 198 sounds like a good port number next we can make a variable called max clients so this is how many clients are allowed to connect to the server so we can put 12 or you know you can stress test later and see how many work on your server before it explodes next we are going to just write a function called network create Netflix that's great I just wrote Netflix Network create server and then we're going to write the server type now I'm just gonna go ahead and put in network socket TCP next we're gonna put in our port which is our port variable and then max clients or it max client actually yeah let's make that plural what's with this variable name it max clients okay so basically that's it to make this server go live and be on online one thing I'm going to point out is in this network server create this guy right here network socket type you're gonna want to know what's going on here so basically there are three types of servers or yeah servers there's three types of servers you can make in game maker studio two there is a TCP server there is a was it called you brainfart you DP server and then recently added there is WebSockets WS server so there are these three types and it would do you good for the game that you're making to research the the difference especially between TCP and UDP because each one has a pro and a con I'll just save you a lot of work and just say go with TCP probably for most of your projects and basically this has to do with some servers are faster but some packets can get lost in translation while other servers are slower but it's more reliable in getting data so I believe TCP is the one that's more reliable while UDP is the one that's a little more faster but a little less reliable I think but you can research that and and really you need to for the type of game that you're making you need to research and figure out which one's best for you I can't tell you that it depends on how you make your game we're going to go TCP and then my favorite just-released WebSockets now you if this game is gonna be html5 and it's gonna be a browser-based game it you need to use this on the client and the server you need to you need to change this guy to this okay and that'll make it a web server and you I mean use WebSockets so for this tutorial we're going with TCP next we're going to make a buffer which I'll just name it server buffer equals buffer creates you're gonna want a size of your buffer which we're gonna make it about a megabyte buffer type is gonna be buffer fixed and then alignment you can just put the number one so basically oh I closed that I should have closed that um the paint thing but basically the data that the server and the clients are sending back and forth are called packets and basically we create something called a buffer which basically takes a bunch of data puts it in this thing called a buffer you it could be like called the car you know it's like a it's like a little I don't know I guess it's called you can call it the passenger of a car and then it takes that buffer it goes into a packet with that which that's the car that's the vehicle and sends that packet somewhere so the buffer is going to contain a lot of things and we'll get more into that as we go that's it that's literally it once you have this once we run this we have a freaking server going and that's it now the first time you run this you might have a pop up with windows saying should we allow this network to happen and you just click yes so that's going on next we're going to go to our a little Ola I guess we can add an event we're going to add an async event so async and we will go to networking and we're going to write a little bit of code type under slash event equals V s now find value and so basically async if you've never used it it's going to create a a DS map where you're going to get data from and the DS map is going to be called a sink oops a sink load that's what's gonna be called and there's a whole bunch of keys and stuff I can't get into explaining all this will be here 20 minutes if you'd like me to make another video explaining it I can but basically just see to know it creates a DES map and you can get data from that so and the key is called type and then we're gonna do a switch and we're gonna say type events and then we're going to now basics which event you know pretty easy then we're gonna say case Network type connect so yeah break yeah okay so basically when a client connects you know so in our networking event so a networking event happened a client just sent information to the server so when that happens when the client sends info to the server this networking thing is going to go off and this DF map is gonna have some data in it and then it's the switch right here that takes that data and sees what exactly is it's going on so when we connect we can do some stuff so I think I'm going to go back to our create event we're going to add a DS list called what do I have a cold there yeah that's a good name for it socket list and basically a socket is the unique ID of a client that has connected so that way you can send data to that unique ID of the client so we're gonna store all of that in a list called socket list so when a client connects we're basically going to say DS list ad and then we're gonna say socket list and then the value of that is actually let's do that socket equals vs map client value async load socket so we're pulling from that DS map again we're getting the socket of the client that just connected then DS list ad socket list socket boom so now the server has a record of hey we have a client and we've added it to the list so now we can send data to it okay and then of course we want to add case Network type disconnect that whenever this client disconnects we want to remove that socket from the list so let's go ahead and do that socket equals vs map find value async load get the socket again and then yeah vs list delete socket list D S map I'm sorry D s list find index socket list socket so it finds this socket position using DS lists find index and that deletes it from the socket list that way when client number one moves left it's not sending information to a client that doesn't exist anymore so pretty easy and then we want to add one more friendly little thing here and and these are going to be even more useful later because when a connects maybe you want to say okay go into the game world and so you'll send data out of this the switch maybe a client disconnects well maybe you want to send information to all the other clients that that client disconnected delete his character like destroy him okay then we'll say net case Network type data and we will say receive packets so we're gonna make a script called receive packets so this is anytime we receive a packet or in other words data from the clients it's going to run the script which is basically going to be a huge switch that's like has a bunch of stuff like did they walk did they jump did they pick up an item did they whatever did they kill an enemy and it's gonna go through this massive list of every possibility find what the player did and then send that information off to everybody else so we're gonna need two pieces of information here we're gonna need buffer equals buffer I'm sorry DS map find value go back to that ain't a sink load very useful stuff grab the buffer and then we're gonna do that one more time but we're gonna say socket equals ID so for this one you use ID instead of socket like we did there so this is some buffer information and this is the socket of the client that unique ID of the client that just sent information so while we're at it let's go ahead and create a script called receive packets or is it packet receive packet write one more thing we want to do is before this receive packet we want to go to the very start of our buffer so buffer seek and yeah cool cool so that's what you're gonna have to do and basically what this does what this guy does right here what are you giving me an error for is going to the very start of your buffer so there's a lot of information in the buffer and you want to always go back on top of that buffer to get the very first piece of information that's in that buffer so just so you know again it'd be really helpful if you don't know about buffers to go ahead and and start looking into those and learn a little bit about it so let's open up our receive packet script and which is right here oh gosh I have two of them open okay receive packet let's grab our two arguments which is buffer and our socket and then we want to say message ID equals buffer read buffer you eight and then the type up what am i doing buffer and then buffer you wait okay so and then we can do a switch message ID and all of our possible things that the server and the client can be doing gonna be right there in this big ol switch event okay so that about does it for the server for now make sure again you have this message ID this is basically should always be the first thing that the buffer sends is what you want the the client to say to the server so like oh the first thing is I moved left or moved right so hopefully this will make sense sometimes sometime in the future okay let's go over to our client and let's go ahead and go to our room this is easy we can do this part throw the client into the room and rename the room room client let's open up Kahn client see un-under slash client and let's add an event a create event and basically now we're going to connect to the server we're going to have this client connect to the server and to do that we're going to say client equals because it's gonna give us a little piece of data in our client so we can go ahead and make this into a very variable client equals network create socket and then network socket TCP remember we talked about that so if you did UDP both client and server need to be saying UDP just so you know next we want to say network connect client and then our URL which is actually an IP address so put in 127.0.0.1 and then we want to put in the port so and make sure these are in parenthesis so this is your IP address you're gonna probably want to test on a local IP address and if you want to get your local IP address open up your command prompt Windows Keys CMD click enter and type ipconfig and like I probably shouldn't show you this my bad anyway you didn't just see that so anyway well I might as well show you cuz you saw it so yeah basically this guy right here your default gateway 192.168.0.1 would be my my local IP address but other peer friends won't be able to connect to that only people on your network would be able to connect to that just so you know okay so 127.0.0.1 also normally works it should work too okay so for the port we want the same port as our server so remember our server port was six four one nine eight so six four one nine eight Network connect yay that's all you have to do and if we ran the game we would connect to the server and it would be that easy if the server was running and online so next we want to create a buffer for clients 1024 so that's about a megabyte buffer so we couldn't send about a megabyte of data basically okay created the buffer for the client next we want to add an async networking kind of the same we did in the other thing type of N equals DS map fine value async load type a switch type event and then case Network type data you probably don't need a connect or disconnect for this for the clients this is again we did this in the server remember the network type data network type data so this is data like you jumped you picked up an item you were running around you're doing whatever that's what that is so it's basically the same stuff so buffer equals D s map fine value async load buffer and then we want to go to the start of that buffer by going buffer seek buffer buffer seek start 0 again I don't have it in me to explain every detail like I normally do my tutorials so like I said it would just really benefit you to take time to study buffers and and how they work so that you can just kind of know what's going on here if you have questions you can definitely leave a comment so we have our buffer that ok so this is data the server is gonna be sending the server is gonna send a buffer remember the buffers the passenger of the car in the car is the packet so a packets coming with a passenger and that passengers name is buffer and it's coming to this to the client like a speeding bullet and like a Tesla car and then the the clients gonna grab that buffer go to the very start of the buffer and then we're gonna run received packet buffer so just like in our server we created receive packet we're going to receive a packet in our client so we're gonna make a script called received packet and it's going to be we're going to yeah buffer equals argument zero that's the piece of data that we're sending is the buffer information and then message ID equals buffer read buffer the type is a buffer you eight yeah so again whenever we're sending information the first thing we're sending this buffer read is a little piece of data it's a number but it's going to be a number that we're going to assign and it could be one it could be ten ten could be equal to jump five could be equal to you know walk left or whatever so you'll see and then we're gonna do a switch message ID look at this okay now we can do something freaking fun okay that everything we just did is the hardest part of networking everything after this gets fun and gets easy so we've set it up we've set up a framework for our server and our client to start communicating so let's run it just to see if we're gonna get any errors so I'm running my server and I'm running my client so Oh malformed switch statement this guy right here what uh okay well I'm gonna go ahead and comment out the switch statement then apparently you have to have data in it so let's try that again let's run the server let's run the client you can click f5 to run your projects by the way okay no errors we should be good so you can't see it it's just black screens but basically this server is online in this client totally connected to the server you know how we can find out that that's true by in our let's go to our where we are we can say we can say this connected equals network connect now this this function will return if we've connected or not so then we can say show message connected so if we get a number greater than 0 or 0 or greater then that means we're connected 0 we're connected it works oh that's makes me so happy so now see if we close the server and run the client we should get a negative number hopefully maybe run why aren't you running like froze ok run why isn't it running ok so I stopped the video for a second because I didn't know what was going on but basically when we run the client without the server running we will eventually get a negative number but it literally takes like 5 minutes because it's trying to connect to a server that's not running and then we get a pop up but it takes forever and I'm not gonna put you through that torture just trust me when a server is not running you get a negative number when it is running you get a number of 0 or higher ok so we have established the server in a connection like I said that this is was the hardest part once you get got this down you don't have to mess with a lot of this code again ok really where you're gonna be working out of at this point is out of this script file right here called receive packet I come oh yeah I had to comment that out because we couldn't run this stuff so let me uncomment this one over here in the server so yeah everything from here on out mainly you're gonna be working out of receive packet a little other side things but this is it for the most part so let's go ahead and set up our first thing so we're going to create not a room delete that we're gonna create an object called object player in our client and we're yeah I guess we can give them a little sprite I guess I'm just gonna give um a green square sprite player and yeah I called him object client we're gonna name them object player so we have our object player and let's go ahead and let's go ahead and mmm yeah let's throw him in the client for now later we'll make it to where he won't be in the client until the client connects and then the server will tell the client to create the player but for now go ahead and put him in the room he's not gonna hurt anything and let's go ahead and make a hello world thing we'll go ahead and say key pressed space and now let's do something really fun okay so whenever we click space we're basically going to send we're gonna send data to the server and we're gonna send data from the server back to the client so this is just and then we'll end the video and I'll do a part two later and we'll expound on all of this but what I'm gonna show you is the very basic of everything you're going to be doing so when we click space we're gonna say buffer seek remember this but we're gonna say Conn client dot client buffer so we're just referring to that buffer that we created in the in the client we're gonna say buffer seek start zero again go into the very start of the buffer like we always want to do then we're gonna say buffer right kind of like ini files you write and you read write again Conn client client buffer so our buffer from the client and then we're gonna say buffer under slash string and we're gonna give it the value of hello world and then we're going to say network send packet and we're going to say we wanna what we named our packet client okay we're going to say yeah we're going to say Conn client dot client okay and then our buffer ID is client buffer and then our size is yeah buffer under slash tell Conn client dot client buffer okay that's it that's how you send information to the server with a code kind of like this and you can write as much as you want before you send the packet so we could say we could have three of these and or four of these and it says hello world it says Missouri it says and we can put as much data as we want up to about a megabyte before and then send all of that into packet and then the server grabs this okay we're just gonna do one for now and so when we click space it's sending data to the surface so that data is gonna go to the server and it's going to go right into this script and I'm sorry one thing we want to add before hello world is our is our handy-dandy message ID okay so what we're going to do is let's go ahead and do this again buffer right Conn client client buffer and then buffer you eight and then the value let's give it the value of number one so we're sending two little pieces of data that first piece of data is the number one which is going to be and that's the message ID in the switch statement so switch when there's number one it's going to read this data right here okay so it's sending this it's taking this buffer putting it all in a buffer and sending that buffer through a packet into the server and this is where where we're gonna land so we sent two pieces of data that first piece of data is being read right there that message ID is that first beat it's a data that's being that's being read which is the number one okay the second piece of data from number two and all the other additional pieces of data will be read in our switch statement so case one which is our hello world we can save our message equals buffer read buffer buffer string so make sure that these things always sync up your type of buffer so we send a string buffer string so we're going to read a buffer string we sent a buffer you eight which is basically a number what zero to 200 and something it's so we sent a you eight and the first thing we read is a you eight now you can look at the different types of buffers that you can send if we open it up in the help file there's all these different types of bubble-bubbles buffers a boolean true or false a string so familiarize yourself with these because basically they're all different sizes so when you're sending packets you want everything as small as possible so a buffer you a is a number 0 to 255 a buffer you 16 is 0-2 to 65,000 so you might want to use your x and y positions for your players using a buffer you 16 32 is all the way up to 4 million okay so good to know but they have to sync if they don't think it's not gonna work okay so the server has got this information so if we say show message after this the server will display a pop-up that says hello world so let's go ahead and the server let's run the client malformed yeah okay I got to comment this out in the client ok f5 run in the client okay so here's our player here's our server here here's our client so when click space this guy's gonna show a pop-up oops what I didn't want to do that I did not want to do that okay let's let's click space hello world we did it we sent data from the client to the server as we showed a pop-up aren't you so happy that's it we just sent data but now like these could be on different computers they could be in a different country right okay so we've sent data from the client to the server the next thing we want to do is send it from the server to the to the to the client so let's go ahead and do that real quick like yeah let me just yep okay okay okay so this is our server receive packet and basically we can delete this message let's make the let's make the client receive the message okay so we can just say again buffer seek this time we're gonna want to say server buffer and then we're gonna say buffers see start and then we're going to do 0 and then yeah we're gonna just say again buffer right so it's the same but it's in Reverse instead of sending it to the server we're sending it to the client so a server buffer the type is a buffer string and the value is what we received from the client which is message and then we just want to say network actually what do we want to do we want to make sure that that's right but we also want to send the message ID I always forget to do that buffer right server buffer buffer you eight and then the number one which is our case so the client will read that okay so we have our two pieces of data and then network send packet we want to send it to the socket which is the client that sent it to us is right there the socket and then we want to say the yeah server buffer and then buffer the size is buffer towel server buffer BAM so now basically what's happening now the client is sending this message and then the server is just sending that message right back to the client who sent it so in our client in our receive packet we're just gonna say case one remember that's the first piece of data we send is the number one and it's grabbing it right there and buffer read you eight so hello world it's basically gonna save our message equals buffer read yeah buffer and then buffer string and then show message a message and now it will let's indent this properly and now it will show the message on the client instead of the server so we can go ahead and run these now so so here's our server here's our client when we click space so now this pop-up is from the client it's not from the server okay so you can play around with that I'm gonna leave you there the next video I'll show you how to send from the server to all the clients so maybe hello world will go on to all the clients right now it just goes to the back to the client it sent it but I'll leave you with that one thing I'll do let's do this let's I'll just show you the potential here let's add another frame into our sprite player let's give them an orange frame let's go to our object player and let's create him put a create event image feed equals zero we're going to change the image index of our player okay so now when we click space instead of sending hello world we're gonna delete this next piece of data all we're gonna send is the message ID okay that's it we're sending the message ID one to the to the server that's all we're gonna do so let's we remove the hello world message the string we're gonna go to the server we're gonna delete this buffer read because we don't need it because we're not getting any data other than this piece of data right here let's delete this buffer string that we were sending because we don't need that because there's no message to send delete this comment then let's go back to client let's go to receive packet let's delete this because we're not receiving any data other than this message ID but what we're gonna do is say object player dot image index equals one that's it it's just gonna make us Orange isn't that great so now when we play the game and we click space okay so here's the server here's the client when we click space boom it changes us to Orange that's the potential that's the kind of stuff that you can do it's in you know again if this server wasn't running you wouldn't be able to do that it's all just sending data back and forth so I hope you like this video it's the longest video I've ever made I'm gonna make part two where we'll get into movement and other types of things and of course sending data to all the clients please leave a like if you watch this video and found it helpful I really want to keep making these I want to know you guys are watching and that it's being helpful to you please leave a comment if you like this but that is it for this video like I said it gets pretty easy from here on out challenging easy but it's a lot easier than everything else we just did this was the hard part it gets very easy from here on out so follow me on Twitter if you're not already you can support me on patreon that's a big help as well for my projects and I'd like to do this more so I would I guess I'll see you guys next video thank you so much for watching
Info
Channel: Wizirdi
Views: 38,442
Rating: undefined out of 5
Keywords: gamemaker, gmstudio2, gmstudio, gamemaker studio, networking, sockets, multiplayer, packets, multiplayer games, mmo, gamemaker mmo, mmorpg tutorial, make an mmorpg, gmstudio online, online game tutorial
Id: NbsXRuNijlo
Channel Id: undefined
Length: 41min 23sec (2483 seconds)
Published: Tue Oct 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.