Skip to content

Commit

Permalink
2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
twisterghost committed Sep 13, 2020
1 parent 23f959f commit 5115818
Show file tree
Hide file tree
Showing 29 changed files with 343 additions and 302 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Patchwire for GameMaker: Studio
GameMaker client scripts for the Patchwire multiplayer server framework

Version 1.1.0
Version 2.0.0

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

## Installation

Download the latest .zip [release](https://github.com/twisterghost/patchwire/releases) of Patchwire's scripts and add them into your GameMaker project.
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)

## Usage

Expand All @@ -24,15 +24,18 @@ This will initialize the client networking and connect to the given Patchwire se
```GML
// obj_network_manager - Create
net_cmd_add_handler("connected", handle_connected);
net_cmd_add_handler("connected", function(data) {
status = "Connected";
name = data[? "name"];
});
```

```GML
// obj_network_manager - Networking
net_cmd_resolve();
net_resolve();
```

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.
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.

#### Writing command handler scripts

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

```GML
// Script: handle_chat
var data = argument0;
var fromUser = data[? "user"];
var message = data[? "message"];
net_cmd_add_handler("chat", function(data) {
show_message(data[? "user"] + ': ' + data[? "message"]);
});

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

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.

> **NOTE:** If you don't provide the `command` value in `net_cmd_init`, Patchwire will just send whatever the most recently created command was.
# Docs
Expand Down
6 changes: 6 additions & 0 deletions src/patchwire-gm/objects/obj_networking/Alarm_0.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// @description attempt to connect

status = "Connecting.";
connecting = true;
net_connect("127.0.0.1", 3001);
alarm[1] = 20;
6 changes: 6 additions & 0 deletions src/patchwire-gm/objects/obj_networking/Alarm_1.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// @description
if (connecting) {
status += ".";
alarm[1] = 20;
}

39 changes: 34 additions & 5 deletions src/patchwire-gm/objects/obj_networking/Create_0.gml
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
/// @description
/// @description set up
net_init();

playerId = "Unregistered";
username = "Unnamed";
registered = false;
messageOfTheDay = "No MotD";
status = "Idle";
connecting = false;

net_cmd_add_handler("register", function(data) {
playerId = data[? "playerId"];
username = data[? "username"];
registered = data[? "registered"];
status = "Registered. Press <space> to disconnect.";
});

net_cmd_add_handler("joined", function(data) {
net_cmd_add_handler("welcome", function(data) {
messageOfTheDay = data[? "motd"];

show_message("Message of the day:\n" + messageOfTheDay);
var username = get_string("Connected! Enter a username.", "Player 1");
status = "Registering...";

var cmd = net_cmd_init("register");
cmd[? "username"] = username;
net_cmd_send(cmd);
});

net_cmd_add_handler("disconnected", function() {
status = "Gracefully disconnected.";
});

net_cmd_add_handler("dropped", function() {
status = "Connection abruptly dropped.";
});

net_cmd_add_handler("connectFailed", function() {
status = "Failed to connect.";
connecting = false;
});

net_cmd_add_handler("connected", function() {
connecting = false;
status = "Connected.";
});


alarm[0] = 10;

net_connect("127.0.0.1", 3001);
8 changes: 5 additions & 3 deletions src/patchwire-gm/objects/obj_networking/Draw_0.gml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// @description
draw_text(10, 10, playerId);
draw_text(10, 30, "Message of the day: " + messageOfTheDay);
/// @description draw info
draw_text(10, 10, username);
draw_text(10, 30, "Registration status: " + string(registered));
draw_text(10, 50, "Message of the day: " + messageOfTheDay);
draw_text(10, 70, status);
3 changes: 3 additions & 0 deletions src/patchwire-gm/objects/obj_networking/KeyPress_32.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// @description disconnect
status = "Disconnecting...";
net_disconnect();
3 changes: 1 addition & 2 deletions src/patchwire-gm/objects/obj_networking/Other_68.gml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/// @description
net_cmd_resolve();
net_resolve();
3 changes: 3 additions & 0 deletions src/patchwire-gm/objects/obj_networking/obj_networking.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions src/patchwire-gm/patchwire-gm.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

12 changes: 0 additions & 12 deletions src/patchwire-gm/scripts/net_cmd_init/net_cmd_init.gml

This file was deleted.

12 changes: 0 additions & 12 deletions src/patchwire-gm/scripts/net_cmd_init/net_cmd_init.yy

This file was deleted.

57 changes: 0 additions & 57 deletions src/patchwire-gm/scripts/net_cmd_parse/net_cmd_parse.gml

This file was deleted.

38 changes: 0 additions & 38 deletions src/patchwire-gm/scripts/net_cmd_resolve/net_cmd_resolve.gml

This file was deleted.

12 changes: 0 additions & 12 deletions src/patchwire-gm/scripts/net_cmd_resolve/net_cmd_resolve.yy

This file was deleted.

31 changes: 0 additions & 31 deletions src/patchwire-gm/scripts/net_cmd_send/net_cmd_send.gml

This file was deleted.

Loading

0 comments on commit 5115818

Please sign in to comment.