Skip to content

Commit 4a4d2e2

Browse files
Added option to disable RPC
1 parent a42981d commit 4a4d2e2

File tree

14 files changed

+107
-33
lines changed

14 files changed

+107
-33
lines changed

GM Netcode.resource_order

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
{"name":"Socket","order":4,"path":"scripts/Socket/Socket.yy",},
99
{"name":"Client","order":2,"path":"scripts/Client/Client.yy",},
1010
{"name":"obj_client","order":1,"path":"objects/obj_client/obj_client.yy",},
11+
{"name":"obj_client_status","order":3,"path":"objects/obj_client_status/obj_client_status.yy",},
1112
{"name":"RPC","order":1,"path":"scripts/RPC/RPC.yy",},
13+
{"name":"obj_controller","order":1,"path":"objects/obj_controller/obj_controller.yy",},
1214
{"name":"Server","order":3,"path":"scripts/Server/Server.yy",},
1315
{"name":"WebSocket","order":1,"path":"scripts/WebSocket/WebSocket.yy",},
1416
{"name":"Manager","order":2,"path":"scripts/Manager/Manager.yy",},
1517
{"name":"TCPSocket","order":1,"path":"scripts/TCPSocket/TCPSocket.yy",},
1618
{"name":"GameClient","order":1,"path":"scripts/GameClient/GameClient.yy",},
19+
{"name":"obj_server_status","order":2,"path":"objects/obj_server_status/obj_server_status.yy",},
1720
],
1821
}

objects/obj_ball/Create_0.gml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
color = c_white;

objects/obj_ball/Draw_0.gml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
draw_set_color(color);
12
draw_circle(x, y, 8, false);
3+
draw_set_color(c_white);

objects/obj_ball/obj_ball.yy

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

objects/obj_client/Other_68.gml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ else if (_type == network_type_data) {
1818
try {
1919
var _json = buffer_read(_buffer, buffer_text);
2020
var _data = json_parse(_json);
21-
client.rpc.handleMessage(_data, _id);
21+
client.triggerEvent("message", {
22+
data: _data,
23+
socket: _id
24+
});
2225
} catch (_error) {
23-
show_debug_message(_error.message);
26+
client.triggerEvent("error", _error);
2427
}
2528
}
2629

objects/obj_server/Other_68.gml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ else if (_type == network_type_data && server.hasClient(_id)) {
1919
try {
2020
var _json = buffer_read(_buffer, buffer_text);
2121
var _data = json_parse(_json);
22-
server.rpc.handleMessage(_data, _id);
22+
server.triggerEvent("message", {
23+
data: _data,
24+
socket: _id
25+
});
2326
} catch (_error) {
24-
show_debug_message(_error.message);
27+
server.triggerEvent("error", _error);
2528
}
2629
}
2730

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
var _count = global.server.clientManager.getCount();
1+
var _count = global.server.clients.getCount();
22
draw_text(16, 16, "Total Client: " + string(_count));

readme.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
This framework is designed to simplify the setup of a GameMaker server and client.
2-
It uses [JSON-RPC](https://www.jsonrpc.org/specification) to invoke methods from another application.
1+
# GM Netcode
2+
This library is designed to simplify the setup of a GameMaker server and client.
3+
It uses [JSON-RPC](https://www.jsonrpc.org/specification) to invoke methods from another application but you can disable it if needed.
34

45
_Note: it is still in its early stages, there is still much to document and improve._
56

@@ -87,11 +88,56 @@ function GameServer(_port) : TCPServer(_port) constructor {
8788
// Server received notification to create ball, then store the ball position
8889
ballPosition = _pos;
8990
// Send "create_ball" notification to all clients
90-
clientManager.forEach(function(_client_socket, _client) {
91+
clients.forEach(function(_client_socket, _client) {
9192
rpc.sendNotification("create_ball", ballPosition, _client_socket);
9293
});
9394
});
9495
}
9596
```
9697
Start the server and multiple clients,
97-
then click on the screen to check if the balls appear on all clients.
98+
then click on the screen to check if the balls appear on all clients.
99+
## Example without RPC
100+
If you don't want to use RPC, override the "message" event on server/client.
101+
1. Disabling RPC on GameServer:
102+
```gml
103+
function GameServer(_port) : TCPServer(_port) constructor {
104+
// Disable RPC by setting the message event
105+
setEvent("message", function(_message) {
106+
var _data = _message.data;
107+
var _socket = _message.socket;
108+
// If the message type is "create_ball"
109+
if (_data.type == "create_ball") {
110+
// Store the data
111+
data = _data;
112+
// Send this data to all clients
113+
clients.forEach(function(_client_socket) {
114+
network.sendData(data, _client_socket);
115+
});
116+
}
117+
});
118+
}
119+
```
120+
2. Disable RPC on GameClient:
121+
```gml
122+
function GameClient(_ip, _port) : TCPSocket(_ip, _port) constructor {
123+
// Disable RPC by setting the message event
124+
setEvent("message", function(_message) {
125+
var _data = _message.data;
126+
// If the message type is "create_ball"
127+
if (_data.type == "create_ball") {
128+
// Create the ball instance
129+
instance_create_depth(_data.x, _data.y, 0, obj_ball);
130+
}
131+
});
132+
static step = function() {
133+
if (mouse_check_button_pressed(mb_left)) {
134+
// Send message to create ball
135+
network.sendData({
136+
type: "create_ball",
137+
x: mouse_x,
138+
y: mouse_y
139+
});
140+
}
141+
}
142+
}
143+
```

scripts/GameClient/GameClient.gml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function GameClient(_ip, _port) : TCPSocket(_ip, _port) constructor {
22
self.ping = 0;
3-
rpc.registerHandler("create_ball", function(_position) {
4-
instance_create_depth(_position.x, _position.y, 0, obj_ball);
3+
rpc.registerHandler("create_ball", function(_pos) {
4+
var _inst = instance_create_depth(_pos.x, _pos.y, 0, obj_ball);
55
});
66
sendPing = function() {
77
// Wait 1 second to send ping

scripts/GameServer/GameServer.gml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ function GameServer(_port) : TCPServer(_port) constructor {
55
rpc.registerHandler("ping", function(_params, _socket) {
66
return _params;
77
});
8-
rpc.registerHandler("create_ball", function(_params, _socket) {
9-
ballPosition = _params;
10-
clientManager.forEach(function(_client_socket, _client) {
8+
rpc.registerHandler("create_ball", function(_position, _socket) {
9+
ballPosition = _position;
10+
clients.forEach(function(_client_socket) {
1111
rpc.sendNotification("create_ball", ballPosition, _client_socket);
1212
});
1313
});
@@ -21,7 +21,7 @@ function GameServer(_port) : TCPServer(_port) constructor {
2121
});
2222
rpc.registerHandler("get_name_list", function(_params, _socket) {
2323
nameList = [];
24-
clientManager.forEach(function(_client_socket, _client) {
24+
clients.forEach(function(_client_socket, _client) {
2525
array_push(nameList, _client.getName());
2626
});
2727
return string_join_ext(", ", nameList);

0 commit comments

Comments
 (0)