Up to date
This page is up to date for Godot 4.0
.
If you still find outdated information, please open an issue.
When to use scenes versus scripts¶
We've already covered how scenes and scripts are different. Scripts define an engine class extension with imperative code, scenes with declarative code.
Each system's capabilities are different as a result. Scenes can define how an extended class initializes, but not what its behavior actually is. Scenes are often used in conjunction with a script, the scene declaring a composition of nodes, and the script adding behaviour with imperative code.
Anonymous types¶
It is possible to completely define a scenes' contents using a script alone. This is, in essence, what the Godot Editor does, only in the C++ constructor of its objects.
But, choosing which one to use can be a dilemma. Creating script instances is identical to creating in-engine classes whereas handling scenes requires a change in API:
const MyNode = preload("my_node.gd")
const MyScene = preload("my_scene.tscn")
var node = Node.new()
var my_node = MyNode.new() # Same method call
var my_scene = MyScene.instantiate() # Different method call
var my_inherited_scene = MyScene.instantiate(PackedScene.GEN_EDIT_STATE_MAIN) # Create scene inheriting from MyScene
using Godot;
public partial class Game : Node
{
public static CSharpScript MyNode { get; } = GD.Load<CSharpScript>("MyNode.cs");
public static PackedScene MyScene { get; } = GD.Load<PackedScene>("MyScene.tscn");
private Node _node;
private <