Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Monitor personalizzati delle prestazioni

Introduzione

As explained in the Pannello di debug documentation, Godot features a Debugger > Monitors bottom panel that allows tracking various values with graphs showing their evolution over time. The data for those graphs is sourced from the engine's Performance singleton.

Godot lets you declare custom values to be displayed in the Monitors tab. Example use cases for custom performance monitors include:

  • Displaying performance metrics that are specific to your project. For instance, in a voxel game, you could create a performance monitor to track the number of chunks that are loaded every second.

  • Displaying in-game metrics that are not strictly related to performance, but are still useful to graph for debugging purposes. For instance, you could track the number of enemies present in the game to make sure your spawning mechanic works as intended.

Creare un monitor delle prestazioni personalizzato

In this example, we'll create a custom performance monitor to track how many enemies are present in the currently running project.

La scena principale presenta un nodo Timer con il seguente script allegato:

extends Timer


func _ready():
    # The slash delimiter is used to determine the category of the monitor.
    # If there is no slash in the monitor name, a generic "Custom" category
    # will be used instead.
    Performance.add_custom_monitor("game/enemies", get_enemy_count)
    timeout.connect(_on_timeout)
    # Spawn 20 enemies per second.
    wait_time = 0.05
    start()


func _on_timeout():
    var enemy = preload("res://enemy.tscn").instantiate()
    get_parent().add_child(enemy)


# This function is called every time the performance monitor is queried
# (this occurs once per second in the editor, more if called manually).
# The function must return a number greater than or equal to 0 (int or float).
func get_enemy_count():
    return get_tree().get_nodes_in_group("enemies").size()

Il secondo parametro di Performance.add_custom_monitor è un Callable.

enemy.tscn è una scena con un nodo radice Node2D e un nodo figlio Timer. Il Node2D ha il seguente script allegato:

extends Node2D


func _ready():
    add_to_group("enemies")
    $Timer.timeout.connect(_on_timer_timeout)
    # Despawn enemies 2.5 seconds after they spawn.
    $Timer.wait_time = 2.5
    $Timer.start()


func _on_timer_timeout():
    queue_free()

In questo esempio, poiché generiamo 20 nemici al secondo e ogni nemico scompare 2,5 secondi dopo essere stati generati, ci aspettiamo che il numero di nemici presenti nella scena si stabilizzi a 50. Possiamo esserne sicuri osservando il grafico.

Per visualizzare il grafico creato da questo monitor di prestazioni personalizzato, eseguire il progetto, passare all'editor mentre il progetto è in esecuzione e aprire Debugger > Monitor in basso alla finestra dell'editor. Scorri verso il basso fino alla nuova sezione Game e seleziona Enemies. Dovrebbe esserci un grafico come questo:

Esempio di grafico dell'editor da un monitor delle prestazioni personalizzato

Esempio di grafico dell'editor da un monitor delle prestazioni personalizzato

Nota

Il codice di gestione del monitor di prestazioni non deve necessariamente risiedere nello stesso script dei nodi stessi. È possibile scegliere di spostare la registrazione del monitor di prestazioni e la funzione getter in un autoload.

Consultare un monitor delle prestazioni in un progetto

Se si desidera visualizzare il valore del monitor delle prestazioni nella finestra del progetto in esecuzione (anziché nell'editor), usare Performance.get_custom_monitor("category/name") per recuperare il valore del monitor personalizzato. È possibile visualizzare il valore usando un Label, RichTextLabel, Custom drawing in 2D, Testo 3D, ecc.

Questo metodo può servire anche nei progetti esportati (in modalità debug e rilascio), consentendo di creare visualizzazioni al di fuori dell'editor.