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?
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>:
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
@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?)
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
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. and you dont care what type of node it is
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
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.