Tutorial for Novices on Building Connections
In the world of game development, creating networked games can be an exciting venture. This article aims to provide a beginner's guide to setting up networked games using sockets in GameMaker Studio 2.
Creating and Connecting Sockets
The process begins by creating and opening socket connections. GameMaker Studio 2 offers networking functions such as to create a TCP or UDP socket. One peer acts as a "host" by opening and listening on a socket to accept incoming connections. Other peers use to initiate a connection to the host's IP address and port.
Handling Socket Events
To handle connection success, failures, data receipt, and disconnection notices, use asynchronous networking events (e.g., the event). Receiving data from a socket connection triggers an Asynchronous Network Event, which generates a DS map that can be accessed to detect incoming data and act on it.
Sending and Receiving Data
Once connected, peers communicate game state and inputs by sending data packets through . Received packets are parsed in the networking event, and game state updated accordingly.
Synchronizing Game State
Design messages to synchronize player actions, positions, and events, ensuring both peers maintain consistent game state.
Closing Connections
Use when disconnecting or closing the session.
Key Functions
- – creates a socket, where references the socket and is TCP or UDP.
- – connects to a peer’s listening socket.
- – sends raw data over the socket.
- – closes the socket.
Considerations
- Choosing TCP vs UDP: TCP guarantees packet delivery and order, simplifying development but possibly adding latency. UDP is faster but requires additional logic for reliability.
- NAT and Firewall traversal: P2P networks often require peers to be on accessible IPs or use techniques like hole punching.
- Synchronization and latency handling: Implement prediction, interpolation, or reconciliation to handle network delays and keep gameplay smooth.
- Security: Validate all incoming data to avoid exploits or crashes.
While GameMaker Studio 2 does not provide higher-level P2P abstractions, the core socket API combined with asynchronous networking events allows building a P2P system with careful networking and game logic design.
For implementation examples, you may refer to community tutorials or forums covering GameMaker socket programming. Keep in mind that working with networking on any platform can be frustrating and requires time and patience.
Server Types
There are two types of servers: dedicated and all-in-one. A dedicated server is independent of the players and receives and re-sends data packets. An all-in-one server is hosted by at least one of the players and is most useful for co-op gaming and small scale strategy games. The programming part for both server types is similar, but a dedicated server requires two projects, while an all-in-one server incorporates server functions into a single game project.
For a networked game to function in GameMaker Studio 2, a server must be set up to receive and send data packets. The client connects to the server using a separate function that takes the IP address and port as arguments. The type of information being received is determined using a switch statement.
When checking for a connection, an additional key/value pair in the DS map, "socket", is used to compare the socket ID of the connecting/disconnecting device. The first bytes of the buffer received are used to get the command constant that was sent to identify what "type" of information has been received.
Once the maximum number of clients are connected, any further client trying to connect will not be able to, but there is no error given for this. A failsafe system must be coded to handle this situation.
The first step in receiving data is to check that the data incoming belongs to that of the server socket. For the server, updating all client positions, health, etc... is done when receiving data. For the client, updating the other clients' information for rendering is done when receiving data.
Sending data involves writing it to a buffer and sending the buffer over the network as a packet of data. Each different type of information received is parsed from the buffer and associated with the ID of the incoming socket.
The Asynchronous Network Event is used to detect client/server data transfers. The "type" key in the DS map returns the type of network event being triggered and can be one of three constants. The "id" key in the DS map represents the identifying value of the socket receiving the data, while the "ip" key in the DS map represents the IP address of the connecting socket (as a string).
This article covers the basics of setting up a networked game using sockets, intended for intermediate users familiar with GML. It does not cover setting up a MMO or the guide does not cover setting up a MMO.
- In GameMaker Studio 2, the process of creating networked games involves using sockets for connection, such as creating a TCP or UDP socket with provided networking functions.
- Asynchronous networking events, like the , are used to handle connection success, failures, data receipt, and disconnection notices.
- Communication between peers in the networked game occurs by sending data packets through the function, which is then parsed in the networking event to update the game state.
- When designing messaging for the game, it's essential to ensure synchronization of player actions, positions, and events, maintaining a consistent game state for both peers.