Browse Source

Merge pull request #1380 from KellyThomas/resources

add c sharp samples to step_by_step/resources
Max Hilbrunner 7 years ago
parent
commit
4abdf55ce5
1 changed files with 29 additions and 3 deletions
  1. 29 3
      getting_started/step_by_step/resources.rst

+ 29 - 3
getting_started/step_by_step/resources.rst

@@ -82,21 +82,36 @@ Loading resources from code
 Loading resources from code is easy. There are two ways to do it. The
 Loading resources from code is easy. There are two ways to do it. The
 first is to use load(), like this:
 first is to use load(), like this:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     func _ready():
     func _ready():
             var res = load("res://robi.png") # resource is loaded when line is executed
             var res = load("res://robi.png") # resource is loaded when line is executed
             get_node("sprite").texture = res
             get_node("sprite").texture = res
 
 
+ .. code-tab:: csharp
+
+    public override void _Ready()
+    {
+        var texture = (Texture)GD.Load("res://robi.png"); // resource is loaded when line is executed
+        var sprite = (Sprite)GetNode("sprite");
+        sprite.Texture = texture;
+    }
+
 The second way is more optimal, but only works with a string constant
 The second way is more optimal, but only works with a string constant
 parameter, because it loads the resource at compile-time.
 parameter, because it loads the resource at compile-time.
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     func _ready():
     func _ready():
             var res = preload("res://robi.png") # resource is loaded at compile time
             var res = preload("res://robi.png") # resource is loaded at compile time
             get_node("sprite").texture = res
             get_node("sprite").texture = res
 
 
+ .. code-tab:: csharp
+
+    // preload() is unavailable in C Sharp
+
 Loading scenes
 Loading scenes
 --------------
 --------------
 Scenes are also resources, but there is a catch. Scenes saved to disk 
 Scenes are also resources, but there is a catch. Scenes saved to disk 
@@ -107,12 +122,23 @@ To obtain an instance of the scene, the method
 :ref:`PackedScene.instance() <class_PackedScene_instance>`
 :ref:`PackedScene.instance() <class_PackedScene_instance>`
 must be used.
 must be used.
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     func _on_shoot():
     func _on_shoot():
             var bullet = preload("res://bullet.tscn").instance()
             var bullet = preload("res://bullet.tscn").instance()
             add_child(bullet)                  
             add_child(bullet)                  
 
 
+ .. code-tab:: csharp
+
+    private PackedScene _bulletScene = (PackedScene)GD.Load("res://bullet.tscn");
+
+    public void OnShoot()
+    {
+        Node bullet = _bulletScene.Instance();
+        AddChild(bullet);
+    }
+
 This method creates the nodes in the scene's hierarchy, configures 
 This method creates the nodes in the scene's hierarchy, configures 
 them (sets all the properties) and returns the root node of the scene, 
 them (sets all the properties) and returns the root node of the scene, 
 which can be added to any other node.
 which can be added to any other node.