|
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. |
3 | 4 |
|
4 | 5 | _Note: it is still in its early stages, there is still much to document and improve._ |
5 | 6 |
|
@@ -87,11 +88,56 @@ function GameServer(_port) : TCPServer(_port) constructor { |
87 | 88 | // Server received notification to create ball, then store the ball position |
88 | 89 | ballPosition = _pos; |
89 | 90 | // Send "create_ball" notification to all clients |
90 | | - clientManager.forEach(function(_client_socket, _client) { |
| 91 | + clients.forEach(function(_client_socket, _client) { |
91 | 92 | rpc.sendNotification("create_ball", ballPosition, _client_socket); |
92 | 93 | }); |
93 | 94 | }); |
94 | 95 | } |
95 | 96 | ``` |
96 | 97 | 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 | +``` |
0 commit comments