From f04f88e7619cd63548dca4ddf96a6e32f7d5caf2 Mon Sep 17 00:00:00 2001 From: Max Hilbrunner Date: Sat, 21 Apr 2018 17:30:51 +0200 Subject: [PATCH] Merge pull request #1380 from KellyThomas/resources add c sharp samples to step_by_step/resources --- getting_started/step_by_step/resources.rst | 32 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/getting_started/step_by_step/resources.rst b/getting_started/step_by_step/resources.rst index 4402b6cb7..c72f95ccd 100644 --- a/getting_started/step_by_step/resources.rst +++ b/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 first is to use load(), like this: -:: +.. tabs:: + .. code-tab:: gdscript GDScript func _ready(): var res = load("res://robi.png") # resource is loaded when line is executed 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 parameter, because it loads the resource at compile-time. -:: +.. tabs:: + .. code-tab:: gdscript GDScript func _ready(): var res = preload("res://robi.png") # resource is loaded at compile time get_node("sprite").texture = res + .. code-tab:: csharp + + // preload() is unavailable in C Sharp + Loading scenes -------------- 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() ` must be used. -:: +.. tabs:: + .. code-tab:: gdscript GDScript func _on_shoot(): var bullet = preload("res://bullet.tscn").instance() 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 them (sets all the properties) and returns the root node of the scene, which can be added to any other node.