-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundLoad.cs
More file actions
105 lines (81 loc) · 2.29 KB
/
BackgroundLoad.cs
File metadata and controls
105 lines (81 loc) · 2.29 KB
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
using Godot;
using System;
using System.Threading;
public class BackgroundLoad : Control
{
private const int OK = 0;
private const int ERR_FILE_EOF = 18;
private const int SIMULATED_DELAY_SEC = 1;
private System.Threading.Thread _thread = null;
private ResourceInteractiveLoader _loader = null;
private ProgressBar _progress = null;
public override void _Ready()
{
_progress = (ProgressBar)GetNode("ProgressBar");
}
public void _ThreadLoad(string path)
{
_loader = ResourceLoader.LoadInteractive(path);
//check for errors
if (_loader == null)
{
GD.Print("ResourceLoader is null and check if the resource path is valid : ", path);
return;
}
var total = _loader.GetStageCount();
GD.Print("total stages : ", total);
_progress.SetMax(total);
PackedScene pScene = null;
//#iterate until we have a resource
//Update progress bar, use call deferred, which routes to main thread
while (true)
{
//Call deferred to set load step
_progress.SetValue(_loader.GetStage());
GD.Print("Value: ", _loader.GetStage());
//Simulate a delay
OS.DelayMsec( SIMULATED_DELAY_SEC * 1000);
var err = (int)_loader.Poll();
GD.Print("Error: ", err);
if (err == ERR_FILE_EOF) //load finished
{
pScene = (PackedScene)_loader.GetResource();
break;
} else if (err != OK) {
GD.Print("ResourceLoader failed on loading resource!!");
break;
}
}
_loader = null;
CallDeferred(nameof(_ThreadDone), pScene);
}
public void _ThreadDone(PackedScene pScene)
{
//check for errors
if (pScene == null)
{
GD.Print("No resource is available.");
return;
}
GD.Print("Thread is done.");
_thread.Join();
_progress.Hide();
Node newScene = pScene.Instance();
GetTree().CurrentScene.Free();
GetTree().CurrentScene = null;
GetTree().Root.AddChild(newScene);
GetTree().CurrentScene = newScene;
_progress.Visible = false;
}
public void GoToScene(string path)
{
CallDeferred(nameof(DeferredGoToScene), path);
}
public void DeferredGoToScene(string path)
{
_thread = new System.Threading.Thread(() => _ThreadLoad(path));
_thread.Start();
Raise(); //show on top
_progress.Visible = true;
}
}