How to use LocalToMap in c# to get the map coordinates of the player position?

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

I’m in the process of converting my script from gdscript to c# and i’m unsure why this throws an error while running the project.

Vector2I tile_coords = top_layer.LocalToMap(player_position);

— Debugging process stopped —
Cannot open file ‘/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.UnmanagedCallbacksGenerator/Godot.NativeInterop.NativeFuncs.generated.cs’.
Failed to read file: ‘/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.UnmanagedCallbacksGenerator/Godot.NativeInterop.NativeFuncs.generated.cs’.
Cannot load C# script file ‘/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.UnmanagedCallbacksGenerator/Godot.NativeInterop.NativeFuncs.generated.cs’.
Failed loading resource: /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.UnmanagedCallbacksGenerator/Godot.NativeInterop.NativeFuncs.generated.cs. Make sure resources have been imported by opening the project in the editor at least once.


E 0:00:00:0806 Godot.NativeInterop.NativeFuncs.generated.cs:332 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* ): Condition “!tile_set.is_valid()” is true. Returning: Vector2i()
<C++ Source> scene/2d/tile_map.cpp:3259 @ local_to_map()
Godot.NativeInterop.NativeFuncs.generated.cs:332 @ void Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr , IntPtr , System.Void** , System.Void* )
NativeCalls.cs:10069 @ Godot.Vector2I Godot.NativeCalls.godot_icall_1_1095(IntPtr , IntPtr , Godot.Vector2* )
TileMap.cs:701 @ Godot.Vector2I Godot.TileMap.LocalToMap(Godot.Vector2 )
layered_map_gen.cs:115 @ void layered_map_gen+<generate_chunks>d__37.MoveNext()
:0 @ void System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start(TStateMachine& )
:0 @ void System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start(TStateMachine& )
:0 @ void layered_map_gen.generate_chunks(Godot.Vector2 )
layered_map_gen.cs:102 @ void layered_map_gen._Process(Double )
Node.cs:1795 @ Boolean Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
CanvasItem.cs:1371 @ Boolean Godot.CanvasItem.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
Node2D.cs:516 @ Boolean Godot.Node2D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
layered_map_gen_ScriptMethods.generated.cs:59 @ Boolean layered_map_gen.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(IntPtr , Godot.NativeInterop.godot_string_name* , Godot.NativeInterop.godot_variant** , Int32 , Godot.NativeInterop.godot_variant_call_error* , Godot.NativeInterop.godot_variant* )

Make sure resources have been imported by opening the project in the editor at least once.

Is featured in the stack trace. Perhaps a file or resource was added to the project outside of the godot editor, which creates .import files with additional metadata.

spaceyjase | 2023-04-17 10:43

:bust_in_silhouette: Reply From: DocRobs

I’m an idiot, it was because i didn’t assign a TileSet to the tilemap…