To create a multiplayer game using Godot 3.5.1 with your PC as the server and players able to join using their global IP addresses, you can follow these steps. We'll create a simple scene where players can move a character sprite over a network.
-
Install Godot and create a new project.
-
Create a new scene (Ctrl + N).
-
Add a Node as the root node and rename it to "Main".
-
Save the scene (Ctrl + S).
-
Create another scene for the player.
-
Add an Area2D node as the root node of the player’s scene and rename it to "Player".
-
Add a Sprite node as a child of the "Player" node, and set the texture to a character image.
-
Add a CollisionShape2D node as a child of the "Player" node, and set its shape to a rectangle that matches the character image's dimensions.
-
Save the player scene (Ctrl + S).
-
Create a new script for the player scene (right-click on the "Player" node -> Attach Script).
-
In the player's script, add the following code to handle basic movement:
extends Area2D
export (int) var player_speed = 200
var velocity = Vector2()
func _physics_process(delta):
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * player_speed
position += velocity * delta
-
Save the player script.
-
Go back to the main scene and add a NetworkedMultiplayerENet node as a child of "Main".
-
Create a new script for the main scene (right-click on the "Main" node -> Attach Script).
-
In the script for the main scene, add the following code to set up the server and client networking, instantiate players, and handle connections:
extends Node
const PlayerScene = preload("res://Player.tscn")
var server = null
var players = {}
var my_player = null
func _ready():
var pktpeer = NetworkedMultiplayerENet.new()
get_tree().set_network_peer(pktpeer)
server = ask_user_if_server()
if server:
pktpeer.create_server(12345, 16)
else:
var ip = get_global_ip()
pktpeer.create_client(ip, 12345)
get_tree().connect("network_peer_connected", self, "_on_peer_connected")
get_tree().connect("network_peer_disconnected", self, "_on_peer_disconnected")
func ask_user_if_server():
return true # For now, we set it as the server by default. You can implement your own UI logic here.
func get_global_ip():
# You should implement a method to let the user input the IP address of the server.
return "127.0.0.1"
func _on_peer_connected(id):
if server:
rpc_unreliable("spawn_player", id, get_tree().get_network_unique_id() != id)
elif id == get_tree().get_network_unique_id():
spawn_player(id, true)
func _on_peer_disconnected(id):
if id in players:
players[id].queue_free()
players.erase(id)
remotesync func spawn_player(id, is_remote):
var player = PlayerScene.instance()
player.name = "player_" + str(id)
players[id] = player
player.set("is_remote", is_remote)
add_child(player)
if not is_remote:
my_player = player
- In the player's script, add the following code to synchronize the position between server and clients:
rpcmode var position setget _set_position
func _set_position(_position):
position = _position
rpc_unreliable("_set_position", _position)
- Save the scripts and run the main scene.
You'll now have a simple multiplayer game where clients can connect to your server using the global IP address. The server should be running on your PC, and the clients can be other instances of the game on other devices. You can play around with the ask_user_if_server and get_global_ip functions to create a UI for the user to enter their IP address and choose if they want to be a server or client. Note that this is a very basic example and can be improved for a production game.
Keep in mind that you might need to do some port forwarding in your router to allow incoming connections from the internet.