Work in progress

The content of this page was not yet updated for Godot 4.0 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

Optimization using Servers

Engines like Godot provide increased ease of use thanks to their high level constructs and features. Most of them are accessed and used via the Scene System. Using nodes and resources simplifies project organization and asset management in complex games.

There are, of course, always drawbacks:

  • There is an extra layer of complexity.

  • Performance is lower than when using simple APIs directly.

  • It is not possible to use multiple threads to control them.

  • More memory is needed.

In many cases, this is not really a problem (Godot is very optimized, and most operations are handled with signals, so no polling is required). Still, sometimes it can be. For example, dealing with tens of thousands of instances for something that needs to be processed every frame can be a bottleneck.

This type of situation makes programmers regret they are using a game engine and wish they could go back to a more handcrafted, low level implementation of game code.

Still, Godot is designed to work around this problem.

See also

You can see how using low-level servers works in action using the Bullet Shower demo project

Servers

One of the most interesting design decisions for Godot is the fact that the whole scene system is optional. While it is not currently possible to compile it out, it can be completely bypassed.

At the core, Godot uses the concept of Servers. They are very low-level APIs to control rendering, physics, sound, etc. The scene system is built on top of them and uses them directly. The most common servers are:

Explore their APIs and you will realize that all the functions provided are low-level implementations of everything Godot allows you to do.

RIDs

The key to using servers is understanding Resource ID (RID) objects. These are opaque handles to the server implementation. They are allocated and freed manually. Almost every function in the servers requires RIDs to access the actual resource.

Most Godot nodes and resources contain these RIDs from the servers internally, and they can be obtained with different functions. In fact, anything that inherits Resource can be directly casted to an RID. Not all resources contain an RID, though: in such cases, the RID will be empty. The resource can then be passed to server APIs as an RID.

Warning

Resources are reference-counted (see RefCounted), and references to a resource’s RID are not counted when determining whether the resource is still in use. Make sure to keep a reference to the resource outside the server, or else both it and its RID will be erased.

For nodes, there are many functions available:

Try exploring the nodes and resources you are familiar with and find the functions to obtain the server RIDs.

It is not advised to control RIDs from objects that already have a node associated. Instead, server functions should always be used for creating and controlling new ones and interacting with the existing ones.

Creating a sprite

This is an example of how to create a sprite from code and move it using the low-level CanvasItem API.

GDScript

  1. extends Node2D
  2. # RenderingServer expects references to be kept around.
  3. var texture
  4. func _ready():
  5. # Create a canvas item, child of this node.
  6. var ci_rid = RenderingServer.canvas_item_create()
  7. # Make this node the parent.
  8. RenderingServer.canvas_item_set_parent(ci_rid, get_canvas_item())
  9. # Draw a texture on it.
  10. # Remember, keep this reference.
  11. texture = load("res://my_texture.png")
  12. # Add it, centered.
  13. RenderingServer.canvas_item_add_texture_rect(ci_rid, Rect2(texture.get_size() / 2, texture.get_size()), texture)
  14. # Add the item, rotated 45 degrees and translated.
  15. var xform = Transform2D().rotated(deg_to_rad(45)).translated(Vector2(20, 30))
  16. RenderingServer.canvas_item_set_transform(ci_rid, xform)

The Canvas Item API in the server allows you to add draw primitives to it. Once added, they can’t be modified. The Item needs to be cleared and the primitives re-added (this is not the case for setting the transform, which can be done as many times as desired).

Primitives are cleared this way:

GDScript

  1. RenderingServer.canvas_item_clear(ci_rid)

Instantiating a Mesh into 3D space

The 3D APIs are different from the 2D ones, so the instantiation API must be used.

GDScript

  1. extends Node3D
  2. # RenderingServer expects references to be kept around.
  3. var mesh
  4. func _ready():
  5. # Create a visual instance (for 3D).
  6. var instance = RenderingServer.instance_create()
  7. # Set the scenario from the world, this ensures it
  8. # appears with the same objects as the scene.
  9. var scenario = get_world_3d().scenario
  10. RenderingServer.instance_set_scenario(instance, scenario)
  11. # Add a mesh to it.
  12. # Remember, keep the reference.
  13. mesh = load("res://mymesh.obj")
  14. RenderingServer.instance_set_base(instance, mesh)
  15. # Move the mesh around.
  16. var xform = Transform3D(Basis(), Vector3(20, 100, 0))
  17. RenderingServer.instance_set_transform(instance, xform)

Creating a 2D RigidBody and moving a sprite with it

This creates a RigidBody2D using the PhysicsServer2D API, and moves a CanvasItem when the body moves.

GDScript

  1. # Physics2DServer expects references to be kept around.
  2. var body
  3. var shape
  4. func _body_moved(state, index):
  5. # Created your own canvas item, use it here.
  6. RenderingServer.canvas_item_set_transform(canvas_item, state.transform)
  7. func _ready():
  8. # Create the body.
  9. body = Physics2DServer.body_create()
  10. Physics2DServer.body_set_mode(body, Physics2DServer.BODY_MODE_RIGID)
  11. # Add a shape.
  12. shape = Physics2DServer.rectangle_shape_create()
  13. # Set rectangle extents.
  14. Physics2DServer.shape_set_data(shape, Vector2(10, 10))
  15. # Make sure to keep the shape reference!
  16. Physics2DServer.body_add_shape(body, shape)
  17. # Set space, so it collides in the same space as current scene.
  18. Physics2DServer.body_set_space(body, get_world_2d().space)
  19. # Move initial position.
  20. Physics2DServer.body_set_state(body, Physics2DServer.BODY_STATE_TRANSFORM, Transform2D(0, Vector2(10, 20)))
  21. # Add the transform callback, when body moves
  22. # The last parameter is optional, can be used as index
  23. # if you have many bodies and a single callback.
  24. Physics2DServer.body_set_force_integration_callback(body, self, "_body_moved", 0)

The 3D version should be very similar, as 2D and 3D physics servers are identical (using RigidBody3D and PhysicsServer3D respectively).

Getting data from the servers

Try to never request any information from RenderingServer, PhysicsServer2D or PhysicsServer3D by calling functions unless you know what you are doing. These servers will often run asynchronously for performance and calling any function that returns a value will stall them and force them to process anything pending until the function is actually called. This will severely decrease performance if you call them every frame (and it won’t be obvious why).

Because of this, most APIs in such servers are designed so it’s not even possible to request information back, until it’s actual data that can be saved.