Browse Source

Improve C# GD.Load() examples

* Use typed version of GD.Load()
* Use absolute paths (i.e., prefixed with "res://")
* Use "Path/To/" intermediate path values on abstract examples, to
  illustrate that files can live in various directories
* Use PascalCase on files that are not GDScript or GLSL
Mark Wilson 1 year ago
parent
commit
147d2233c7

+ 3 - 3
tutorials/best_practices/godot_interfaces.rst

@@ -80,14 +80,14 @@ access.
         // No "preload" loads during scene load exists in C#.
         // No "preload" loads during scene load exists in C#.
 
 
         // Initialize with a value. Editable at runtime.
         // Initialize with a value. Editable at runtime.
-        public Script MyScript = GD.Load<Script>("MyScript.cs");
+        public Script MyScript = GD.Load<Script>("res://Path/To/MyScript.cs");
 
 
         // Initialize with same value. Value cannot be changed.
         // Initialize with same value. Value cannot be changed.
-        public readonly Script MyConstScript = GD.Load<Script>("MyScript.cs");
+        public readonly Script MyConstScript = GD.Load<Script>("res://Path/To/MyScript.cs");
 
 
         // Like 'readonly' due to inaccessible setter.
         // Like 'readonly' due to inaccessible setter.
         // But, value can be set during constructor, i.e. MyType().
         // But, value can be set during constructor, i.e. MyType().
-        public Script Library { get; } = GD.Load<Script>("res://addons/plugin/library.gd");
+        public Script MyNoSetScript { get; } = GD.Load<Script>("res://Path/To/MyScript.cs");
 
 
         // If need a "const [Export]" (which doesn't exist), use a
         // If need a "const [Export]" (which doesn't exist), use a
         // conditional setter for a tool script that checks if it's executing
         // conditional setter for a tool script that checks if it's executing

+ 1 - 1
tutorials/best_practices/logic_preferences.rst

@@ -96,7 +96,7 @@ either? Let's see an example:
         public override void _Ready()
         public override void _Ready()
         {
         {
             // Can assign the value during initialization.
             // Can assign the value during initialization.
-            ABuilding = GD.Load<PackedScene>("res://office.tscn");
+            ABuilding = GD.Load<PackedScene>("res://Office.tscn");
         }
         }
     }
     }
 
 

+ 22 - 13
tutorials/best_practices/scenes_versus_scripts.rst

@@ -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

+ 1 - 1
tutorials/physics/using_character_body_2d.rst

@@ -337,7 +337,7 @@ uses the mouse pointer. Here is the code for the Player, using ``move_and_slide(
 
 
     public partial class MyCharacterBody2D : CharacterBody2D
     public partial class MyCharacterBody2D : CharacterBody2D
     {
     {
-        private PackedScene _bullet = (PackedScene)GD.Load("res://bullet.tscn");
+        private PackedScene _bullet = GD.Load<PackedScene>("res://Bullet.tscn");
         private int _speed = 200;
         private int _speed = 200;
 
 
         public void GetInput()
         public void GetInput()

+ 4 - 5
tutorials/scripting/cross_language_scripting.rst

@@ -74,9 +74,8 @@ with :ref:`new() <class_CSharpScript_method_new>`.
 
 
 .. code-block:: gdscript
 .. code-block:: gdscript
 
 
-    var my_csharp_script = load("res://path_to_cs_file.cs")
+    var my_csharp_script = load("res://Path/To/MyCSharpNode.cs")
     var my_csharp_node = my_csharp_script.new()
     var my_csharp_node = my_csharp_script.new()
-    print(my_csharp_node.str2) # barbar
 
 
 .. warning::
 .. warning::
 
 
@@ -101,8 +100,8 @@ be instantiated with :ref:`GDScript.New() <class_GDScript_method_new>`.
 
 
 .. code-block:: csharp
 .. code-block:: csharp
 
 
-    GDScript MyGDScript = (GDScript)GD.Load("res://path_to_gd_file.gd");
-    GodotObject myGDScriptNode = (GodotObject)MyGDScript.New(); // This is a GodotObject
+    GDScript MyGDScript = GD.Load<GDScript>("res://path/to/my_gd_script.gd");
+    GodotObject myGDScriptNode = (GodotObject)MyGDScript.New(); // This is a GodotObject.
 
 
 Here we are using an :ref:`class_Object`, but you can use type conversion like
 Here we are using an :ref:`class_Object`, but you can use type conversion like
 explained in :ref:`doc_c_sharp_features_type_conversion_and_casting`.
 explained in :ref:`doc_c_sharp_features_type_conversion_and_casting`.
@@ -178,7 +177,7 @@ to said method.
     string[] arr = new string[] { "a", "b", "c" };
     string[] arr = new string[] { "a", "b", "c" };
     myGDScriptNode.Call("print_array", arr); // a, b, c
     myGDScriptNode.Call("print_array", arr); // a, b, c
     myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 }); // 1, 2, 3
     myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 }); // 1, 2, 3
-    // Note how the type of each array entry does not matter as long as it can be handled by the marshaller
+    // Note how the type of each array entry does not matter as long as it can be handled by the marshaller.
 
 
 .. warning::
 .. warning::
 
 

+ 1 - 1
tutorials/scripting/instancing_with_signals.rst

@@ -113,7 +113,7 @@ Here is the code for the player using signals to emit the bullet:
         [Signal]
         [Signal]
         public delegate void ShootEventHandler(PackedScene bullet, float direction, Vector2 location);
         public delegate void ShootEventHandler(PackedScene bullet, float direction, Vector2 location);
 
 
-        private PackedScene _bullet = GD.Load<PackedScene>("res://bullet.tscn");
+        private PackedScene _bullet = GD.Load<PackedScene>("res://Bullet.tscn");
 
 
         public override void _Input(InputEvent @event)
         public override void _Input(InputEvent @event)
         {
         {

+ 3 - 2
tutorials/scripting/resources.rst

@@ -87,7 +87,8 @@ There are two ways to load resources from code. First, you can use the ``load()`
 
 
     public override void _Ready()
     public override void _Ready()
     {
     {
-        var texture = (Texture)GD.Load("res://robi.png"); // Godot loads the Resource when it reads the line.
+        // Godot loads the Resource when it executes this line.
+        var texture = GD.Load<Texture>("res://Robi.png");
         var sprite = GetNode<Sprite2D>("sprite");
         var sprite = GetNode<Sprite2D>("sprite");
         sprite.Texture = texture;
         sprite.Texture = texture;
     }
     }
@@ -128,7 +129,7 @@ To get an instance of the scene, you have to use the
 
 
  .. code-tab:: csharp
  .. code-tab:: csharp
 
 
-    private PackedScene _bulletScene = (PackedScene)GD.Load("res://bullet.tscn");
+    private PackedScene _bulletScene = GD.Load<PackedScene>("res://Bullet.tscn");
 
 
     private void OnShoot()
     private void OnShoot()
     {
     {

+ 3 - 3
tutorials/scripting/singletons_autoload.rst

@@ -191,7 +191,7 @@ current scene and replace it with the requested one.
 
 
 
 
     func _deferred_goto_scene(path):
     func _deferred_goto_scene(path):
-        # It is now safe to remove the current scene
+        # It is now safe to remove the current scene.
         current_scene.free()
         current_scene.free()
 
 
         # Load the new scene.
         # Load the new scene.
@@ -224,11 +224,11 @@ current scene and replace it with the requested one.
 
 
     public void DeferredGotoScene(string path)
     public void DeferredGotoScene(string path)
     {
     {
-        // It is now safe to remove the current scene
+        // It is now safe to remove the current scene.
         CurrentScene.Free();
         CurrentScene.Free();
 
 
         // Load a new scene.
         // Load a new scene.
-        var nextScene = (PackedScene)GD.Load(path);
+        var nextScene = GD.Load<PackedScene>(path);
 
 
         // Instance the new scene.
         // Instance the new scene.
         CurrentScene = nextScene.Instantiate();
         CurrentScene = nextScene.Instantiate();