Make more snippets in GDScript basics page follow the styleguide (#3487)

This commit is contained in:
Michael Alexsander
2020-05-03 10:22:42 -03:00
committed by GitHub
parent f7ca54c021
commit e4f6430262

View File

@@ -987,7 +987,6 @@ will then appear with its new icon in the editor::
# Item.gd
extends Node
class_name Item, "res://interface/icons/item.png"
.. image:: img/class_name_editor_register_example.png
@@ -1000,11 +999,14 @@ Here's a class file example:
class_name Character
var health = 5
func print_health():
print(health)
func print_this_script_three_times():
print(get_script())
print(ResourceLoader.load("res://character.gd"))
@@ -1093,9 +1095,11 @@ This is better explained through examples. Consider this scenario::
var entity = null
var message = null
func _init(e=null):
entity = e
func enter(m):
message = m
@@ -1103,6 +1107,7 @@ This is better explained through examples. Consider this scenario::
# Idle.gd (inheriting class)
extends "State.gd"
func _init(e=null, m=null).(e):
# Do something with 'e'.
message = m
@@ -1138,9 +1143,12 @@ function.
# An inner class in this class file.
class SomeInnerClass:
var a = 5
func print_value_of_a():
print(a)
# This is the constructor of the class file's main class.
func _init():
var c = SomeInnerClass.new()
@@ -1162,6 +1170,7 @@ class resource is done by calling the ``new`` function on the class object::
# Preload the class only once at compile time.
const MyClass = preload("myclass.gd")
func _init():
var a = MyClass.new()
a.some_function()
@@ -1194,9 +1203,11 @@ with the new value. Vice versa, when ``variable`` is accessed, the *getter* func
var my_var setget my_var_set, my_var_get
func my_var_set(new_value):
my_var = new_value
func my_var_get():
return my_var # Getter must return a value.
@@ -1238,6 +1249,7 @@ placed at the top of the file::
tool
extends Button
func _ready():
print("Hello")
@@ -1276,6 +1288,7 @@ to. To create custom signals for a class, use the ``signal`` keyword.
extends Node
# A signal named health_depleted.
signal health_depleted
@@ -1301,6 +1314,7 @@ signal, the game node's ``_on_Character_health_depleted`` is called::
var character_node = get_node('Character')
character_node.connect("health_depleted", self, "_on_Character_health_depleted")
func _on_Character_health_depleted():
get_tree().reload_current_scene()
@@ -1320,6 +1334,7 @@ the :ref:`Object.connect() <class_Object_method_connect>` method::
...
signal health_changed
func take_damage(amount):
var old_health = health
health -= amount
@@ -1433,6 +1448,7 @@ an example::
yield()
print("world")
func _ready():
var y = my_func()
# Function state saved in 'y'.
@@ -1454,6 +1470,7 @@ for example::
print(yield())
return "cheers!"
func _ready():
var y = my_func()
# Function state saved in 'y'.
@@ -1473,6 +1490,7 @@ Remember to save the new function state, when using multiple ``yield``\s::
print("Turn %d" % i)
yield();
func _ready():
var co = co_func();
while co is GDScriptFunction && co.is_valid():
@@ -1502,6 +1520,7 @@ into an invalid state, for example::
yield(button_func(), "completed")
print("All buttons were pressed, hurray!")
func button_func():
yield($Button0, "pressed")
yield($Button1, "pressed")
@@ -1512,7 +1531,7 @@ You can also get the signal's argument once it's emitted by an object:
::
# Wait for when any node is added to the scene tree
# Wait for when any node is added to the scene tree.
var node = yield(get_tree(), "node_added")
If you're unsure whether a function may yield or not, or whether it may yield
@@ -1528,10 +1547,11 @@ multiple times, you can yield to the ``completed`` signal conditionally:
return result
func make():
var result = generate()
if result is GDScriptFunctionState: # still working
if result is GDScriptFunctionState: # Still working.
result = yield(result, "completed")
return result
@@ -1553,6 +1573,7 @@ be obtained when a call to ``Node._ready()`` is made.
var my_label
func _ready():
my_label = get_node("MyLabel")