neikeq / GodotSharp

Moved to the Godot repo: https://github.com/godotengine/godot/tree/master/modules/mono
MIT License
60 stars 8 forks source link

Node Instance Issue #43

Open nobnob2015 opened 6 years ago

nobnob2015 commented 6 years ago

I'm trying to instance a node I created. The node 'BaseBall' is a within it's own scene and has a sprite.

On a new scene I'm trying to instance a ball (I'll want to have a few of these balls all over the place eventually).

public override void _Ready()
{
    GD.print("Setup New Node");
    // var myNode = GetNode("res://BaseBall") as BaseBall;
    // var myNode = GetNode("/root/BaseBall") as BaseBall;
    // var myNode = GetNode("res://root/BaseBall") as BaseBall;
    var myNode = GetNode("BaseBall") as BaseBall;
    if(myNode == null) 
    {
        GD.print("Node is NULL!");
    } 
}

I always get node as NULL.

As you can see from comments I've tried a few different versions.

NathanWarden commented 6 years ago

I bet neikeq is going to want an example project to reproduce this. Also, did you try the equivelant code using GDScript? I find myself doing this because:

A) If I know I'm doing it right, and it isn't working in GDScript then it's possibly a Godot issue B) If it's working in GDScript then you know for sure that your code is correct and that it's a C# issue

I was trying to get OS.MsecTicks() about a month ago and the game was just crashing. I thought it was a C# issue and was about to report it, but thought I might as well try it in GDScript first. Sure enough, Godot crashed using GDScript as well, so it wasn't a C# issue.

Hope that helps :) Nathan

neikeq commented 6 years ago

Yes, I can't reproduce so an example project would be great.

27thLiz commented 6 years ago

I'm not sure, you want to create a new instance of your scene right? GetNode can only get nodes which are already in the scene tree/instanced. To create a new instance you'll have to Godot.Load() the scene and use .instance() and AddChild(), just like in GDScript. http://docs.godotengine.org/en/stable/learning/step_by_step/scripting_continued.html#instancing-scenes