Time to get your clients ready to communicate with the server! The client needs to connect to the server, a step towards establishing a network for your MMORPG.
Initiation To start, create a custom script to handle the initial setup named scr_client_init(). A script is just a separate section of code which you can call from anywhere. This script will be called when the game is first started.
In other examples, the program will be more modulated and professional, but this tutorial is based around teaching you the basics, and will be kept as simple as possible.
Create a script named scr_client_init with the following code:
This might seem rather complex, however when split into the individual components, it really isn't.
First, 2 variables are declared. These are global variables, so they can be accessed anytime from anywhere.
The IP address is that of your local machine "127.0.0.1" which will allow you to test your network on the same PC. The port is just a value that is less likely to be in use by other programs on the client's computer.
Refer to our Quick Glossary to understand these terms more fully.
Once these variables are declared, the function dllinit()is called, which initiates the 39dll. To find out what the arguments parsed into the function do, refer to the initiation function itself. These values will set up the DLL with default settings.
Next, we call the 39dll function tcpconnect() which attempts a connection to the server, assigning the value it returns to a new global variable.
The value returned by tcpconnect() is used later within the networking code, and also in the last part of this script. If that value is less than 0, the connection was a failure and the script returns false. If the value is greater than or equal to 0 then the script was a success because a connection was successfully established.
Connection Handling We now have a script that attempts to connect to the server. It returns either true or false based on whether the connection was successfully established.
We must now handle the connection with some simple code.
Create a room named rm_connect Create an object, named obj_initiate, and place it in this room. It does not need a sprite.
In the Create event of obj_initiate, place this code within an "Execute code" action:
All this does is run the initiation script we created above. On success, we change the object intoobj_controller (which doesn't exist yet), and advance to rm_connected.
On failure, we free 39dll.dll, define a temporary variable and then set the variable to the returned value of show_message_ext(). This function displays a message box with 3 options ("Retry" [blank] "Cancel"). Option 1, "Retry", returns the value 1 if clicked. Option 3, "Cancel", returns the value 3 if clicked.
The conditional statement following simply handles the returned value. If that value is 1, the game is restarted. If the value is anything else, the game is ended.
Create a room named rm_connect, and place obj_initiate anywhere in it.
The client now attempts a connection to the server, however it can't continue unless the server is actually listening for the connection attempt.