Skip to content

Commit 5115818

Browse files
committed
2.0.0
1 parent 23f959f commit 5115818

29 files changed

+343
-302
lines changed

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Patchwire for GameMaker: Studio
22
GameMaker client scripts for the Patchwire multiplayer server framework
33

4-
Version 1.1.0
4+
Version 2.0.0
55

6-
Compatible with [Patchwire 0.2.*](https://github.com/twisterghost/patchwire).
6+
Compatible with [Patchwire 0.5.*](https://github.com/twisterghost/patchwire).
77

88
## Installation
99

10-
Download the latest .zip [release](https://github.com/twisterghost/patchwire/releases) of Patchwire's scripts and add them into your GameMaker project.
10+
Download the latest .yymps [release](https://github.com/twisterghost/patchwire/releases) of Patchwire and import the local package. For detailed instructions, see [this guide](https://gmcore.io/installing.html)
1111

1212
## Usage
1313

@@ -24,15 +24,18 @@ This will initialize the client networking and connect to the given Patchwire se
2424
```GML
2525
// obj_network_manager - Create
2626
27-
net_cmd_add_handler("connected", handle_connected);
27+
net_cmd_add_handler("connected", function(data) {
28+
status = "Connected";
29+
name = data[? "name"];
30+
});
2831
```
2932

3033
```GML
3134
// obj_network_manager - Networking
32-
net_cmd_resolve();
35+
net_resolve();
3336
```
3437

35-
In the create event, we specify a handler script (`handle_connected`) for the `connected` command. In the networking event, we tell the Patchwire client to handle incoming commands. In this case, when the `connected` command is received, its contents will be sent to the `handle_connected` script, which can do whatever you please. `argument0` in the handler script will be the ID of a `ds_map` containing the data sent from the server.
38+
In the create event, we specify a handler function for the `connected` command. In the networking event, we tell the Patchwire client to handle incoming commands with `net_resolve()`. In this case, when the `connected` command is received, its contents will be sent to the provided function, which can do whatever you please. In this case, we're just setting some variables. The first parameter of the handler function will be a ds_map of data from the server. This map will be automatically destroyed after all handlers run.
3639

3740
#### Writing command handler scripts
3841

@@ -49,15 +52,13 @@ Lets use the example of writing a handler for a `chat` command from the server.
4952
Patchwire will route this command into the handler, providing the command JSON as a `ds_map`, so we can handle it like this:
5053

5154
```GML
52-
// Script: handle_chat
53-
var data = argument0;
54-
var fromUser = data[? "user"];
55-
var message = data[? "message"];
55+
net_cmd_add_handler("chat", function(data) {
56+
show_message(data[? "user"] + ': ' + data[? "message"]);
57+
});
5658

57-
show_message(fromUser + ': ' + message);
5859
```
5960
60-
You do not need to handle deleting the data map after your handler script runs. Patchwire cleans it up for you.
61+
Again, you do not need to handle deleting the data map after your handler script runs. Patchwire cleans it up for you.
6162
6263
#### Sending commands to the server
6364
@@ -74,8 +75,6 @@ command[? "someKey"] = "someValue";
7475
net_cmd_send(command);
7576
```
7677

77-
That is all you need to do in order to send a command to the server. You can add as much or as little data to the command as you like.
78-
7978
> **NOTE:** If you don't provide the `command` value in `net_cmd_init`, Patchwire will just send whatever the most recently created command was.
8079
8180
# Docs
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// @description attempt to connect
2+
3+
status = "Connecting.";
4+
connecting = true;
5+
net_connect("127.0.0.1", 3001);
6+
alarm[1] = 20;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// @description
2+
if (connecting) {
3+
status += ".";
4+
alarm[1] = 20;
5+
}
6+

src/patchwire-gm/objects/obj_networking/Create_0.gml

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,48 @@
1-
/// @description
1+
/// @description set up
22
net_init();
33

4-
playerId = "Unregistered";
4+
username = "Unnamed";
5+
registered = false;
56
messageOfTheDay = "No MotD";
7+
status = "Idle";
8+
connecting = false;
69

710
net_cmd_add_handler("register", function(data) {
8-
playerId = data[? "playerId"];
11+
username = data[? "username"];
12+
registered = data[? "registered"];
13+
status = "Registered. Press <space> to disconnect.";
914
});
1015

11-
net_cmd_add_handler("joined", function(data) {
16+
net_cmd_add_handler("welcome", function(data) {
1217
messageOfTheDay = data[? "motd"];
1318

19+
show_message("Message of the day:\n" + messageOfTheDay);
20+
var username = get_string("Connected! Enter a username.", "Player 1");
21+
status = "Registering...";
22+
1423
var cmd = net_cmd_init("register");
24+
cmd[? "username"] = username;
1525
net_cmd_send(cmd);
1626
});
1727

28+
net_cmd_add_handler("disconnected", function() {
29+
status = "Gracefully disconnected.";
30+
});
31+
32+
net_cmd_add_handler("dropped", function() {
33+
status = "Connection abruptly dropped.";
34+
});
35+
36+
net_cmd_add_handler("connectFailed", function() {
37+
status = "Failed to connect.";
38+
connecting = false;
39+
});
40+
41+
net_cmd_add_handler("connected", function() {
42+
connecting = false;
43+
status = "Connected.";
44+
});
45+
46+
47+
alarm[0] = 10;
1848

19-
net_connect("127.0.0.1", 3001);
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
/// @description
2-
draw_text(10, 10, playerId);
3-
draw_text(10, 30, "Message of the day: " + messageOfTheDay);
1+
/// @description draw info
2+
draw_text(10, 10, username);
3+
draw_text(10, 30, "Registration status: " + string(registered));
4+
draw_text(10, 50, "Message of the day: " + messageOfTheDay);
5+
draw_text(10, 70, status);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// @description disconnect
2+
status = "Disconnecting...";
3+
net_disconnect();
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/// @description
2-
net_cmd_resolve();
1+
net_resolve();

src/patchwire-gm/objects/obj_networking/obj_networking.yy

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/patchwire-gm/patchwire-gm.yyp

Lines changed: 4 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/patchwire-gm/scripts/net_cmd_add_handler/net_cmd_add_handler.gml

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)