Friday, April 30, 2010



Chapter 3: Client

[Prev: Server] [Next: Telling the Server]


base.gmk object obj_dll create event
server = tcpconnect("43.39.9.132", 12000, 2);
if (server < 0) game_end();

This makes the program send out a connection request to the computer at IP 43.39.9.132, you will need to change this to the server's IP address (if you are on the same network, the network card will contain the IP address, otherwise use "whatsmyip.org" to get it). The next number is the port, notice it is the same as the server's "tcplisten" command in the previous chapter, these must be equal in order to make a connection. The last number means we don't want the connection to freeze our program, so just set it to "2" (some computers may require it to be "1" which is also a nonblocking connection, but only after the primary data send -- this isn't anything to worry about, if "2" doesn't work, use "1" that simple). It will return the socket for the connection or "-1" if it fails. Save this as client.gmk. You can effectively run both the server.gmk and the client.gmk (in that order) and create a connection between the two (use "127.0.0.1" for IP to connect to yourself). Right now there is nothing fancy, it just creates the connection. Let's put some movement into our character.

client.gmk object obj_player step event
if keyboard_check(vk_up) y -= 1;
if keyboard_check(vk_down) y += 1;
if keyboard_check(vk_left) x -= 1;
if keyboard_check(vk_right) x += 1;

This is a basic moving object, you can use any image you want and put 1 in the first room of the file.


[Prev: Server] [Next: Telling the Server]

No comments:

Post a Comment