I Have 2 Scenes - One is the main Scene and the other is a scene which is actually just an object i want to add on a button the to main scene. My problem is that this Object has a script attached to it (this is Area2D object) and in this script i can find nodes with a function “GetNode” and the name but i cannot find the main Object. What is the correct syntax ind C# to get an main object of a second scene ? I know the first scene has a name “/root/…” but the second scene ? I need this to change a position with a script of this object
If the script is already attached to main object, the object you want is the self variable already right? No need to use GetNode. Or maybe I understood it wrong
Thank You for your respond. Yes it is attachted and i tried this but i failed. Now when i think about i may have made a general fault. I want to change the position of this object when i click on it and move with the mouse. This object should be added to the main scene when i press some key in the main scenario. I saw a tutorial that adding dynamicly an object should be done by creating a new scene and then adding it to the main scene. The problem might be that the main node in the second Scene ( my object) is an Area2D which i think has no property “Position” (which would make Sense now). I dont know if i can add new scene that has for example “Node2d” as a root node ?
Object is added to place where you added child.
before you add objects you needed have load them from PackedScene and Instantiate
var scene = ResourceLoader.Load<PackedScene>("res://scene.tscn").Instantiate(); // Load and Instantiate
AddChild(scene); // Add to script
GD.Print(scene.GetPath());
Thank You for the tips. Unfortunatelly it still doesnt work. I cannot even get to the node.
I dont understand why in the second scene i can get a reference from an child but not from the object where the script is attachted to
PK_Light = GetNode(“Light3D”); // works
PK_this_Obj = GetNode(“RB”); // doesnt work
Maybe i was suppose to mention it at the beginning that i am new to godot but this i think is clear now. I am not programming any game. I am trying to make a simulation in 2d of a real production line which then i can programm communication with a PLC and test my PLC programm before i go to a real production. I already done it with other Game engine but i saw that Godot is very powerfull for 2D graphic. This is the reason why i do it all in C# - i already programmed some functions and i want to use them
RB node is this PK_this_Obj = this;
if your node is this we don’t use or needed GetNode etc. and you don’t needed reference for this in this
when you use GetNode("NodePath") is really you use this.GetNode("NodePath") but for make code clear we don’t use second style.
this is most used when you want method from other object and want send reference from calling class.
Class A
public B b;
public void Method(B b)
{//method body}
Class B
{
public override void _Ready() // this method is godot specify, used to look easier
{
A a = new A(); // Creating variable and instance of class A with no argument constructor
a.b = this; // in instance of Class A we setting reference to "this" class (And is B)
a.Method(this); // we call public method from instance of class A with referring this as argument
}
}