浏览代码

GDScript Advanced: Improve custom iterator example

Fix syntax by removing multiple variable declarations on the same line, which GDScript does not (yet?) support, use clearer class and variable names, and rename the misleading "is_done()" function to "should_continue()", as the value returned (and returned by _iter_init() and iter_next() actually signifies the opposite: true means continue, false means stop. Also remove the superfluous do_step() function. 

Closes #2433.

Co-authored-by: a e <[email protected]>
Max Hilbrunner 6 年之前
父节点
当前提交
aed79c2583
共有 1 个文件被更改,包括 16 次插入16 次删除
  1. 16 16
      getting_started/scripting/gdscript/gdscript_advanced.rst

+ 16 - 16
getting_started/scripting/gdscript/gdscript_advanced.rst

@@ -425,37 +425,37 @@ functions in your script. An example implementation of a forward iterator follow
 
 ::
 
-    class FwdIterator:
-        var start, curr, end, increment
+    class ForwardIterator:
+        var start
+        var current
+        var end
+        var increment
 
-        func _init(start, stop, inc):
+        func _init(start, stop, increment):
             self.start = start
-            self.curr = start
+            self.current = start
             self.end = stop
-            self.increment = inc
+            self.increment = increment
 
-        func is_done():
-            return (curr < end)
-
-        func do_step():
-            curr += increment
-            return is_done()
+        func should_continue():
+            return (current < end)
 
         func _iter_init(arg):
-            curr = start
-            return is_done()
+            current = start
+            return should_continue()
 
         func _iter_next(arg):
-            return do_step()
+            current += increment
+            return should_continue()
 
         func _iter_get(arg):
-            return curr
+            return current
 
 And it can be used like any other iterator:
 
 ::
 
-    var itr = FwdIterator.new(0, 6, 2)
+    var itr = ForwardIterator.new(0, 6, 2)
     for i in itr:
         print(i) # Will print 0, 2, and 4