5
5
6
6
namespace Tests . Manual
7
7
{
8
- public partial class Test : Node2D
8
+ public partial class Test : Node
9
9
{
10
10
[ Export ]
11
11
private bool runTestOnReady ;
12
12
[ Export ]
13
13
private NodePath spritePath ;
14
+ [ Export ]
15
+ private Label pauseLabel ;
14
16
public Sprite2D sprite ;
15
17
16
18
public override void _Ready ( )
17
19
{
18
20
sprite = GetNode < Sprite2D > ( spritePath ) ;
19
21
if ( runTestOnReady )
20
22
Run ( ) . Forget ( ) ;
23
+ ProcessMode = ProcessModeEnum . Always ;
24
+ pauseLabel . Text = GetTree ( ) . Paused ? "Paused" : "Unpaused" ;
21
25
}
22
26
23
27
public override void _Input ( InputEvent @event )
24
28
{
25
- if ( @event . IsActionReleased ( "ui_select " ) )
29
+ if ( @event . IsActionReleased ( "ui_left " ) )
26
30
{
27
31
Run ( ) . Forget ( ) ;
28
32
}
33
+ else if ( @event . IsActionReleased ( "ui_right" ) )
34
+ {
35
+ RunPause ( ) . Forget ( ) ;
36
+ }
37
+ else if ( @event . IsActionReleased ( "ui_up" ) )
38
+ {
39
+ GetTree ( ) . Paused = ! GetTree ( ) . Paused ;
40
+ pauseLabel . Text = GetTree ( ) . Paused ? "Paused" : "Unpaused" ;
41
+ }
29
42
}
30
43
31
44
private async GDTaskVoid Run ( )
32
45
{
33
- GD . Print ( "Pre delay" ) ;
46
+ GD . Print ( "Run: Pre delay" ) ;
34
47
sprite . Visible = false ;
35
48
await GDTask . Delay ( TimeSpan . FromSeconds ( 3 ) ) ;
36
49
sprite . Visible = true ;
37
- GD . Print ( "Post delay after 3 seconds" ) ;
50
+ GD . Print ( "Run: Post delay after 3 seconds" ) ;
38
51
39
- GD . Print ( "Pre RunWithResult" ) ;
52
+ GD . Print ( "Run: Pre RunWithResult" ) ;
40
53
string result = await RunWithResult ( ) ;
41
- GD . Print ( $ "Post got result: { result } ") ;
54
+ GD . Print ( $ "Run: Post got result: { result } ") ;
42
55
43
- GD . Print ( "LongTask started" ) ;
56
+ GD . Print ( "Run: LongTask started" ) ;
44
57
var cts = new CancellationTokenSource ( ) ;
45
58
46
59
CancellableReallyLongTask ( cts . Token ) . Forget ( ) ;
47
60
48
61
await GDTask . Delay ( TimeSpan . FromSeconds ( 3 ) ) ;
49
62
cts . Cancel ( ) ;
50
- GD . Print ( "LongTask cancelled" ) ;
63
+ GD . Print ( "Run: LongTask cancelled" ) ;
51
64
52
65
await GDTask . WaitForEndOfFrame ( ) ;
53
- GD . Print ( "WaitForEndOfFrame" ) ;
66
+ GD . Print ( "Run: WaitForEndOfFrame" ) ;
54
67
await GDTask . WaitForPhysicsProcess ( ) ;
55
- GD . Print ( "WaitForPhysicsProcess" ) ;
68
+ GD . Print ( "Run: WaitForPhysicsProcess" ) ;
56
69
await GDTask . NextFrame ( ) ;
57
- GD . Print ( "NextFrame" ) ;
70
+ GD . Print ( "Run: NextFrame" ) ;
58
71
}
59
72
60
73
private async GDTask < string > RunWithResult ( )
@@ -66,13 +79,60 @@ private async GDTask<string> RunWithResult()
66
79
private async GDTaskVoid CancellableReallyLongTask ( CancellationToken cancellationToken )
67
80
{
68
81
int seconds = 10 ;
69
- GD . Print ( $ "Starting long task ({ seconds } seconds long).") ;
82
+ GD . Print ( $ "Run: Starting long task ({ seconds } seconds long).") ;
70
83
for ( int i = 0 ; i < seconds ; i ++ )
71
84
{
72
- GD . Print ( $ "Working on long task for { i } seconds...") ;
85
+ GD . Print ( $ "Run: Working on long task for { i } seconds...") ;
73
86
await GDTask . Delay ( TimeSpan . FromSeconds ( 1 ) , cancellationToken : cancellationToken ) ;
74
87
}
75
- GD . Print ( "Finished long task." ) ;
88
+ GD . Print ( "Run: Finished long task." ) ;
89
+ }
90
+
91
+ private async GDTaskVoid RunPause ( )
92
+ {
93
+ GD . Print ( "RunPause: Pre delay" ) ;
94
+ sprite . Visible = false ;
95
+ await GDTask . Delay ( TimeSpan . FromSeconds ( 3 ) , PlayerLoopTiming . PauseProcess ) ;
96
+ sprite . Visible = true ;
97
+ GD . Print ( "RunPause: Post delay after 3 seconds" ) ;
98
+
99
+ GD . Print ( "RunPause: Pre RunWithResult" ) ;
100
+ string result = await RunWithResultPause ( ) ;
101
+ GD . Print ( $ "RunPause: Post got result: { result } ") ;
102
+
103
+ GD . Print ( "RunPause: LongTask started" ) ;
104
+ var cts = new CancellationTokenSource ( ) ;
105
+
106
+ CancellableReallyLongTaskPause ( cts . Token ) . Forget ( ) ;
107
+
108
+ await GDTask . Delay ( TimeSpan . FromSeconds ( 3 ) , PlayerLoopTiming . PauseProcess ) ;
109
+ cts . Cancel ( ) ;
110
+ GD . Print ( "RunPause: LongTask cancelled" ) ;
111
+
112
+ await GDTask . Yield ( PlayerLoopTiming . PauseProcess ) ;
113
+ GD . Print ( "RunPause: Yield(PlayerLoopTiming.PauseProcess)" ) ;
114
+ await GDTask . Yield ( PlayerLoopTiming . PausePhysicsProcess ) ;
115
+ GD . Print ( "RunPause: Yield(PlayerLoopTiming.PausePhysicsProcess)" ) ;
116
+ await GDTask . NextFrame ( PlayerLoopTiming . PauseProcess ) ;
117
+ GD . Print ( "RunPause: NextFrame" ) ;
118
+ }
119
+
120
+ private async GDTask < string > RunWithResultPause ( )
121
+ {
122
+ await GDTask . Delay ( TimeSpan . FromSeconds ( 2 ) , PlayerLoopTiming . PauseProcess ) ;
123
+ return "Hello" ;
124
+ }
125
+
126
+ private async GDTaskVoid CancellableReallyLongTaskPause ( CancellationToken cancellationToken )
127
+ {
128
+ int seconds = 10 ;
129
+ GD . Print ( $ "RunPause: Starting long task ({ seconds } seconds long).") ;
130
+ for ( int i = 0 ; i < seconds ; i ++ )
131
+ {
132
+ GD . Print ( $ "RunPause: Working on long task for { i } seconds...") ;
133
+ await GDTask . Delay ( TimeSpan . FromSeconds ( 1 ) , PlayerLoopTiming . PauseProcess , cancellationToken ) ;
134
+ }
135
+ GD . Print ( "RunPause: Finished long task." ) ;
76
136
}
77
137
}
78
138
}
0 commit comments