Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.NET] Use collection expressions #10433

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tutorials/2d/custom_drawing_in_2d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ like this:
// We are going to paint with this color.
Color godotBlue = new Color("478cbf");
// We pass the array of Vector2 to draw the shape.
DrawPolygon(_head, new Color[]{ godotBlue });
DrawPolygon(_head, [godotBlue]);
}

When running it you should see something like this:
Expand Down Expand Up @@ -474,7 +474,7 @@ draw the line, like this:
Color white = Colors.White;
Color godotBlue = new Color("478cbf");

DrawPolygon(_head, new Color[]{ godotBlue });
DrawPolygon(_head, [godotBlue]);

// We draw the while line on top of the previous shape.
DrawPolyline(_mouth, white, _mouthWidth);
Expand Down Expand Up @@ -535,7 +535,7 @@ its radius, and the third is its color:
Color godotBlue = new Color("478cbf");
Color grey = new Color("414042");

DrawPolygon(_head, new Color[]{ godotBlue });
DrawPolygon(_head, [godotBlue]);
DrawPolyline(_mouth, white, _mouthWidth);

// Four circles for the 2 eyes: 2 white, 2 grey.
Expand Down Expand Up @@ -589,7 +589,7 @@ like this:
Color godotBlue = new Color("478cbf");
Color grey = new Color("414042");

DrawPolygon(_head, new Color[]{ godotBlue });
DrawPolygon(_head, [godotBlue]);
DrawPolyline(_mouth, white, _mouthWidth);
DrawCircle(new Vector2(42.479f, 65.4825f), 9.3905f, white);
DrawCircle(new Vector2(85.524f, 65.4825f), 9.3905f, white);
Expand Down Expand Up @@ -652,7 +652,7 @@ to do it, like this:
Color godotBlue = new Color("478cbf");
Color grey = new Color("414042");

DrawPolygon(_head, new Color[]{ godotBlue });
DrawPolygon(_head, [godotBlue]);
DrawPolyline(_mouth, white, _mouthWidth);
DrawCircle(new Vector2(42.479f, 65.4825f), 9.3905f, white);
DrawCircle(new Vector2(85.524f, 65.4825f), 9.3905f, white);
Expand Down
22 changes: 11 additions & 11 deletions tutorials/3d/procedural_geometry/arraymesh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Under ``_ready()``, create a new Array.

.. code-tab:: csharp C#

var surfaceArray = new Godot.Collections.Array();
Godot.Collections.Array surfaceArray = [];

This will be the array that we keep our surface information in - it will hold
all the arrays of data that the surface needs. Godot will expect it to be of
Expand All @@ -108,7 +108,7 @@ size ``Mesh.ARRAY_MAX``, so resize it accordingly.

.. code-tab:: csharp C#

var surfaceArray = new Godot.Collections.Array();
Godot.Collections.Array surfaceArray = [];
surfaceArray.Resize((int)Mesh.ArrayType.Max);

Next create the arrays for each data type you will use.
Expand All @@ -123,10 +123,10 @@ Next create the arrays for each data type you will use.

.. code-tab:: csharp C#

var verts = new List<Vector3>();
var uvs = new List<Vector2>();
var normals = new List<Vector3>();
var indices = new List<int>();
List<Vector3> verts = [];
List<Vector2> uvs = [];
List<Vector3> normals = [];
List<int> indices = [];

Once you have filled your data arrays with your geometry you can create a mesh
by adding each array to ``surface_array`` and then committing to the mesh.
Expand Down Expand Up @@ -196,14 +196,14 @@ Put together, the full code looks like:
{
public override void _Ready()
{
var surfaceArray = new Godot.Collections.Array();
Godot.Collections.Array surfaceArray = [];
surfaceArray.Resize((int)Mesh.ArrayType.Max);

// C# arrays cannot be resized or expanded, so use Lists to create geometry.
var verts = new List<Vector3>();
var uvs = new List<Vector2>();
var normals = new List<Vector3>();
var indices = new List<int>();
List<Vector3> verts = [];
List<Vector2> uvs = [];
List<Vector3> normals = [];
List<int> indices = [];

/***********************************
* Insert code here to generate mesh.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/best_practices/data_preferences.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ tree structures.
{
private TreeNode _parent = null;

private List<TreeNode> _children = new();
private List<TreeNode> _children = [];

public override void _Notification(int what)
{
Expand Down
4 changes: 2 additions & 2 deletions tutorials/best_practices/godot_interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ access.
{
if (EnemyScn == null)
{
return new string[] { "Must initialize property 'EnemyScn'." };
return ["Must initialize property 'EnemyScn'."];
}
return Array.Empty<string>();
return [];
}
}

Expand Down
10 changes: 5 additions & 5 deletions tutorials/math/random_number_generation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ to do it for us:
.. code-tab:: csharp

// Use Godot's Array type instead of a BCL type so we can use `PickRandom()` on it.
private Godot.Collections.Array<string> _fruits = new Godot.Collections.Array<string> { "apple", "orange", "pear", "banana" };
private Godot.Collections.Array<string> _fruits = ["apple", "orange", "pear", "banana"];

public override void _Ready()
{
Expand Down Expand Up @@ -273,7 +273,7 @@ prevent repetition:

.. code-tab:: csharp

private string[] _fruits = { "apple", "orange", "pear", "banana" };
private string[] _fruits = ["apple", "orange", "pear", "banana"];
private string _lastFruit = "";

public override void _Ready()
Expand Down Expand Up @@ -450,8 +450,8 @@ get a value from another array as follows:
// Prints a random element using the weighted index that is returned by `RandWeighted()`.
// Here, "apple" will be returned twice as rarely as "orange" and "pear".
// "banana" is twice as common as "orange" and "pear", and four times as common as "apple".
string[] fruits = { "apple", "orange", "pear", "banana" };
float[] probabilities = { 0.5, 1, 1, 2 };
string[] fruits = ["apple", "orange", "pear", "banana"];
float[] probabilities = [0.5f, 1, 1, 2];

var random = new RandomNumberGenerator();
GD.Print(fruits[random.RandWeighted(probabilities)]);
Expand Down Expand Up @@ -501,7 +501,7 @@ ends up empty. When that happens, you reinitialize it to its default value:

.. code-tab:: csharp

private Godot.Collections.Array<string> _fruits = new() { "apple", "orange", "pear", "banana" };
private Godot.Collections.Array<string> _fruits = ["apple", "orange", "pear", "banana"];
// A copy of the fruits array so we can restore the original value into `fruits`.
private Godot.Collections.Array<string> _fruitsFull;

Expand Down
22 changes: 11 additions & 11 deletions tutorials/navigation/navigation_using_navigationmeshes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,13 @@ The following script uses the NavigationServer to parse source geometry from the
// If we did not parse a TileMap with navigation mesh cells we may now only
// have obstruction outlines so add at least one traversable outline
// so the obstructions outlines have something to "cut" into.
_sourceGeometry.AddTraversableOutline(new Vector2[]
{
_sourceGeometry.AddTraversableOutline(
[
new Vector2(0.0f, 0.0f),
new Vector2(500.0f, 0.0f),
new Vector2(500.0f, 500.0f),
new Vector2(0.0f, 500.0f),
});
]);

// Bake the navigation mesh on a thread with the source geometry data.
NavigationServer2D.BakeFromSourceGeometryDataAsync(_navigationMesh, _sourceGeometry, _callbackBaking);
Expand Down Expand Up @@ -615,16 +615,16 @@ The following script uses the NavigationServer to update a navigation region wit
NavigationServer2D.RegionSetMap(_regionRid, GetWorld2D().NavigationMap);

// Add vertices for a convex polygon.
_navigationMesh.Vertices = new Vector2[]
{
_navigationMesh.Vertices =
[
new Vector2(0, 0),
new Vector2(100.0f, 0),
new Vector2(100.0f, 100.0f),
new Vector2(0, 100.0f),
};
];

// Add indices for the polygon.
_navigationMesh.AddPolygon(new int[] { 0, 1, 2, 3 });
_navigationMesh.AddPolygon([0, 1, 2, 3]);

NavigationServer2D.RegionSetNavigationPolygon(_regionRid, _navigationMesh);
}
Expand Down Expand Up @@ -680,16 +680,16 @@ The following script uses the NavigationServer to update a navigation region wit
NavigationServer3D.RegionSetMap(_regionRid, GetWorld3D().NavigationMap);

// Add vertices for a convex polygon.
_navigationMesh.Vertices = new Vector3[]
{
_navigationMesh.Vertices =
[
new Vector3(-1.0f, 0.0f, 1.0f),
new Vector3(1.0f, 0.0f, 1.0f),
new Vector3(1.0f, 0.0f, -1.0f),
new Vector3(-1.0f, 0.0f, -1.0f),
};
];

// Add indices for the polygon.
_navigationMesh.AddPolygon(new int[] { 0, 1, 2, 3 });
_navigationMesh.AddPolygon([0, 1, 2, 3]);

NavigationServer3D.RegionSetNavigationMesh(_regionRid, _navigationMesh);
}
Expand Down
24 changes: 12 additions & 12 deletions tutorials/navigation/navigation_using_navigationobstacles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ Obstacles are not involved in the source geometry parsing so adding them just be

.. code-tab:: csharp 2D C#

Vector2[] obstacleOutline = new Vector2[]
{
Vector2[] obstacleOutline
[
new Vector2(-50, -50),
new Vector2(50, -50),
new Vector2(50, 50),
new Vector2(-50, 50),
};
];

var navigationMesh = new NavigationPolygon();
var sourceGeometry = new NavigationMeshSourceGeometryData2D();
Expand Down Expand Up @@ -123,13 +123,13 @@ Obstacles are not involved in the source geometry parsing so adding them just be

.. code-tab:: csharp 3D C#

Vector3[] obstacleOutline = new Vector3[]
{
Vector3[] obstacleOutline =
[
new Vector3(-5, 0, -5),
new Vector3(5, 0, -5),
new Vector3(5, 0, 5),
new Vector3(-5, 0, 5),
};
];

var navigationMesh = new NavigationMesh();
var sourceGeometry = new NavigationMeshSourceGeometryData3D();
Expand Down Expand Up @@ -234,13 +234,13 @@ For static use an array of ``vertices`` is required.
NavigationServer2D.ObstacleSetRadius(newObstacleRid, 5.0f);

// Use obstacle static by adding a square that pushes agents out.
Vector2[] outline = new Vector2[]
{
Vector2[] outline =
[
new Vector2(-100, -100),
new Vector2(100, -100),
new Vector2(100, 100),
new Vector2(-100, 100),
};
];
NavigationServer2D.ObstacleSetVertices(newObstacleRid, outline);

// Enable the obstacle.
Expand Down Expand Up @@ -280,13 +280,13 @@ For static use an array of ``vertices`` is required.
NavigationServer3D.ObstacleSetRadius(newObstacleRid, 5.0f);

// Use obstacle static by adding a square that pushes agents out.
Vector3[] outline = new Vector3[]
{
Vector3[] outline =
[
new Vector3(-5, 0, -5),
new Vector3(5, 0, -5),
new Vector3(5, 0, 5),
new Vector3(-5, 0, 5),
};
];
NavigationServer3D.ObstacleSetVertices(newObstacleRid, outline);
// Set the obstacle height on the y-axis.
NavigationServer3D.ObstacleSetHeight(newObstacleRid, 1.0f);
Expand Down
8 changes: 4 additions & 4 deletions tutorials/navigation/navigation_using_navigationservers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ Afterwards the function waits for the next physics frame before continuing with
// Create a procedural navigation mesh for the region.
var newNavigationMesh = new NavigationMesh()
{
Vertices = new[]
{
Vertices =
[
new Vector3(0.0f, 0.0f, 0.0f),
new Vector3(9.0f, 0.0f, 0.0f),
new Vector3(0.0f, 0.0f, 9.0f),
},
],
};
int[] polygon = new[] { 0, 1, 2 };
int[] polygon = [0, 1, 2];
newNavigationMesh.AddPolygon(polygon);
NavigationServer3D.RegionSetNavigationMesh(region, newNavigationMesh);

Expand Down
6 changes: 5 additions & 1 deletion tutorials/networking/http_client_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ It will connect and fetch a website.
Debug.Assert(http.GetStatus() == HTTPClient.Status.Connected); // Check if the connection was made successfully.

// Some headers.
string[] headers = { "User-Agent: Pirulo/1.0 (Godot)", "Accept: */*" };
string[] headers =
[
"User-Agent: Pirulo/1.0 (Godot)",
"Accept: */*",
];

err = http.Request(HTTPClient.Method.Get, "/ChangeLog-5.php", headers); // Request a page from the site.
Debug.Assert(err == Error.Ok); // Make sure all is OK.
Expand Down
4 changes: 2 additions & 2 deletions tutorials/networking/http_request_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ But what if you need to send data to the server? Here is a common way of doing i
.. code-tab:: csharp

string json = Json.Stringify(dataToSend);
string[] headers = new string[] { "Content-Type: application/json" };
string[] headers = ["Content-Type: application/json"];
HttpRequest httpRequest = GetNode<HttpRequest>("HTTPRequest");
httpRequest.Request(url, headers, HttpClient.Method.Post, json);

Expand All @@ -147,7 +147,7 @@ For example, to set a custom user agent (the HTTP ``User-Agent`` header) you cou
.. code-tab:: csharp

HttpRequest httpRequest = GetNode<HttpRequest>("HTTPRequest");
httpRequest.Request("https://api.github.com/repos/godotengine/godot/releases/latest", new string[] { "User-Agent: YourCustomUserAgent" });
httpRequest.Request("https://api.github.com/repos/godotengine/godot/releases/latest", ["User-Agent: YourCustomUserAgent"]);

.. danger::

Expand Down
4 changes: 2 additions & 2 deletions tutorials/physics/ray-casting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ from a CharacterBody2D or any other collision object node:
{
var spaceState = GetWorld2D().DirectSpaceState;
var query = PhysicsRayQueryParameters2D.Create(globalPosition, playerPosition);
query.Exclude = new Godot.Collections.Array<Rid> { GetRid() };
query.Exclude = [GetRid()];
var result = spaceState.IntersectRay(query);
}
}
Expand Down Expand Up @@ -263,7 +263,7 @@ member variable. The array of exceptions can be supplied as the last argument as
{
var spaceState = GetWorld2D().DirectSpaceState;
var query = PhysicsRayQueryParameters2D.Create(globalPosition, targetPosition,
CollisionMask, new Godot.Collections.Array<Rid> { GetRid() });
CollisionMask, [GetRid()]);
var result = spaceState.IntersectRay(query);
}
}
Expand Down
12 changes: 6 additions & 6 deletions tutorials/scripting/c_sharp/c_sharp_exports.rst
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,12 @@ The default value of Godot arrays is null. A different default can be specified:
.. code-block:: csharp

[Export]
public Godot.Collections.Array<string> CharacterNames { get; set; } = new Godot.Collections.Array<string>
{
public Godot.Collections.Array<string> CharacterNames { get; set; } =
[
"Rebecca",
"Mary",
"Leah",
};
];

Arrays with specified types which inherit from resource can be set by
drag-and-dropping multiple files from the FileSystem dock.
Expand Down Expand Up @@ -588,11 +588,11 @@ The default value of C# arrays is null. A different default can be specified:
.. code-block:: csharp

[Export]
public Vector3[] Vectors { get; set; } = new Vector3[]
{
public Vector3[] Vectors { get; set; } =
[
new Vector3(1, 2, 3),
new Vector3(3, 2, 1),
}
];

Setting exported variables from a tool script
---------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tutorials/scripting/cross_language_scripting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ to said method.
// Outputs "Hello there!" twice, once per line.
myGDScriptNode.Call("print_n_times", "Hello there!", 2);

string[] arr = new string[] { "a", "b", "c" };
string[] arr = ["a", "b", "c"];
// Output: "a", "b", "c" (one per line).
myGDScriptNode.Call("print_array", arr);
// Output: "1", "2", "3" (one per line).
Expand Down
Loading