Skip to content

Commit 659e2d1

Browse files
committed
Add a PCK loading/unloading demo
1 parent ae57eb2 commit 659e2d1

29 files changed

+612
-0
lines changed

loading/pck_loading/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# PCK loading demo
2+
3+
This demo shows how you can load and unload exported PCK files at runtime.
4+
5+
Language: GDScript
6+
7+
Renderer: GLES 2
8+
9+
## Sub-projects
10+
11+
There are three Godot projects under `sub_projects`. All have been exported to their respective PCK files and are loaded by the main project.
12+
13+
## Screenshots
14+
15+
![Screenshot](screenshots/screenshot.png)

loading/pck_loading/Root.gd

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
extends Control
2+
3+
@onready var vp_orig = $HBoxContainer/VBoxContainer/SubViewportContainer/ViewOrig
4+
@onready var vp_a = $HBoxContainer/VBoxContainer2/SubViewportContainer/ViewA
5+
@onready var vp_b = $HBoxContainer/VBoxContainer3/SubViewportContainer/ViewB
6+
@onready var no_scene_lbl = $NoNodeLbl
7+
8+
@export_file("*.pck") var pck_1
9+
@export_file("*.pck") var pck_2
10+
@export_file("*.pck") var pck_3
11+
12+
13+
func _ready():
14+
reload_scene()
15+
16+
17+
func reload_scene():
18+
$LineEdit.text = ""
19+
populate_file_list("res://")
20+
21+
load_scene_at_node("res://orig.tscn", vp_orig)
22+
load_scene_at_node("res://a.tscn", vp_a)
23+
load_scene_at_node("res://b.tscn", vp_b)
24+
25+
26+
func populate_file_list(path: String):
27+
var dir = Directory.new()
28+
dir.open(path)
29+
dir.list_dir_begin()
30+
var file_name = dir.get_next()
31+
while file_name != "":
32+
if dir.current_is_dir():
33+
populate_file_list(path + file_name + "/")
34+
else:
35+
$LineEdit.text += path + file_name + '\n'
36+
file_name = dir.get_next()
37+
38+
39+
func load_scene_at_node(scene_name: String, node: Node):
40+
var child
41+
if ResourceLoader.exists(scene_name):
42+
child = load(scene_name).instantiate()
43+
else:
44+
child = no_scene_lbl.duplicate()
45+
child.position = Vector2.ZERO
46+
if node.get_child_count() > 0:
47+
node.remove_child(node.get_child(0))
48+
node.add_child(child)
49+
50+
51+
func _on_load1_pressed():
52+
ProjectSettings.load_resource_pack(pck_1, $VBoxContainer/HBoxContainer/CheckBox.is_pressed())
53+
reload_scene()
54+
55+
56+
func _on_unload1_pressed():
57+
ProjectSettings.unload_resource_pack(pck_1)
58+
reload_scene()
59+
60+
61+
func _on_load2_pressed():
62+
ProjectSettings.load_resource_pack(pck_2, $VBoxContainer/HBoxContainer2/CheckBox.is_pressed())
63+
reload_scene()
64+
65+
66+
func _on_unload2_pressed():
67+
ProjectSettings.unload_resource_pack(pck_2)
68+
reload_scene()
69+
70+
71+
func _on_load3_pressed():
72+
ProjectSettings.load_resource_pack(pck_3, $VBoxContainer/HBoxContainer3/CheckBox.is_pressed())
73+
reload_scene()
74+
75+
76+
func _on_unload3_pressed():
77+
ProjectSettings.unload_resource_pack(pck_3)
78+
reload_scene()

loading/pck_loading/icon.png

3.35 KB
Loading

loading/pck_loading/icon.png.import

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://btpbmejdh00qx"
6+
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://icon.png"
14+
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/lossy_quality=0.7
20+
compress/hdr_compression=1
21+
compress/bptc_ldr=0
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

loading/pck_loading/main.tscn

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://biqc8303kctlu"]
2+
3+
[ext_resource type="Script" path="res://Root.gd" id="1_dd1mq"]
4+
5+
[node name="Root" type="Control"]
6+
script = ExtResource( "1_dd1mq" )
7+
pck_1 = "res://sub_projects/1.pck"
8+
pck_2 = "res://sub_projects/2.pck"
9+
pck_3 = "res://sub_projects/3.pck"
10+
11+
[node name="LineEdit" type="Label" parent="."]
12+
offset_left = 67.0
13+
offset_top = 75.0
14+
offset_right = 463.0
15+
offset_bottom = 375.0
16+
autowrap_mode = 1
17+
clip_text = true
18+
19+
[node name="VBoxContainer" type="VBoxContainer" parent="."]
20+
offset_left = 552.0
21+
offset_top = 81.0
22+
offset_right = 932.0
23+
offset_bottom = 379.0
24+
theme_override_constants/separation = 20
25+
26+
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
27+
offset_right = 380.0
28+
offset_bottom = 86.0
29+
size_flags_vertical = 3
30+
theme_override_constants/separation = 20
31+
32+
[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer"]
33+
offset_right = 125.0
34+
offset_bottom = 86.0
35+
size_flags_horizontal = 3
36+
text = "Load 1.pck"
37+
38+
[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer"]
39+
offset_left = 145.0
40+
offset_right = 271.0
41+
offset_bottom = 86.0
42+
size_flags_horizontal = 3
43+
text = "Unload 1.pck"
44+
45+
[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer"]
46+
offset_left = 291.0
47+
offset_right = 380.0
48+
offset_bottom = 86.0
49+
text = "Replace
50+
files?"
51+
52+
[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer"]
53+
offset_top = 106.0
54+
offset_right = 380.0
55+
offset_bottom = 192.0
56+
size_flags_vertical = 3
57+
theme_override_constants/separation = 20
58+
59+
[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer2"]
60+
offset_right = 125.0
61+
offset_bottom = 86.0
62+
size_flags_horizontal = 3
63+
text = "Load 2.pck"
64+
65+
[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer2"]
66+
offset_left = 145.0
67+
offset_right = 271.0
68+
offset_bottom = 86.0
69+
size_flags_horizontal = 3
70+
text = "Unload 2.pck"
71+
72+
[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer2"]
73+
offset_left = 291.0
74+
offset_right = 380.0
75+
offset_bottom = 86.0
76+
text = "Replace
77+
files?"
78+
79+
[node name="HBoxContainer3" type="HBoxContainer" parent="VBoxContainer"]
80+
offset_top = 212.0
81+
offset_right = 380.0
82+
offset_bottom = 298.0
83+
size_flags_vertical = 3
84+
theme_override_constants/separation = 20
85+
86+
[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer3"]
87+
offset_right = 125.0
88+
offset_bottom = 86.0
89+
size_flags_horizontal = 3
90+
text = "Load 3.pck"
91+
92+
[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer3"]
93+
offset_left = 145.0
94+
offset_right = 271.0
95+
offset_bottom = 86.0
96+
size_flags_horizontal = 3
97+
text = "Unload 3.pck"
98+
99+
[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer3"]
100+
offset_left = 291.0
101+
offset_right = 380.0
102+
offset_bottom = 86.0
103+
text = "Replace
104+
files?"
105+
106+
[node name="HBoxContainer" type="HBoxContainer" parent="."]
107+
offset_left = 44.0
108+
offset_top = 415.0
109+
offset_right = 934.0
110+
offset_bottom = 569.0
111+
size_flags_horizontal = 3
112+
113+
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"]
114+
offset_right = 294.0
115+
offset_bottom = 158.0
116+
size_flags_horizontal = 3
117+
118+
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer"]
119+
offset_right = 294.0
120+
offset_bottom = 26.0
121+
text = "orig.tscn"
122+
123+
[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer"]
124+
minimum_size = Vector2(256, 0)
125+
offset_top = 30.0
126+
offset_right = 294.0
127+
offset_bottom = 158.0
128+
size_flags_vertical = 3
129+
130+
[node name="ViewOrig" type="SubViewport" parent="HBoxContainer/VBoxContainer/SubViewportContainer"]
131+
handle_input_locally = false
132+
size = Vector2i(256, 128)
133+
render_target_update_mode = 4
134+
135+
[node name="VBoxContainer2" type="VBoxContainer" parent="HBoxContainer"]
136+
offset_left = 298.0
137+
offset_right = 592.0
138+
offset_bottom = 158.0
139+
size_flags_horizontal = 3
140+
141+
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer2"]
142+
offset_right = 294.0
143+
offset_bottom = 26.0
144+
text = "a.tscn"
145+
146+
[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer2"]
147+
minimum_size = Vector2(256, 0)
148+
offset_top = 30.0
149+
offset_right = 294.0
150+
offset_bottom = 158.0
151+
size_flags_vertical = 3
152+
153+
[node name="ViewA" type="SubViewport" parent="HBoxContainer/VBoxContainer2/SubViewportContainer"]
154+
handle_input_locally = false
155+
size = Vector2i(256, 128)
156+
render_target_update_mode = 4
157+
158+
[node name="VBoxContainer3" type="VBoxContainer" parent="HBoxContainer"]
159+
offset_left = 596.0
160+
offset_right = 890.0
161+
offset_bottom = 158.0
162+
size_flags_horizontal = 3
163+
164+
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer3"]
165+
offset_right = 294.0
166+
offset_bottom = 26.0
167+
text = "b.tscn"
168+
169+
[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer3"]
170+
minimum_size = Vector2(256, 0)
171+
offset_top = 30.0
172+
offset_right = 294.0
173+
offset_bottom = 158.0
174+
size_flags_vertical = 3
175+
176+
[node name="ViewB" type="SubViewport" parent="HBoxContainer/VBoxContainer3/SubViewportContainer"]
177+
handle_input_locally = false
178+
size = Vector2i(256, 128)
179+
render_target_update_mode = 4
180+
181+
[node name="NoNodeLbl" type="Label" parent="."]
182+
offset_left = -184.0
183+
offset_top = -133.0
184+
offset_right = -144.0
185+
offset_bottom = -110.0
186+
text = "Missing scene file!"
187+
188+
[node name="Label" type="Label" parent="."]
189+
offset_left = 65.0
190+
offset_top = 49.0
191+
offset_right = 128.0
192+
offset_bottom = 75.0
193+
text = "File List:"
194+
195+
[connection signal="pressed" from="VBoxContainer/HBoxContainer/Load" to="." method="_on_load1_pressed"]
196+
[connection signal="pressed" from="VBoxContainer/HBoxContainer/Unload" to="." method="_on_unload1_pressed"]
197+
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Load" to="." method="_on_load2_pressed"]
198+
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Unload" to="." method="_on_unload2_pressed"]
199+
[connection signal="pressed" from="VBoxContainer/HBoxContainer3/Load" to="." method="_on_load3_pressed"]
200+
[connection signal="pressed" from="VBoxContainer/HBoxContainer3/Unload" to="." method="_on_unload3_pressed"]

loading/pck_loading/orig.tscn

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[gd_scene format=3 uid="uid://dr3ofl6lrt7v2"]
2+
3+
[node name="Control" type="Control"]
4+
anchor_right = 1.0
5+
anchor_bottom = 1.0
6+
7+
[node name="Label" type="Label" parent="."]
8+
offset_right = 306.0
9+
offset_bottom = 104.0
10+
text = "Greetings from original data.pck
11+
file! This should always be here;
12+
if somehow this scene fails to
13+
load, something messed up
14+
`res://` lookup!"

loading/pck_loading/project.godot

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=5
10+
11+
[application]
12+
13+
config/name="PCK loading"
14+
config/description="This demo shows how you can load and unload exported PCK files at runtime."
15+
run/main_scene="res://main.tscn"
16+
config/icon="res://icon.png"
17+
config/features=PackedStringArray("4.0")
18+
19+
[rendering]
20+
21+
quality/driver/driver_name="GLES2"
22+
vram_compression/import_etc=true
23+
vram_compression/import_etc2=false

loading/pck_loading/screenshots/.gdignore

Whitespace-only changes.
71.6 KB
Loading

loading/pck_loading/sub_projects/.gdignore

Whitespace-only changes.
8.03 KB
Binary file not shown.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[gd_scene load_steps=3 format=3 uid="uid://cynxmx85sit6h"]
2+
3+
[ext_resource type="Texture2D" uid="uid://deyknasv78sq2" path="res://icon.png" id="1_o57oj"]
4+
[ext_resource type="Script" path="res://script1.gd" id="2_u8kao"]
5+
6+
[node name="Node2D" type="Node2D"]
7+
8+
[node name="Label" type="Label" parent="."]
9+
offset_right = 40.0
10+
offset_bottom = 23.0
11+
text = "Greetings from 1.pck!"
12+
13+
[node name="Sprite2D" type="Sprite2D" parent="."]
14+
position = Vector2(23, 41)
15+
scale = Vector2(0.5, 0.5)
16+
texture = ExtResource( "1_o57oj" )
17+
script = ExtResource( "2_u8kao" )
3.35 KB
Loading

0 commit comments

Comments
 (0)