0 votes

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.

in Engine by (57 points)
edited by

3 Answers

+1 vote
Best answer

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_/

by (14,748 points)
selected by

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?

Thanks again, that removeAt method was very helpful!

+1 vote

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>?

by (864 points)

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

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

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

+1 vote

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

by (939 points)

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 :)

It was fun remaking!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.