Wednesday, November 8, 2023

November Update

I want to mention briefly that I have changed my YouTube and Twitter handles.  Eyesgood is a name I have long used in gaming and I have really wanted to identify with a different handle in the context of my game development life.  Yesterday, I had a moment of inspiration and came up with:  Bound2bCoding.  Look for that name going forward as I sunset the use of Eyesgood in my dev life.

October was an amazing month of development work.  While working on converting the inventory engine to C#, I expanded the capabilities of the inventory system to handle scenarios that were not even considered in Godot 3.  I am still in the midst of this translating and refactoring process which I expect to run out to the end of the year.

C# Tip:  Unsubscribe C# Events Before Removing Scenes

One thing I discovered while testing my C# version may be of interest to any developers reading this.  I learned that C# events that are subscribed to are NOT unsubscribed just because the node is removed from the scene tree in Godot.  For example, I have a singleton named InventoryEngine which handles all the drag and drop transactions among other things.  I also have an InventoryWindow scene that subscribes to an event in the InventoryEngine.  The window node is instantiated each time a container is opened.  When the inventory window is closed, it is removed from the scene tree along with all its children.  

InventoryEngine (singleton)  

    public event EventHandler<int> RefreshContainerWindow;

InventoryWindow (node)    

    _inventoryEngine.RefreshContainerWindow += On_InventoryEngine_RefreshContainerWindow;   

InventoryWindow (node)

        _inventoryEngine.RefreshContainerWindow += On_InventoryEngine_RefreshContainerWindow;

I was having weird issues with my inventory windows not refreshing when dragging and dropping items.  It took me a long while to realize that events were firing from nodes that were no longer in the scene.  This was because I was not using Godot Signals, but rather ordinary C# event handlers.  According to the documentation, Godot handles the Godot Signals for disposed objects, but it does NOT handle any C# event listeners.  To fix this, I had to unsubscribe those subscribers in the InventoryWindow code behind before deleting the window node.  This is done in the _ExitTree() method.

public override void _ExitTree()
{
    _inventoryEngine.RefreshContainerWindow -= On_InventoryEngine_RefreshContainerWindow;          
    base._ExitTree();
}

I hope this helps someone else who may be pulling their hair out trying to track down this kind of issue.

Visual Studio Tip:  Don't forget to point your debug to the new DLL when upgrading Godot.

Here is another gotcha for Visual Studio.  When you upgrade your Godot version, Visual Studio => {project name}Debug Properties => Path to the executable, and click Browse to locate the new Godot executable.  This will allow debugging in the version you have upgraded to.

That's all for now.  I hope you have learned something useful.

Until next time...

I'm Bound2bCoding


No comments:

Post a Comment

Feel free to leave a comment. All comments are moderated.