Quick Start ‐ Move Cube - RoyasDev/EzNet GitHub Wiki

BROKEN IN VERSION 1.0.0 USE 1.0.1+ FROM RELEASES

Welcome to EzNet! Lets get you started using it quickly with a super simple project called Move Cube!

First make a new Godot 4.0+ project. Then choose a 3D scene and save it under whatever name you like.

Once you have made the project go to the AssetLib tab at the top. AssetLib

Then if it asks press the Go Online button. After the AssetLib is online search for EzNet and click the one by Royas. Download

and then download it. ClickDownload

Once it has downloaded go to EzNet/scenes and drag the Base_Network_Manager.tscn into your scene.

image

Now press CTRL+A and make a Control node

image

Underneath the control node make a Label node and name it IP Label. Then make a LineEdit and name it IP LineEdit. Repeat this for the port and arrange them however you want. Once you have your IP and Port LineEdits set the IP's text to 127.0.0.1 and the Port's text to 9999.

IPLineEdit

Now make 2 buttons underneath the control and name 1 Connect and the other Host, setting the buttons text to match their names. image

Make an new script on the Control node and name it connection_menu.gd

image

Now at the top of the connection menu script get the Network_Manager, IP LineEdit, Port LineEdit, Connect Button and the Host button @onready image

still in the connection menu script delete the _process function. Then make a function called _connect() and another called _host()

image

In the _connect() function set the network_manager.networker.ip equal to the ip.text, set the network_manager.networker.port equal to int(port.text) and then call network_manager._connect_client() and finally call the hide() function to hide the connection menu

image

In the _host() function set the network_manager.networker.port equal to int(port.text) and then call the network_manager._create_server() and finally hide the connection menu using the hide() function

image

And now for the last part of this script all you have to do is connect the buttons to the functions we made in _ready.

image

Here's the whole script.

extends Control

@onready var network_manager : NetworkManager = get_node("/root/Node3D/Network_Manager")
@onready var ip : LineEdit = get_node("IP LineEdit")
@onready var port : LineEdit = get_node("Port LineEdit")
@onready var connect : Button = get_node("Connect")
@onready var host : Button = get_node("Host")

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	connect.pressed.connect(_connect)
	host.pressed.connect(_host)

func _connect():
	network_manager.networker.ip = ip.text
	network_manager.networker.port = int(port.text)
	network_manager._connect_client()
	hide()

func _host():
	network_manager.networker.port = int(port.text)
	network_manager._create_server()
	hide()

Now go back to the node 3D at the top of the scene and make a regular node and call it Move Cube

image

Make a new script called move_cube.gd and attach it to the node we just made

image

Underneath the new Move Cube node we just made add a MeshInstance3D node and set the mesh to be a New BoxMesh

Boxmesh

Open up the move_cube.gd script we made and change it from extends Node to extends NetworkObject

image

In the _ready function call super(). This is needed to initialize the network object.

image

Under the line where we extended the NetworkObject now make a variable called sync_position of type Vector3 and set it equal to Vector3.ZERO variables that start with sync_ on NetworkObjects are automatically synced across the network

image

Now get the reference to the MeshInstance3D node that we have childed to our Move Cube node thought @onready

image

In the _process function check if this client isn't the server by calling network_manager.is_server

image

If they aren't the server set the cubes position to be equal to the sync_position then return sync_ variables can only be changed on the server

image

to finish the _process function make a variable called update_position and set it equal to the cube.postion. Then we will move the updated position based on what arrow keys the user is holding down.

image

Now our move_cube.gd script is done so here is all of the code.

extends NetworkObject

var sync_position : Vector3 = Vector3.ZERO

@onready var cube : MeshInstance3D = get_node("MeshInstance3D")

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	super()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if !network_manager.is_server:
		cube.position = sync_position
		return
	
	var updated_position = cube.position
	
	if Input.is_action_pressed("ui_down"):
		updated_position.z += delta
	
	if Input.is_action_pressed("ui_up"):
		updated_position.z -= delta
	
	if Input.is_action_pressed("ui_left"):
		updated_position.x -= delta
	
	if Input.is_action_pressed("ui_right"):
		updated_position.x += delta
	
	sync_position = updated_position
	cube.position = updated_position

Go back to Godot and click on the Move Cube. Then set the Network Manager Name property to be Node3D/Network_Manager

image

Make a Camera3D and set its position to (0.0, 5.0, 5.0) and rotation to (-45.0, 0.0, 0.0)

image

Make a DirectionalLight3D and set its rotation to (-90.0, 0.0, 0.0)

image

Finally go to Debug at the top and click Customize Run Instances...

image

Check Enable Multiple Instances and set the number below it to be 2 or greater

run_instances

Now run the project and select one of the game instances as the host and then connect the other one. Use the arrow keys to move the server arround and the movements will be automatically synced to the clients!

move_cube_gif