• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Differentiating Data with gamemaker studio 2 networking

B

Benjamin Herne

Guest
I have managed to create a connection between my client and server programs. Now I need to send some data - a username and a password. What I do not understand is how to tell the server that "This packet is a username" or "This packet is a password". I have seen it done before, using some form of global variable that is the same across the two projects. Is it a simple as writing two things to the buffer? Like writing the id of the data sent - like 1 for a username and 2 for a password and then reading from a point later in the buffer for the actual data?
 

Simon Gust

Member
You're thinking of the message id.
It does exactly that and should be included in every packet.

It's best if you make an enum for them
Code:
enum msg {username, password} // and more
This code should be the same in both projects (if you have a server project and a client project).

Everytime you make a packet the first thing you write into your buffer is what kind of data should be sent and received.
If you want to send the username you write
Code:
buffer_write(buffer, buffer_u8, msg.username);
buffer_write(buffer, buffer_string, my_username);
On the client side you must read in the same order.
Code:
var msg_id = buffer_read(buffer, buffer_u8);
switch (msg_id)
{
   case msg.username:
       client_username = buffer_read(buffer, buffer_string);
   break;
   case msg.password:
       client_password = buffer_read(buffer, buffer_string);
   break; 
   // etc...
}
 

Hyomoto

Member
Something that should added to this discussion is that you should also think about it kind of the same way you, for example, load a save game, or even structure a program. How does the program know you are entering a login and password? Oh, you are on the login and password screen. Of course. How does my game know I'm ready to load the save file? Ah, I have to tell it. That may seem overly vague but the point is the server should be listening and the conversation goes a bit like:
Code:
SERVER: Ding!  Oh, I have a message.  IP wants to log on.
SERVER: <sends request to IP for username and password>
SERVER: <is waiting for reply from IP>
IP: Here is my username and password.
IP: <sends username>
SERVER: <receives username>
SERVER: <sends response, I got the username, send password>
IP: <receives response>
IP: <sends password>
SERVER: <receives password>
SERVER: Hmm, these look good, I'll let you in.
SERVER: <adds IP to list of accepted clients>
SERVER: <sends response, you are now connected>
IP: <receives response>
Now maybe that sounds a little dumb, but a networking action isn't really so different from two people or more people talking. It's just that the conversation they have is a little strange. If you just pick up the phone and yell a username and password into it, probably not going to get a lot done. Consider banking: you have to call, let them know what you are trying to do, they'll ask you for some details to validate who you are, and then you can proceed. The important things there is that when they ask me for, say, my date of birth if I give them my social security number it's not going to work well. In the case of the login process, the server should be expecting the username from the client because it asked for it. And the client should know to send it because the server told it to. Now, granted, you can roll some of this stuff up, such as perhaps the initial request to log in includes the username, the server can say, okay what's your password. Or maybe you send both, but you still need to tell the server: "Hey, I'm trying to log in." Otherwise even if you tell it: "This data is a password." It's left going, "Yeah, and what do you want me to do with it?"
 
B

Benjamin Herne

Guest
Yeah, this looks like it will work. I am probably going to send a string-written ds_list that contains username and password with a message id of msg.login or something - like:
Code:
//in create event
enum msg {login};    //more to be added later

//when sending
buffer_seek(buffer, buffer_seek_start, 0);
buffer_write(buffer, buffer_u8, msg.login);
buffer_write(buffer, buffer_string, login_list_string);
network_packet_send(/*insert server details here*/)
 
Top