blob: 8da1eb3c8840a6910219bf0b1de23b888b6deb2b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
<?xml version="1.0" encoding="UTF-8" ?>
<class name="bool" version="4.0">
<brief_description>
Boolean built-in type.
</brief_description>
<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 a 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].
[codeblocks]
[gdscript]
var _can_shoot = true
func shoot():
if _can_shoot:
pass # Perform shooting actions here.
[/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.
[codeblocks]
[gdscript]
var _can_shoot = true
func shoot():
if _can_shoot and Input.is_action_pressed("shoot"):
create_bullet()
[/gdscript]
[csharp]
private bool _canShoot = true;
public void Shoot()
{
if (_canShoot && 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.
[codeblocks]
[gdscript]
var _can_shoot = true
onready var _cool_down = $CoolDownTimer
func shoot():
if _can_shoot and Input.is_action_pressed("shoot"):
create_bullet()
_can_shoot = false
_cool_down.start()
func _on_CoolDownTimer_timeout():
_can_shoot = true
[/gdscript]
[csharp]
private bool _canShoot = true;
private Timer _coolDown;
public override void _Ready()
{
_coolDown = GetNode<Timer>("CoolDownTimer");
}
public void Shoot()
{
if (_canShoot && Input.IsActionPressed("shoot"))
{
CreateBullet();
_canShoot = false;
_coolDown.Start();
}
}
public void OnCoolDownTimerTimeout()
{
_canShoot = true;
}
[/csharp]
[/codeblocks]
</description>
<tutorials>
</tutorials>
<methods>
<method name="bool" qualifiers="constructor">
<return type="bool">
</return>
<description>
Constructs a default-initialized [bool] set to [code]false[/code].
</description>
</method>
<method name="bool" qualifiers="constructor">
<return type="bool">
</return>
<argument index="0" name="from" type="bool">
</argument>
<description>
Constructs a [bool] as a copy of the given [bool].
</description>
</method>
<method name="bool" qualifiers="constructor">
<return type="bool">
</return>
<argument index="0" name="from" type="float">
</argument>
<description>
Cast a [float] value to a boolean value, this method will return [code]false[/code] if [code]0.0[/code] is passed in, and [code]true[/code] for all other floats.
</description>
</method>
<method name="bool" qualifiers="constructor">
<return type="bool">
</return>
<argument index="0" name="from" type="int">
</argument>
<description>
Cast an [int] value to a boolean value, this method will return [code]false[/code] if [code]0[/code] is passed in, and [code]true[/code] for all other ints.
</description>
</method>
<method name="operator !=" qualifiers="operator">
<return type="bool">
</return>
<argument index="0" name="right" type="bool">
</argument>
<description>
</description>
</method>
<method name="operator <" qualifiers="operator">
<return type="bool">
</return>
<argument index="0" name="right" type="bool">
</argument>
<description>
</description>
</method>
<method name="operator ==" qualifiers="operator">
<return type="bool">
</return>
<argument index="0" name="right" type="bool">
</argument>
<description>
</description>
</method>
<method name="operator >" qualifiers="operator">
<return type="bool">
</return>
<argument index="0" name="right" type="bool">
</argument>
<description>
</description>
</method>
</methods>
<constants>
</constants>
</class>
|