Skip to content

Commit 04071f6

Browse files
committed
Add a PCK loading/unloading demo
1 parent ae57eb2 commit 04071f6

29 files changed

+589
-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

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 = DirAccess.open(path)
28+
dir.list_dir_begin()
29+
var file_name = dir.get_next()
30+
while file_name != "":
31+
if dir.current_is_dir():
32+
populate_file_list(path + file_name + "/")
33+
else:
34+
$LineEdit.text += path + file_name + '\n'
35+
file_name = dir.get_next()
36+
37+
38+
func load_scene_at_node(scene_name: String, node: Node):
39+
var child
40+
if ResourceLoader.exists(scene_name):
41+
child = load(scene_name).instantiate()
42+
else:
43+
child = no_scene_lbl.duplicate()
44+
child.position = Vector2.ZERO
45+
if node.get_child_count() > 0:
46+
node.remove_child(node.get_child(0))
47+
node.add_child(child)
48+
49+
50+
func _on_load1_pressed():
51+
ProjectSettings.load_resource_pack(pck_1, $VBoxContainer/HBoxContainer/CheckBox.is_pressed())
52+
reload_scene()
53+
54+
55+
func _on_unload1_pressed():
56+
ProjectSettings.unload_resource_pack(pck_1)
57+
reload_scene()
58+
59+
60+
func _on_load2_pressed():
61+
ProjectSettings.load_resource_pack(pck_2, $VBoxContainer/HBoxContainer2/CheckBox.is_pressed())
62+
reload_scene()
63+
64+
65+
func _on_unload2_pressed():
66+
ProjectSettings.unload_resource_pack(pck_2)
67+
reload_scene()
68+
69+
70+
func _on_load3_pressed():
71+
ProjectSettings.load_resource_pack(pck_3, $VBoxContainer/HBoxContainer3/CheckBox.is_pressed())
72+
reload_scene()
73+
74+
75+
func _on_unload3_pressed():
76+
ProjectSettings.unload_resource_pack(pck_3)
77+
reload_scene()

loading/pck_loading/icon.svg

+1
Loading

loading/pck_loading/icon.svg.import

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://bq1luvv1dkg2j"
6+
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://icon.svg"
14+
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.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
35+
svg/scale=1.0

loading/pck_loading/main.tscn

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
layout_mode = 3
7+
anchors_preset = 0
8+
script = ExtResource("1_dd1mq")
9+
pck_1 = "res://sub_projects/1.pck"
10+
pck_2 = "res://sub_projects/2.pck"
11+
pck_3 = "res://sub_projects/3.pck"
12+
13+
[node name="LineEdit" type="Label" parent="."]
14+
layout_mode = 0
15+
offset_left = 67.0
16+
offset_top = 75.0
17+
offset_right = 463.0
18+
offset_bottom = 375.0
19+
autowrap_mode = 1
20+
clip_text = true
21+
22+
[node name="VBoxContainer" type="VBoxContainer" parent="."]
23+
layout_mode = 0
24+
offset_left = 552.0
25+
offset_top = 81.0
26+
offset_right = 932.0
27+
offset_bottom = 379.0
28+
theme_override_constants/separation = 20
29+
30+
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
31+
layout_mode = 2
32+
size_flags_vertical = 3
33+
theme_override_constants/separation = 20
34+
35+
[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer"]
36+
layout_mode = 2
37+
size_flags_horizontal = 3
38+
text = "Load 1.pck"
39+
40+
[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer"]
41+
layout_mode = 2
42+
size_flags_horizontal = 3
43+
text = "Unload 1.pck"
44+
45+
[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer"]
46+
layout_mode = 2
47+
text = "Replace
48+
files?"
49+
50+
[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer"]
51+
layout_mode = 2
52+
size_flags_vertical = 3
53+
theme_override_constants/separation = 20
54+
55+
[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer2"]
56+
layout_mode = 2
57+
size_flags_horizontal = 3
58+
text = "Load 2.pck"
59+
60+
[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer2"]
61+
layout_mode = 2
62+
size_flags_horizontal = 3
63+
text = "Unload 2.pck"
64+
65+
[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer2"]
66+
layout_mode = 2
67+
text = "Replace
68+
files?"
69+
70+
[node name="HBoxContainer3" type="HBoxContainer" parent="VBoxContainer"]
71+
layout_mode = 2
72+
size_flags_vertical = 3
73+
theme_override_constants/separation = 20
74+
75+
[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer3"]
76+
layout_mode = 2
77+
size_flags_horizontal = 3
78+
text = "Load 3.pck"
79+
80+
[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer3"]
81+
layout_mode = 2
82+
size_flags_horizontal = 3
83+
text = "Unload 3.pck"
84+
85+
[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer3"]
86+
layout_mode = 2
87+
text = "Replace
88+
files?"
89+
90+
[node name="HBoxContainer" type="HBoxContainer" parent="."]
91+
layout_mode = 0
92+
offset_left = 44.0
93+
offset_top = 415.0
94+
offset_right = 934.0
95+
offset_bottom = 569.0
96+
size_flags_horizontal = 3
97+
98+
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"]
99+
layout_mode = 2
100+
size_flags_horizontal = 3
101+
102+
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer"]
103+
layout_mode = 2
104+
text = "orig.tscn"
105+
106+
[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer"]
107+
layout_mode = 2
108+
size_flags_vertical = 3
109+
110+
[node name="ViewOrig" type="SubViewport" parent="HBoxContainer/VBoxContainer/SubViewportContainer"]
111+
handle_input_locally = false
112+
size = Vector2i(256, 128)
113+
render_target_update_mode = 4
114+
115+
[node name="VBoxContainer2" type="VBoxContainer" parent="HBoxContainer"]
116+
layout_mode = 2
117+
size_flags_horizontal = 3
118+
119+
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer2"]
120+
layout_mode = 2
121+
text = "a.tscn"
122+
123+
[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer2"]
124+
layout_mode = 2
125+
size_flags_vertical = 3
126+
127+
[node name="ViewA" type="SubViewport" parent="HBoxContainer/VBoxContainer2/SubViewportContainer"]
128+
handle_input_locally = false
129+
size = Vector2i(256, 128)
130+
render_target_update_mode = 4
131+
132+
[node name="VBoxContainer3" type="VBoxContainer" parent="HBoxContainer"]
133+
layout_mode = 2
134+
size_flags_horizontal = 3
135+
136+
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer3"]
137+
layout_mode = 2
138+
text = "b.tscn"
139+
140+
[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer3"]
141+
layout_mode = 2
142+
size_flags_vertical = 3
143+
144+
[node name="ViewB" type="SubViewport" parent="HBoxContainer/VBoxContainer3/SubViewportContainer"]
145+
handle_input_locally = false
146+
size = Vector2i(256, 128)
147+
render_target_update_mode = 4
148+
149+
[node name="NoNodeLbl" type="Label" parent="."]
150+
layout_mode = 0
151+
offset_left = -184.0
152+
offset_top = -133.0
153+
offset_right = -144.0
154+
offset_bottom = -110.0
155+
text = "Missing scene file!"
156+
157+
[node name="Label" type="Label" parent="."]
158+
layout_mode = 0
159+
offset_left = 65.0
160+
offset_top = 49.0
161+
offset_right = 128.0
162+
offset_bottom = 75.0
163+
text = "File List:"
164+
165+
[connection signal="pressed" from="VBoxContainer/HBoxContainer/Load" to="." method="_on_load1_pressed"]
166+
[connection signal="pressed" from="VBoxContainer/HBoxContainer/Unload" to="." method="_on_unload1_pressed"]
167+
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Load" to="." method="_on_load2_pressed"]
168+
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Unload" to="." method="_on_unload2_pressed"]
169+
[connection signal="pressed" from="VBoxContainer/HBoxContainer3/Load" to="." method="_on_load3_pressed"]
170+
[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/features=PackedStringArray("4.1")
17+
config/icon="res://icon.png"
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.
5.83 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://ciqig5rfb0qsj" path="res://icon.svg" id="1_v38uw"]
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(64, 73)
15+
scale = Vector2(0.5, 0.5)
16+
texture = ExtResource("1_v38uw")
17+
script = ExtResource("2_u8kao")
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://ciqig5rfb0qsj"
6+
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://icon.svg"
14+
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.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
35+
svg/scale=1.0

0 commit comments

Comments
 (0)