How do I use GetNode().variable in C# godot

Godot Version

4.3.stable.mono

Question

I can’t seem to get a variable from another node no matter what I do, I’m not sure what the problem is and I’m very new to C#

this is the code from the parent

public partial class Node2dParent : Node2D
{
	//A string that the parent holds, this is meant to be accessed by the child node
	public string ParentVar = "parent var";
	public override void _Ready()
	{
		//this Gets the parent node
		var Child = this.GetNode<Node2D>("ParentNode2D");
		//this is supposed to print the ChidVar variable that is held by the child node
		GD.Print(Child.ChildVar);
	}
}

this is the code from the child

public partial class Node2dchild : Node2D
{
	//this assigns a variable that is meant to be used by the parent node
	public string ChildVar = "child var";
	public override void _Ready()
	{
		//this gets the parent node
		var parent = this.GetNode<Node2D>("ParentNode2D");
		//this is meant to print the Parent Var variable that is heled by the parent
		GD.Print(parent.ParentVar);
	}
}

my question is what ways can I get a variable from a parent and what ways can I get a variable from a child?

Name of your child is “ParentNode2D”?
Instead of Node2D use class of child in “< >” and you will have acces to public variables

2 Likes

The issue here is a typing issue. var infers the type from the right side of the assignment.

this is the same as writing the following:

public partial class Node2dParent : Node2D
{
		//...
		Node2D Child = this.GetNode<Node2D>("ParentNode2D");

		GD.Print(Child.ChildVar);
}

But the Node2D class does not have a member variable “ChildVar”.
Only the Node2dchild class has this member variable.

You’re telling the compiler that it is working with a Node2D class, but what you actually want is telling it that’s a Node2dChild class.
In our case, this means changing the type you expect, from GetNode<Node2D> to GetNode<Node2dchild>:

Node2dchild Child = this.GetNode<Node2dchild>("ParentNode2D")

the same applies to the other assignment, here you want:

Node2dParent parent = this.GetNode<Node2dParent>("ParentNode2D");

In general I’d advise you to forget that var exists in C#, at least for now - being explicit will help you build an understanding for types.


As a side node, I’m not sure if your paths / references in GetNode are correct.

If your tree looks like this

ParentNode2D (with script Node2dParent)
→ ChildNode2D (with script Node2dchild)

then you can get the child from the parent via GetNode<Node2dchild>("ChildNode2D")

and the parent from the child via
GetParent<Node2dParent>()

1 Like

Try with this:

public partial class Node2dParent : Node2D
{
	//A string that the parent holds, this is meant to be accessed by the child node
	public string ParentVar = "parent var";
	public override void _Ready()
	{
		//this Gets the parent node
		var Child = this.GetNode<Node2D>("ParentNode2D");
		//this is supposed to print the ChidVar variable that is held by the child node
		GD.Print((string)Child.ChildVar);
	}
}

The “(string)” tells that “ChildVar” is of type “string”
I think this should work
Hope this will be helpful

No, no need to cast a string to a string. That’s not the issue here. Even if Child.ChildVar was not a string, GD.Print would still be able to handle it in most (all?) cases.

It worked, thanks so much! I ran into another problem with it not finding the nodes when _Ready() was called, I think this is just a problem with godot but after I assigned the getnode and print its variable code i had to a key press everything was loaded by then

1 Like

@substain I was also wondering if this would work with other nodes like a CharacterBody2D or an Area2D, is there a different way to go about those (Node2dchild seems to only work with actual Node2D elements, maybe I’m wrong about this?)


obraz
but you can lose access to specific to node variables in scripts.
If you attach custom node2D script to Area2D you will be unable get colliders etc via code

1 Like

usually the best approach is to get as specific as you can (see @Moreus answer as well) - unless you can’t know or don’t care about the more specific type

@taartplant if you want store data in nodes is good idea to use Metadata they don’t needed scripts attached to have it, and all nodes can have it. :slight_smile: and you dont care what type of node it is :slight_smile:



obraz

Thanks a bunch. The setmeta, getmeta functions are working and I’m now able to transfer information between nodes. Sorry if I troubled you guys. I really appreciate the help

1 Like

Thank you for the tip, I’ll definitely look into the different ways I can type things, before this post I thought I could only type things as strings, bools, ints, and floats. My eyes are now opened. Thanks for sharing your knowledge.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.