浏览代码

Moved references to the global scope (#3213)

Fixes https://github.com/godotengine/godot/issues/34638
Meru Patel 5 年之前
父节点
当前提交
f5a8ec7556
共有 1 个文件被更改,包括 16 次插入6 次删除
  1. 16 6
      tutorials/optimization/using_servers.rst

+ 16 - 6
tutorials/optimization/using_servers.rst

@@ -90,7 +90,10 @@ This is a simple example of how to create a sprite from code and move it using t
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
 
 
     extends Node2D
     extends Node2D
-
+    
+    # VisualServer expects references to be kept around
+    var sprite
+    
     func _ready():
     func _ready():
         # Create a canvas item, child of this node.
         # Create a canvas item, child of this node.
         var ci_rid = VisualServer.canvas_item_create()
         var ci_rid = VisualServer.canvas_item_create()
@@ -98,7 +101,7 @@ This is a simple example of how to create a sprite from code and move it using t
         VisualServer.canvas_item_set_parent(ci_rid, get_canvas_item())
         VisualServer.canvas_item_set_parent(ci_rid, get_canvas_item())
         # Draw a sprite on it.
         # Draw a sprite on it.
         # Remember, keep this reference.
         # Remember, keep this reference.
-        var sprite = load("res://mysprite.png")
+        sprite = load("res://mysprite.png")
         # Add it, centered.
         # Add it, centered.
         VisualServer.canvas_item_add_texture_rect(ci_rid, Rect2(sprite.get_size() / 2, sprite.get_size()), sprite)
         VisualServer.canvas_item_add_texture_rect(ci_rid, Rect2(sprite.get_size() / 2, sprite.get_size()), sprite)
         # Add the item, rotated 45 degrees and translated.
         # Add the item, rotated 45 degrees and translated.
@@ -126,7 +129,10 @@ The 3D APIs are different than the 2D ones, so the instantiation API must be use
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
 
 
     extends Spatial
     extends Spatial
-
+    
+    # VisualServer expects references to be kept around
+    var mesh
+    
     func _ready():
     func _ready():
         # Create a visual instance (for 3D).
         # Create a visual instance (for 3D).
         var instance = VisualServer.instance_create()
         var instance = VisualServer.instance_create()
@@ -136,7 +142,7 @@ The 3D APIs are different than the 2D ones, so the instantiation API must be use
         VisualServer.instance_set_scenario(instance, scenario)
         VisualServer.instance_set_scenario(instance, scenario)
         # Add a mesh to it.
         # Add a mesh to it.
         # Remember, keep the reference.
         # Remember, keep the reference.
-        var mesh = load("res://mymesh.obj")
+        mesh = load("res://mymesh.obj")
         VisualServer.instance_set_base(instance, mesh)
         VisualServer.instance_set_base(instance, mesh)
         # Move the mesh around.
         # Move the mesh around.
         var xform = Transform(Basis(), Vector3(20, 100, 0))
         var xform = Transform(Basis(), Vector3(20, 100, 0))
@@ -150,6 +156,10 @@ and moves a :ref:`CanvasItem <class_CanvasItem>` when the body moves.
 
 
 .. tabs::
 .. tabs::
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
+    
+    # Physics2DServer expects references to be kept around
+    var body
+    var shape
 
 
     func _body_moved(state, index):
     func _body_moved(state, index):
         # Created your own canvas item, use it here.
         # Created your own canvas item, use it here.
@@ -157,10 +167,10 @@ and moves a :ref:`CanvasItem <class_CanvasItem>` when the body moves.
 
 
     func _ready():
     func _ready():
         # Create the body.
         # Create the body.
-        var body = Physics2DServer.body_create()
+        body = Physics2DServer.body_create()
         Physics2DServer.body_set_mode(body, Physics2DServer.BODY_MODE_RIGID)
         Physics2DServer.body_set_mode(body, Physics2DServer.BODY_MODE_RIGID)
         # Add a shape.
         # Add a shape.
-        var shape = RectangleShape2D.new()
+        shape = RectangleShape2D.new()
         shape.extents = Vector2(10, 10)
         shape.extents = Vector2(10, 10)
         # Make sure to keep the shape reference!
         # Make sure to keep the shape reference!
         Physics2DServer.body_add_shape(body, shape)
         Physics2DServer.body_add_shape(body, shape)