IP in Godot – Complete Guide

Welcome to this comprehensive tutorial where we’ll dive into the world of Internet Protocol (IP) support within Godot 4, a powerful game engine that continues to empower developers on their game creation journey. Grasping how IP works in Godot is pivotal, not just for building online multiplayer games, but for any project that requires network communication. As you step through this guide, we’ll explore the capabilities of the IP class in Godot 4, demystifying essential network operations like DNS resolution and interface management. Whether you’re taking your first steps into networking or are an adept coder looking to enhance your skills, this tutorial is designed to be both accessible and engaging for all.

What is the IP Class in Godot 4?

The IP class in Godot 4 is a cornerstone for developers looking to harness the power of network communication within their games and applications. This class is a part of Godot’s robust set of network management tools, and it specifically provides DNS hostname resolution support—crucial for converting human-readable domain names into IP addresses that computers can understand. The IP class does not handle TCP/IP communication directly; that’s left to the StreamPeerTCP and TCPServer classes, but it lays the groundwork for such connections.

What is it for?

The IP class is essentially for:

– Managing DNS hostname resolutions to enable your game to communicate with servers by their domain names rather than raw IP addresses.
– Querying and managing local network interface information so that your game can interact with the various network adapters on a user’s system.

Having this functionality is crucial when building games that require networking, such as multiplayer titles, or any application that needs to communicate over the internet.

Why Should I Learn It?

Understanding how IP works in Godot 4 allows you to:

– Seamlessly integrate online features into your games.
– Handle networking without needing to dive into the complexities of low-level socket programming.
– Use Godot’s built-in functionalities to manage network operations effectively, thereby reducing the time and effort you’d otherwise spend on implementing these features from scratch.

Learning about the IP class equips you with the knowledge to build more immersive, connected experiences. They keep your players returning for more, whether they’re battling it out in a multiplayer arena or simply downloading content updates for your latest RPG. Let’s get ready to unlock the potential of networked games with the power of Godot’s IP class!

CTA Small Image

Resolving Hostnames with the IP Class

To begin with one of the most basic operations, let’s look at resolving a domain name to an IP address. This is akin to finding a phone number for a contact in a phone book. In Godot 4, the IP class makes this operation straightforward.

var ip = IP.new()
var address = ip.resolve_hostname("example.com")
print("The IP address is: " + address)

Note that the resolve_hostname function is synchronous and can cause your game to freeze if the DNS server takes time to respond. To prevent this, you can use the asynchronous version as follows:

var ip = IP.new()

Then, you can listen for the signal ‘hostname_resolved’ to get the result:

ip.connect("hostname_resolved", self, "_on_hostname_resolved")

func _on_hostname_resolved(ip_address):
    print("The resolved IP address is: " + ip_address)

Now let’s check how to get all IP addresses associated with a hostname, as there could be more than one:

var ip = IP.new()
var addresses = ip.resolve_hostname_addresses("example.com")
for address in addresses:
    print("An IP address is: " + address)

Querying and Managing Local Network Information

Beyond domain resolution, the IP class also facilitates querying local network interfaces. This is useful for determining all network interfaces available on a user’s system.

var ip = IP.new()
var interfaces = ip.get_local_interfaces()

for i in interfaces:
    print("Interface name: " + i.name)
    print("Interface IP addresses: " + str(i.addresses))

You can use this information, for instance, to bind your server to a specific local interface, which can help in situations where multiple network cards are present, and you want to control which one your game uses for networking.

We can also check whether an IP address is within a given local subnet:

var ip = IP.new()
var local_ip = ""
var mask = ""

if ip.is_in_local_subnet(local_ip):
    print(local_ip + " is in the local subnet based on the mask " + mask)
    print(local_ip + " is not in the local subnet based on the mask " + mask)

The above snippets handle some of the core aspects of IP management in Godot 4. In our next section, we will expand our understanding and delve into more complex examples that build on what we have learned so far.

Next, we explore more nuanced features of the IP class in Godot 4. As you delve into network-aware game development, you’ll often find the need to ensure that the network communication is both efficient and secure. Let’s start with an example of how you can leverage Godot’s capabilities to achieve this.

Sometimes you might want to clear the DNS cache. This is particularly useful if your game needs to update the IP addresses it connects to, due to changes on the server side or while developing and testing:

var ip = IP.new()
ip.clear_cache()  // Clears the DNS cache

Ensuring secure communication is paramount, so let’s look at handling SSL certificates. Godot allows you to set the default SSL certificates for your project, which is needed for secure network connections:

var ip = IP.new()
var cert = load("res://path_to_your/certificate.crt")

For games that need to establish a large number of connections, it can be useful to know the TCP stack’s status. You can check if there are available connections:

var ip = IP.new()
if ip.get_tcp_status() == IP.TCP_STATUS_NO_ERROR:
    print("TCP stack is ready for new connections")
    print("Unable to establish a new TCP connection")

Moving on, it’s often important to ensure that the network events are processed in a timely manner. In Godot, you may need to manually poll the IP class to process network events:

func _process(delta):
    var ip = IP.new()
    ip.poll()  // Make sure to call this frequently, especially when using asynchronous methods

Let’s not forget about the users who are behind a Universal Plug and Play (UPnP) compatible router and want to forward ports for multiplayer games. This can be done quite easily with Godot:

var ip = IP.new()
var port = 7777  // The port you want to forward
ip.upnp_discover()  // Start discovery of UPnP devices
ip.upnp_add_port_mapping(port)  // Request to add a port mapping

Conversely, when your game is done with the port, or if you want to clean up on exit, you can remove the port mapping:

ip.upnp_delete_port_mapping(port)  // Request to delete the port mapping

Each of these samples exemplifies the versatility of the IP class in Godot 4. By mastering these techniques, you can ensure that your game provides a reliable and secure experience for your users, whether they are playing solo with periodic content updates or are engaging in an intensive multiplayer session.

Here at Zenva, we understand the importance of hands-on learning when it comes to mastering game development. As you move forward in your Godot journey, always remember to test these code snippets within your own projects and don’t hesitate to experiment. The true power of a feature like the IP class is only fully realized when you apply it to solve your unique game development challenges.

In continuing with our exploration of the IP class, let’s look at building a simple local IP scanner. This may come in handy when you want your game to search for other instances of the game on your local network, which is especially useful for local multiplayer or peer-to-peer networking:

var ip = IP.new()
for i in range(1, 255):
    var local_ip = "192.168.1." + str(i)
    ip.ping_host(local_ip, 2000)  # Ping with a timeout of 2000 milliseconds

To know which IPs responded to the ping, connect to the signal ‘host_ping_reply’:

ip.connect("host_ping_reply", self, "_on_host_ping_reply")

func _on_host_ping_reply(host, port, ping_time):
    print("Host replied: " + host + " with ping time: " + str(ping_time))

As we continue, let’s tackle error management when performing IP operations. This is an essential aspect to keep in mind especially when designing networked applications that need to be robust and handle different failure cases efficiently. Let’s look at how to handle errors when resolving hostnames:

var ip = IP.new()
var result = ip.resolve_hostname("example.com")

if result is Error:
    print("Failed to resolve hostname")
    print("The IP address is: " + result)

It is equally important to gracefully handle asynchronous operations. Here’s how we might handle an error during an async hostname resolution:

func _on_hostname_resolved(status, ip_address):
    if status == IP.RESOLVER_STATUS_DONE:
        print("The resolved IP address is: " + ip_address)
    elif status == IP.RESOLVER_STATUS_ERROR:
        print("An error occurred while resolving the hostname")

In some cases, knowing the local machine’s public IP can be of great interest, for example when setting up a server that should be reachable from the internet. While Godot does not provide a built-in way to get the public IP, you can still achieve this by querying an external service:

var http_request = HTTPRequest.new()
http_request.connect("request_completed", self, "_on_request_completed")

func _on_request_completed(result, response_code, headers, body):
    var public_ip = body.get_string_from_utf8()
    print("My public IP is: " + public_ip)

Also, if you’re making a game that includes voice chatting or streaming capabilities, you might need to handle UDP sockets. Here’s an example of binding a UDP socket to a port:

var udp = PacketPeerUDP.new()
if udp.listen(port, IP.new().get_local_addresses()[0]) == OK:
    print("UDP socket is listening on port: " + str(port))
    print("Failed to bind UDP socket to port: " + str(port))

Finally, if you want to clean up and close the UDP socket:


These code examples are stepping stones toward creating networked applications that are both powerful and responsive to the needs of the game environment. We at Zenva encourage learners to dissect and repurpose these examples for their specific projects. Dive into your Godot editor and bring these networking concepts to life to enhance your game’s connectivity and engagement. Happy coding!

Continuing Your Godot Journey

Having dipped your toes into the IP class and networking within Godot 4, you might be wondering, “What’s next?” The world of game development is expansive and ever-changing, and there’s always more to learn. If you’re eager to expand your skill set and delve deeper into Godot, we have just the resource for you.

Check out our Godot Game Development Mini-Degree, a curated collection of courses designed to elevate your abilities from beginner to pro. Through these courses, you’ll tackle a wide range of topics, from GDScript and asset management to UI systems and in-depth game mechanics across various genres. It’s a perfect way to build a robust portfolio of real Godot projects and showcase your newfound expertise.

If you’re looking for even more Godot content, we’ve got you covered with a broad selection of Godot courses at Zenva Academy. Whether you’re interested in enhancing specific skills or exploring new areas within game development, our courses are accessible 24/7 to fit your learning style. Start your next project with confidence and the support of Zenva’s high-quality educational content!


Exploring the IP class in Godot 4 reveals just a glimpse of what’s possible in the realm of networked game development. With a solid understanding of these concepts, you’re well on your way to creating engaging multiplayer experiences, smoothly integrated online features, and much more. Remember, practice is key to mastery. Don’t just read about these features—experiment with them in your own Godot projects. Push the limits of what you can build, and watch as your games come alive with the exciting aspect of networking.

Your journey doesn’t end here. Embark on a path to becoming a Godot maestro with our comprehensive Godot Game Development Mini-Degree and further your learning with a variety of expertly crafted courses at Zenva Academy. Each step you take is a move towards refining your skills, expanding your portfolio, and bringing your incredible game ideas to life. We at Zenva are thrilled to be a part of your game development adventure and can’t wait to see what you’ll create next!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.