C# Type Casting

Godot Version



I spent all day on this yesterday sadly and i’m begrudgingly throwing in the towel. I have an a TileManager class that extends Node2d. In that class is a Dictionary i would like to access. If i set it up like this then it will allow me to set the node i want to access in the editor but then the access to the Dictionary isnt there because its not part of Node2D. I cant seem to figure out how i need to cast to get access to that Dictionary. The code below is just snippets with relevant areas

public partial class WorldGen : TileMap
	public FastNoiseLite noise;
	public Node2D tileManager;

       //Currently not working because im declaring tileManager 
as Node2D however trying to do public TileManager tilemanager
 results in potential access however it wont let me set it in editor 
as it only wants Node2D

       tileManager.tileHealth.Add(new Vector2I(x,y),3);
public Node2D tileManager;
no needed public if you want only get from inspector
		if (tileManager is TileManager _tileManager)
		_tileManager.tileHealth.Add(new Vector2I(x,y),3);


else error or anything

I ended up just coming up with another solution for tileManager but im having the same exact issue with any Reference I try to get.

	private Inventory inventoryRef;

inventoryRef.inventory.Add(tileType, 1);

Im selecting my Inventory node in the inspector and it seems to be fine however
the preceding attempt to add to inventory always returns a null instance

Well i tracked that issue down… Turns out it had nothing to do with the reference and everything to do with how i declared the dictionary in Inventory…

Doesn’t Work

public Dictionary inventory;

Does Work

public Dictionary inventory = new Dictionary();

I don’t see why this shouldn’t work. You should be able to drag the instance of your Tile Manager to that in the inspector.