#[C#] _EnterTree() not getting called

1 messages · Page 1 of 1 (latest)

dawn marten
#

I'm attempting to instantiate a node the way that I've done many times before, and for some reason I don't understand its _EnterTree() isn't getting called. I assume something I'm doing is causing a silent failure under the hood, but I don't know what it would be.

// In a separate .dll that my main game is referencing
public partial class MyCustomMultiplayerSpawner : MultiplayerSpawner
{
  public override void _EnterTree()
  {
    GD.Print("Entered tree");
    // other more important logic too
  }
}

// In my main game
var spawner = new MyCustomMultiplayerSpawner();
spawner.Name = "Spawner";
Node parent = <some other node>;
Debug.Assert(parent.IsInsideTree()); // succeeds
parent.AddChild(spawner);

I would expect _EnterTree() on the spawner to be called as part of the last line there, but that doesn't seem to happen and I can't figure out why not. Is it a problem that MyCustomMultiplayerSpawner is in a separate .dll? Do I need to create it in a different way or something?

Thanks for any help!

(I could manually call _EnterTree(), but it seems like this issue is indicative of more stuff going wrong; I also set the MultiplayerSpawner's SpawnFunction from within there, and that's failing with ERROR: Custom spawn requires the 'spawn_function' property to be a valid callable. which I assume is related to the fact that this spawner isn't considered to be a valid node in the tree. Although when I print a list of parent.GetChildren() I do indeed see the spawner there, so 🤷 ).

young spade
#

I can't answer your question, but it seems to me that you can try to eliminate the chance that the class being in a different dll is the issue. Maybe you also want to check if the culprit is higher up in the scene tree (esp. instanced scenes)? Try to hone in on the cause by eliminating possibilities. Sorry if I'm not telling you anything new, but personally, I sometimes need to be reminded of my innate problem solving skills 😉

void viper
#

When you want to create a new Node, you should be doing it my calling Instantiate() on a packed scene object. In your code you're just doing it with the new keyword. You can find documentation on it here

#

since you seem experienced with C#, I'll paste the code I'm using in my game for a generic factory. I think it will give you the idea without needing to read the docs:

public partial class Node2DFactory<T> : Node where T : Node2D
{

    #region Prefabs

    [Export(PropertyHint.NodeType)]
    public PackedScene Prefab;

    #endregion

    protected T Instantiate()
    {
        var newItemObject = (T)Prefab.Instantiate();
        return newItemObject;
    }
    
}
#

Basically try replacing
var spawner = new MyCustomMultiplayerSpawner();
with

[Export(PropertyHint.NodeType)]
public PackedScene SpawnerPrefab;

...

var spawner = (MyCustomMultiplayerSpawner)SpawnerPrefab.Instantiate();```
#

and attaching the MyCustomMultiplayerSpawner tscn file to the script in the inspector for SpawnerPrefab

#

if you still need clarification let me know!

#

I think Godot isn't creating the node properly when you just use the new TypeName() syntax, which is likely causing your issue, as well as a bunch of other problems down the line.