|
@@ -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() <class_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.
|