summaryrefslogtreecommitdiff
path: root/doc/classes/bool.xml
diff options
context:
space:
mode:
authorHaSa1002 <johawitt@outlook.de>2020-07-31 16:07:26 +0200
committerHaSa1002 <johawitt@outlook.de>2020-09-26 12:29:55 +0200
commitc5aded55dfb5162b62df839a48142d2755270901 (patch)
tree4dbad1a879a039db51efaf5a73cc15369193f45b /doc/classes/bool.xml
parentfea72f2a7102dc0eec083bf5397330cb7a05de94 (diff)
Add C# code examples to the docs
Only existing GDScript code examples are converted and added to the docs. This is the first batch include classes beginning with A and B. Included classes: * AcceptDialog * AESContext * Animation * AnimationNodeStateMachine * AnimationNodeStateMachinePlayback * AnimationNodeStateMachineTransition * Array * ArrayMesh * AStar * AStar2D * Bool * Button
Diffstat (limited to 'doc/classes/bool.xml')
-rw-r--r--doc/classes/bool.xml83
1 files changed, 67 insertions, 16 deletions
diff --git a/doc/classes/bool.xml b/doc/classes/bool.xml
index 869fc14d40..ce4d000a9b 100644
--- a/doc/classes/bool.xml
+++ b/doc/classes/bool.xml
@@ -6,36 +6,87 @@
<description>
Boolean is a built-in type. There are two boolean values: [code]true[/code] and [code]false[/code]. You can think of it as an switch with on or off (1 or 0) setting. Booleans are used in programming for logic in condition statements, like [code]if[/code] statements.
Booleans can be directly used in [code]if[/code] statements. The code below demonstrates this on the [code]if can_shoot:[/code] line. You don't need to use [code]== true[/code], you only need [code]if can_shoot:[/code]. Similarly, use [code]if not can_shoot:[/code] rather than [code]== false[/code].
- [codeblock]
- var can_shoot = true
+ [codeblocks]
+ [gdscript]
+ var _can_shoot = true
func shoot():
- if can_shoot:
+ if _can_shoot:
pass # Perform shooting actions here.
- [/codeblock]
+ [/gdscript]
+ [csharp]
+ private bool _canShoot = true;
+
+ public void Shoot()
+ {
+ if (_canShoot)
+ {
+ // Perform shooting actions here.
+ }
+ }
+ [/csharp]
+ [/codeblocks]
The following code will only create a bullet if both conditions are met: action "shoot" is pressed and if [code]can_shoot[/code] is [code]true[/code].
[b]Note:[/b] [code]Input.is_action_pressed("shoot")[/code] is also a boolean that is [code]true[/code] when "shoot" is pressed and [code]false[/code] when "shoot" isn't pressed.
- [codeblock]
- var can_shoot = true
+ [codeblocks]
+ [gdscript]
+ var _can_shoot = true
func shoot():
- if can_shoot and Input.is_action_pressed("shoot"):
+ if _can_shoot and Input.is_action_pressed("shoot"):
create_bullet()
- [/codeblock]
+ [/gdscript]
+ [csharp]
+ private bool _canShoot = true;
+
+ public void Shoot()
+ {
+ if (_canShoot &amp;&amp; Input.IsActionPressed("shoot"))
+ {
+ CreateBullet();
+ }
+ }
+ [/csharp]
+ [/codeblocks]
The following code will set [code]can_shoot[/code] to [code]false[/code] and start a timer. This will prevent player from shooting until the timer runs out. Next [code]can_shoot[/code] will be set to [code]true[/code] again allowing player to shoot once again.
- [codeblock]
- var can_shoot = true
- onready var cool_down = $CoolDownTimer
+ [gdscript]
+ var _can_shoot = true
+ onready var _cool_down = $CoolDownTimer
func shoot():
- if can_shoot and Input.is_action_pressed("shoot"):
+ if _can_shoot and Input.is_action_pressed("shoot"):
create_bullet()
- can_shoot = false
- cool_down.start()
+ _can_shoot = false
+ _cool_down.start()
func _on_CoolDownTimer_timeout():
- can_shoot = true
- [/codeblock]
+ _can_shoot = true
+ [/gdscript]
+ [csharp]
+ private bool _canShoot = true;
+ private Timer _coolDown;
+
+ public override void _Ready()
+ {
+ _coolDown = GetNode&lt;Timer&gt;("CoolDownTimer");
+ }
+
+ public void Shoot()
+ {
+ if (_canShoot &amp;&amp; Input.IsActionPressed("shoot"))
+ {
+ CreateBullet();
+ _canShoot = false;
+ _coolDown.Start();
+ }
+ }
+
+ public void OnCoolDownTimerTimeout()
+ {
+ _canShoot = true;
+ }
+ [/csharp]
+ [/codeblocks]
</description>
<tutorials>
</tutorials>