What would be the equivalent of Vector2[].remove in C#?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AlMoeSharpton86

I’m following this tutorial
https://www.davidepesce.com/2019/11/19/godot-tutorial-how-to-use-navigation2d-for-pathfinding/

I’m trying to follow this tutorial which uses GDScript but in C#.

In the final section

   else:
			# The player get to the next point
			position = path[0]
			path.remove(0)
		# Update the distance to walk
		distance_to_walk -= distance_to_next_point

That “path.remove(0)” I can’t seem to find in C#, it seems arrays are more limited in C# or I’m I doing something wrong. I have also tried to define my vector2 array as like

"Godot.Collections.Array< Vector2 > path " to access addtional methods such as remove, clear and such but that gives me conversion errors “Can’t convert between Godot Collections and Vector2D etc.” In other code.

But if I define a variable like ‘Vector2 path’ , it doesnt have the extra useful methods like remove, clear and such. Am I missing something?

Thank you.

PS: Path is a Vector2D object.

:bust_in_silhouette: Reply From: jgodfrey

I don’t use the C# version of Godot, but I’d expect C# Array object to have a RemoveAt(index) method that should be the equivalent of the mentioned gdscript code. That should remove an item from the array at the specified index.

In case you don’t know, Godot’s C# API is here:

https://godotsharp.net/api/3.2.0/

Here’s theArray docs:

https://godotsharp.net/api/3.2.0/Godot.Collections.Array/

And the Array<T> docs:

https://godotsharp.net/api/3.2.0/Godot.Collections.Array_T_/

Thank you JGodfrey, one of my problems if I use a C# array like that like "Godot.Collections.Array< Vector2D > " for example I get conversion errors asforementioned, maybe it’s something I’m misunderstanding?

You don’t just define a 2D Vector array just like Vector2D variableName?

AlMoeSharpton86 | 2020-06-21 20:37

Thanks again, that removeAt method was very helpful!

AlMoeSharpton86 | 2020-06-22 16:46

:bust_in_silhouette: Reply From: JimArtificer

One of the nice things about using C# is that you are not limited to the types in GDScript. Have you tried using System.Collections.Generic.List<Vector2>?

Thank you, haven’t tried with Lists, I’ll try that!

AlMoeSharpton86 | 2020-06-22 15:31

Cannot implicitly convert type ‘System.Collections.Generic.List<Godot.Vector2>’ to ‘Godot.Vector2’ How would you solve this issue?

AlMoeSharpton86 | 2020-06-22 15:41

Thank you! Lists and types were the correct way to go, I used the method .ToArray to solve the conversion errors. Thank you :slight_smile:

AlMoeSharpton86 | 2020-06-22 16:30

:bust_in_silhouette: Reply From: juppi

I had some fun remaking that tutorial in C#.
I converted that Array Vector2[] path just into a List: player.Path = new List<Vector2>(path)

In a List you can just remove an object Path.Remove(Path[0])

You can find the code on GitHub: https://github.com/JupiterRider/Navigation2D-Completed-CSharp

Thank you! Good to see another Godot C# user, I used lists to solve it as the above answer suggested to. Thank you for the Github link and help :slight_smile:

It was fun remaking!

AlMoeSharpton86 | 2020-06-22 23:08