|
@@ -29,9 +29,9 @@ a change in API:
|
|
const MyNode = preload("my_node.gd")
|
|
const MyNode = preload("my_node.gd")
|
|
const MyScene = preload("my_scene.tscn")
|
|
const MyScene = preload("my_scene.tscn")
|
|
var node = Node.new()
|
|
var node = Node.new()
|
|
- var my_node = MyNode.new() # Same method call
|
|
|
|
- var my_scene = MyScene.instantiate() # Different method call
|
|
|
|
- var my_inherited_scene = MyScene.instantiate(PackedScene.GEN_EDIT_STATE_MAIN) # Create scene inheriting from MyScene
|
|
|
|
|
|
+ var my_node = MyNode.new() # Same method call.
|
|
|
|
+ var my_scene = MyScene.instantiate() # Different method call.
|
|
|
|
+ var my_inherited_scene = MyScene.instantiate(PackedScene.GEN_EDIT_STATE_MAIN) # Create scene inheriting from MyScene.
|
|
|
|
|
|
.. code-tab:: csharp
|
|
.. code-tab:: csharp
|
|
|
|
|
|
@@ -39,8 +39,10 @@ a change in API:
|
|
|
|
|
|
public partial class Game : Node
|
|
public partial class Game : Node
|
|
{
|
|
{
|
|
- public static CSharpScript MyNode { get; } = GD.Load<CSharpScript>("MyNode.cs");
|
|
|
|
- public static PackedScene MyScene { get; } = GD.Load<PackedScene>("MyScene.tscn");
|
|
|
|
|
|
+ public static CSharpScript MyNode { get; } =
|
|
|
|
+ GD.Load<CSharpScript>("res://Path/To/MyNode.cs");
|
|
|
|
+ public static PackedScene MyScene { get; } =
|
|
|
|
+ GD.Load<PackedScene>("res://Path/To/MyScene.tscn");
|
|
private Node _node;
|
|
private Node _node;
|
|
private Node _myNode;
|
|
private Node _myNode;
|
|
private Node _myScene;
|
|
private Node _myScene;
|
|
@@ -50,8 +52,10 @@ a change in API:
|
|
{
|
|
{
|
|
_node = new Node();
|
|
_node = new Node();
|
|
_myNode = MyNode.New().As<Node>();
|
|
_myNode = MyNode.New().As<Node>();
|
|
- _myScene = MyScene.Instantiate(); // Different. Instantiated from a PackedScene
|
|
|
|
- _myInheritedScene = MyScene.Instantiate(PackedScene.GenEditState.Main); // Create scene inheriting from MyScene
|
|
|
|
|
|
+ // Different than calling new() or MyNode.New(). Instantiated from a PackedScene.
|
|
|
|
+ _myScene = MyScene.Instantiate();
|
|
|
|
+ // Create scene inheriting from MyScene.
|
|
|
|
+ _myInheritedScene = MyScene.Instantiate(PackedScene.GenEditState.Main);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -155,14 +159,14 @@ with it, and finally adds it as a child of the ``Main`` node:
|
|
var child = Node.new()
|
|
var child = Node.new()
|
|
child.name = "Child"
|
|
child.name = "Child"
|
|
child.script = preload("child.gd")
|
|
child.script = preload("child.gd")
|
|
- child.owner = self
|
|
|
|
add_child(child)
|
|
add_child(child)
|
|
|
|
+ child.owner = self
|
|
|
|
|
|
.. code-tab:: csharp
|
|
.. code-tab:: csharp
|
|
|
|
|
|
using Godot;
|
|
using Godot;
|
|
|
|
|
|
- public partial class Main : Resource
|
|
|
|
|
|
+ public partial class Main : Node
|
|
{
|
|
{
|
|
public Node Child { get; set; }
|
|
public Node Child { get; set; }
|
|
|
|
|
|
@@ -170,9 +174,13 @@ with it, and finally adds it as a child of the ``Main`` node:
|
|
{
|
|
{
|
|
Child = new Node();
|
|
Child = new Node();
|
|
Child.Name = "Child";
|
|
Child.Name = "Child";
|
|
- Child.SetScript(GD.Load<Script>("child.gd"));
|
|
|
|
- Child.Owner = this;
|
|
|
|
|
|
+ var childID = Child.GetInstanceId();
|
|
|
|
+ Child.SetScript(GD.Load<Script>("res://Path/To/Child.cs"));
|
|
|
|
+ // SetScript() causes the C# wrapper object to be disposed, so obtain a new
|
|
|
|
+ // wrapper for the Child node using its instance ID before proceeding.
|
|
|
|
+ Child = (Node)GodotObject.InstanceFromId(childID);
|
|
AddChild(Child);
|
|
AddChild(Child);
|
|
|
|
+ Child.Owner = this;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -207,7 +215,7 @@ In the end, the best approach is to consider the following:
|
|
.. code-tab:: gdscript GDScript
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
# game.gd
|
|
# game.gd
|
|
- class_name Game # extends RefCounted, so it won't show up in the node creation dialog
|
|
|
|
|
|
+ class_name Game # extends RefCounted, so it won't show up in the node creation dialog.
|
|
extends RefCounted
|
|
extends RefCounted
|
|
|
|
|
|
const MyScene = preload("my_scene.tscn")
|
|
const MyScene = preload("my_scene.tscn")
|
|
@@ -222,7 +230,8 @@ In the end, the best approach is to consider the following:
|
|
// Game.cs
|
|
// Game.cs
|
|
public partial class Game : RefCounted
|
|
public partial class Game : RefCounted
|
|
{
|
|
{
|
|
- public static PackedScene MyScene { get; } = GD.Load<PackedScene>("MyScene.tscn");
|
|
|
|
|
|
+ public static PackedScene MyScene { get; } =
|
|
|
|
+ GD.Load<PackedScene>("res://Path/To/MyScene.tscn");
|
|
}
|
|
}
|
|
|
|
|
|
// Main.cs
|
|
// Main.cs
|