Godot:Script
lastmod 2020-09-24created 2020-09-243 edits
godot scripting.
Processing (Process)
Idle processing
Idle processing, called every frame:
It's important to bear in mind that the frequency with which _Process() is called depends on how many frames per second (FPS) your application is running at. This rate can vary over time and across devices.
To help manage this variability, the delta parameter contains the time elapsed in seconds since the previous call to _Process(), as a floating-point number.
Physics processing
The physics step is always called at a fixed time interval. The default is 60 times per second. You can change the interval in Project Settings under Physics -> Common -> Physics Fps:
Overridable functions
public override void _EnterTree()
{
// When the node enters the Scene Tree, it becomes active
// and this function is called. Children nodes have not entered
// the active scene yet. In general, it's better to use _ready()
// for most cases.
base._EnterTree();
}
public override void _Ready()
{
// This function is called after _enter_tree, but it ensures
// that all children nodes have also entered the Scene Tree,
// and became active.
base._Ready();
}
public override void _ExitTree()
{
// When the node exits the Scene Tree, this function is called.
// Children nodes have all exited the Scene Tree at this point
// and all became inactive.
base._ExitTree();
}
public override void _Process(float delta)
{
// This function is called every frame.
base._Process(delta);
}
public override void _PhysicsProcess(float delta)
{
// This is called every physics frame.
base._PhysicsProcess(delta);
}
Groups
A useful feature for organizing large scenes. In the GUI, groups can be added under Groups at the bottom of the Node panel.
To add one in code:
To call a specific function on all members of a group, use SceneTree.CallGroup().
public void _OnDiscovered() // This is a purely illustrative function.
{
GetTree().CallGroup("enemies", "player_was_discovered"); // 그룹 enemies 의 모든 구성원이 player_was_discovered 함수를 호출합니다.
}
You can also get the full list of nodes in a group by calling SceneTree.GetNodesInGroup():
Node
You can create a node like this.
private Sprite _sprite;
public override void _Ready()
{
base._Ready();
_sprite = new Sprite(); // Create a new sprite!
AddChild(_sprite); // Add it as a child of this node.
}
To delete a node, use the Free() function. When it is called, all child nodes are deleted immediately as well.
| WARNING |
| A situation may occur where you want to delete a node that is currently "blocked". A blocked node is emitting a signal or calling a function. This will crash the game. Running Godot with the debugger will often catch this case and warn you about it. |
The safest way to delete a node is to use Node.QueueFree(). This deletes the node safely during idle.
public void _SomeAction()
{
_sprite.QueueFree(); // Removes the node from the scene and frees it when it becomes safe to do so.
}
Scene instances
Instancing a scene from code is done in two steps. The first is to load the scene from your hard drive:
However, the scene is not yet a node. It is packed in a special resource called PackedScene (packed scene). To create the actual node, you must call the PackedScene.instance() function. This returns a tree of nodes from the PackedScene that you can add to the active scene:
The advantage of this two-step process is that a PackedScene stays loaded and ready to use whenever you need it. This lets you create as many instances as you want. This is especially useful for quickly instancing many enemies, bullets, and other entities in the active scene.