Skip to content

Commit f3efea5

Browse files
authored
Merge pull request #9404 from raulsntos/using-navigation-servers
Add C# code to "Using NavigationServer"
2 parents b5b5d74 + 3a23253 commit f3efea5

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

tutorials/navigation/navigation_using_navigationservers.rst

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,38 +95,38 @@ Afterwards the function waits for the next physics frame before continuing with
9595
extends Node3D
9696

9797
func _ready():
98-
# use call deferred to make sure the entire scene tree nodes are setup
99-
# else await / yield on 'physics_frame' in a _ready() might get stuck
98+
# Use call deferred to make sure the entire scene tree nodes are setup
99+
# else await on 'physics_frame' in a _ready() might get stuck.
100100
call_deferred("custom_setup")
101101

102102
func custom_setup():
103103

104-
# create a new navigation map
104+
# Create a new navigation map.
105105
var map: RID = NavigationServer3D.map_create()
106106
NavigationServer3D.map_set_up(map, Vector3.UP)
107107
NavigationServer3D.map_set_active(map, true)
108108

109-
# create a new navigation region and add it to the map
109+
# Create a new navigation region and add it to the map.
110110
var region: RID = NavigationServer3D.region_create()
111111
NavigationServer3D.region_set_transform(region, Transform3D())
112112
NavigationServer3D.region_set_map(region, map)
113113

114-
# create a procedural navigation mesh for the region
114+
# Create a procedural navigation mesh for the region.
115115
var new_navigation_mesh: NavigationMesh = NavigationMesh.new()
116116
var vertices: PackedVector3Array = PackedVector3Array([
117-
Vector3(0,0,0),
118-
Vector3(9.0,0,0),
119-
Vector3(0,0,9.0)
117+
Vector3(0, 0, 0),
118+
Vector3(9.0, 0, 0),
119+
Vector3(0, 0, 9.0)
120120
])
121121
new_navigation_mesh.set_vertices(vertices)
122122
var polygon: PackedInt32Array = PackedInt32Array([0, 1, 2])
123123
new_navigation_mesh.add_polygon(polygon)
124124
NavigationServer3D.region_set_navigation_mesh(region, new_navigation_mesh)
125125

126-
# wait for NavigationServer sync to adapt to made changes
126+
# Wait for NavigationServer sync to adapt to made changes.
127127
await get_tree().physics_frame
128128

129-
# query the path from the navigationserver
129+
# Query the path from the navigation server.
130130
var start_position: Vector3 = Vector3(0.1, 0.0, 0.1)
131131
var target_position: Vector3 = Vector3(1.0, 0.0, 1.0)
132132
var optimize_path: bool = true
@@ -141,6 +141,59 @@ Afterwards the function waits for the next physics frame before continuing with
141141
print("Found a path!")
142142
print(path)
143143

144+
.. code-tab:: csharp C#
145+
146+
using Godot;
147+
148+
public partial class MyNode3D : Node3D
149+
{
150+
public override void _Ready()
151+
{
152+
// Use call deferred to make sure the entire scene tree nodes are setup
153+
// else await on 'physics_frame' in a _Ready() might get stuck.
154+
CallDeferred(MethodName.CustomSetup);
155+
}
156+
157+
private async void CustomSetup()
158+
{
159+
// Create a new navigation map.
160+
Rid map = NavigationServer3D.MapCreate();
161+
NavigationServer3D.MapSetUp(map, Vector3.Up);
162+
NavigationServer3D.MapSetActive(map, true);
163+
164+
// Create a new navigation region and add it to the map.
165+
Rid region = NavigationServer3D.RegionCreate();
166+
NavigationServer3D.RegionSetTransform(region, Transform3D.Identity);
167+
NavigationServer3D.RegionSetMap(region, map);
168+
169+
// Create a procedural navigation mesh for the region.
170+
var newNavigationMesh = new NavigationMesh()
171+
{
172+
Vertices = new[]
173+
{
174+
new Vector3(0.0f, 0.0f, 0.0f),
175+
new Vector3(9.0f, 0.0f, 0.0f),
176+
new Vector3(0.0f, 0.0f, 9.0f),
177+
},
178+
};
179+
int[] polygon = new[] { 0, 1, 2 };
180+
newNavigationMesh.AddPolygon(polygon);
181+
NavigationServer3D.RegionSetNavigationMesh(region, newNavigationMesh);
182+
183+
// Wait for NavigationServer sync to adapt to made changes.
184+
await ToSignal(GetTree(), SceneTree.SignalName.PhysicsFrame);
185+
186+
// Query the path from the navigation server.
187+
var startPosition = new Vector3(0.1f, 0.0f, 0.1f);
188+
var targetPosition = new Vector3(1.0f, 0.0f, 1.0f);
189+
190+
Vector3[] path = NavigationServer3D.MapGetPath(map, startPosition, targetPosition, optimize: true);
191+
192+
GD.Print("Found a path!");
193+
GD.Print((Variant)path);
194+
}
195+
}
196+
144197
Server Avoidance Callbacks
145198
~~~~~~~~~~~~~~~~~~~~~~~~~~
146199

0 commit comments

Comments
 (0)