Modify the property of a specific node using a plugin then save it

Godot Version

v4.3.stable.mono.official [77dcf97d8]

Question

Using a plugin written in C#, how can I do the following:
I have a Node called GameBase, which is the base node in my game that I use as “root”. It has a script attached to it that is more or less just the following:

public partial class GameBase : Node
{
	[Export] public GameMap GameMap { get; set; }
}

I have an editor plugin I wrote which has a button, when I click said button, I want a way to find that GameBase node, open it, Change the current “GameMap” instance to be something else, then save it.

So far, I tried the following:

private void SwitchStartupMap(GameName.Scripts.Resources.GameMap map)
{
	PackedScene scene = ResourceLoader.Load<PackedScene>(GameBasePath);
	GameBase inst = scene.Instantiate<GameBase>();
	inst.GameMap = map;
	
	scene.Pack(inst);
	ResourceSaver.Save(scene, GameBasePath);
	
	if (!IsSceneOpen(GameBasePath)) return;
	
	EditorInterface.Singleton.ReloadSceneFromPath(GameBasePath);
}

However, when I actually click the button, Godot screams at me that I cannot cast “Node” to “GameBase” even though that scene literally has the script attached to it, so I’m lost as to how to make this happen.

is all [tool]?
resources can be better for saving data.

1 Like

The code which runs inside the editor indeed has the Tool attached to it, but GameBase does not. And I’m not trying to store data. That’s already being done with the GameMap class, as it’s a Resource. GameBase needs to be a Node, and once the game starts, based on that one Property, it will generate everything I need for the game to start and launch on the map I want it to.

Okay, I’ll be honest, I find this quite strange, but changing GameBase to also have the Tool attribute did produce the result I expected, but I fail to see why. Without the Tool attribute, why was it NOT able to cast Node to GameBase, but with the Tool attribute, it could?

I ask because before I started reworking how this system works, I could easily remove and add new children to the GameBase node without it having the Tool attribute, only my editor plugin. So how come now GameBase needs the Tool attribute as well when I try to change a property of it?

Most plugins run only in editor, and everything in editor needed be [tool] like your classes etc when you try access them.

Just find it strange behavior that I could easily change out children or remove children from a Node that had no Tool attribute, but as soon as I try to change one of it’s properties, it requires the Tool attribute. But anyway, that did solve my issue, so thank you.

1 Like