Skip to content

Commit

Permalink
Add a PCK loading/unloading demo
Browse files Browse the repository at this point in the history
  • Loading branch information
rsubtil committed Jul 1, 2023
1 parent ae57eb2 commit 04071f6
Show file tree
Hide file tree
Showing 29 changed files with 589 additions and 0 deletions.
15 changes: 15 additions & 0 deletions loading/pck_loading/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# PCK loading demo

This demo shows how you can load and unload exported PCK files at runtime.

Language: GDScript

Renderer: GLES 2

## Sub-projects

There are three Godot projects under `sub_projects`. All have been exported to their respective PCK files and are loaded by the main project.

## Screenshots

![Screenshot](screenshots/screenshot.png)
77 changes: 77 additions & 0 deletions loading/pck_loading/Root.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
extends Control

@onready var vp_orig = $HBoxContainer/VBoxContainer/SubViewportContainer/ViewOrig
@onready var vp_a = $HBoxContainer/VBoxContainer2/SubViewportContainer/ViewA
@onready var vp_b = $HBoxContainer/VBoxContainer3/SubViewportContainer/ViewB
@onready var no_scene_lbl = $NoNodeLbl

@export_file("*.pck") var pck_1
@export_file("*.pck") var pck_2
@export_file("*.pck") var pck_3


func _ready():
reload_scene()


func reload_scene():
$LineEdit.text = ""
populate_file_list("res://")

load_scene_at_node("res://orig.tscn", vp_orig)
load_scene_at_node("res://a.tscn", vp_a)
load_scene_at_node("res://b.tscn", vp_b)


func populate_file_list(path: String):
var dir = DirAccess.open(path)
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
populate_file_list(path + file_name + "/")
else:
$LineEdit.text += path + file_name + '\n'
file_name = dir.get_next()


func load_scene_at_node(scene_name: String, node: Node):
var child
if ResourceLoader.exists(scene_name):
child = load(scene_name).instantiate()
else:
child = no_scene_lbl.duplicate()
child.position = Vector2.ZERO
if node.get_child_count() > 0:
node.remove_child(node.get_child(0))
node.add_child(child)


func _on_load1_pressed():
ProjectSettings.load_resource_pack(pck_1, $VBoxContainer/HBoxContainer/CheckBox.is_pressed())
reload_scene()


func _on_unload1_pressed():
ProjectSettings.unload_resource_pack(pck_1)
reload_scene()


func _on_load2_pressed():
ProjectSettings.load_resource_pack(pck_2, $VBoxContainer/HBoxContainer2/CheckBox.is_pressed())
reload_scene()


func _on_unload2_pressed():
ProjectSettings.unload_resource_pack(pck_2)
reload_scene()


func _on_load3_pressed():
ProjectSettings.load_resource_pack(pck_3, $VBoxContainer/HBoxContainer3/CheckBox.is_pressed())
reload_scene()


func _on_unload3_pressed():
ProjectSettings.unload_resource_pack(pck_3)
reload_scene()
1 change: 1 addition & 0 deletions loading/pck_loading/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions loading/pck_loading/icon.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bq1luvv1dkg2j"
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icon.svg"
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/bptc_ldr=0
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
170 changes: 170 additions & 0 deletions loading/pck_loading/main.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
[gd_scene load_steps=2 format=3 uid="uid://biqc8303kctlu"]

[ext_resource type="Script" path="res://Root.gd" id="1_dd1mq"]

[node name="Root" type="Control"]
layout_mode = 3
anchors_preset = 0
script = ExtResource("1_dd1mq")
pck_1 = "res://sub_projects/1.pck"
pck_2 = "res://sub_projects/2.pck"
pck_3 = "res://sub_projects/3.pck"

[node name="LineEdit" type="Label" parent="."]
layout_mode = 0
offset_left = 67.0
offset_top = 75.0
offset_right = 463.0
offset_bottom = 375.0
autowrap_mode = 1
clip_text = true

[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 0
offset_left = 552.0
offset_top = 81.0
offset_right = 932.0
offset_bottom = 379.0
theme_override_constants/separation = 20

[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
theme_override_constants/separation = 20

[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Load 1.pck"

[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "Unload 1.pck"

[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "Replace
files?"

[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
theme_override_constants/separation = 20

[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer2"]
layout_mode = 2
size_flags_horizontal = 3
text = "Load 2.pck"

[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer2"]
layout_mode = 2
size_flags_horizontal = 3
text = "Unload 2.pck"

[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer2"]
layout_mode = 2
text = "Replace
files?"

[node name="HBoxContainer3" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
theme_override_constants/separation = 20

[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer3"]
layout_mode = 2
size_flags_horizontal = 3
text = "Load 3.pck"

[node name="Unload" type="Button" parent="VBoxContainer/HBoxContainer3"]
layout_mode = 2
size_flags_horizontal = 3
text = "Unload 3.pck"

[node name="CheckBox" type="CheckBox" parent="VBoxContainer/HBoxContainer3"]
layout_mode = 2
text = "Replace
files?"

[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 0
offset_left = 44.0
offset_top = 415.0
offset_right = 934.0
offset_bottom = 569.0
size_flags_horizontal = 3

[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3

[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer"]
layout_mode = 2
text = "orig.tscn"

[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3

[node name="ViewOrig" type="SubViewport" parent="HBoxContainer/VBoxContainer/SubViewportContainer"]
handle_input_locally = false
size = Vector2i(256, 128)
render_target_update_mode = 4

[node name="VBoxContainer2" type="VBoxContainer" parent="HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3

[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer2"]
layout_mode = 2
text = "a.tscn"

[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer2"]
layout_mode = 2
size_flags_vertical = 3

[node name="ViewA" type="SubViewport" parent="HBoxContainer/VBoxContainer2/SubViewportContainer"]
handle_input_locally = false
size = Vector2i(256, 128)
render_target_update_mode = 4

[node name="VBoxContainer3" type="VBoxContainer" parent="HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3

[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer3"]
layout_mode = 2
text = "b.tscn"

[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer/VBoxContainer3"]
layout_mode = 2
size_flags_vertical = 3

[node name="ViewB" type="SubViewport" parent="HBoxContainer/VBoxContainer3/SubViewportContainer"]
handle_input_locally = false
size = Vector2i(256, 128)
render_target_update_mode = 4

[node name="NoNodeLbl" type="Label" parent="."]
layout_mode = 0
offset_left = -184.0
offset_top = -133.0
offset_right = -144.0
offset_bottom = -110.0
text = "Missing scene file!"

[node name="Label" type="Label" parent="."]
layout_mode = 0
offset_left = 65.0
offset_top = 49.0
offset_right = 128.0
offset_bottom = 75.0
text = "File List:"

[connection signal="pressed" from="VBoxContainer/HBoxContainer/Load" to="." method="_on_load1_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer/Unload" to="." method="_on_unload1_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Load" to="." method="_on_load2_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer2/Unload" to="." method="_on_unload2_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer3/Load" to="." method="_on_load3_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer3/Unload" to="." method="_on_unload3_pressed"]
14 changes: 14 additions & 0 deletions loading/pck_loading/orig.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[gd_scene format=3 uid="uid://dr3ofl6lrt7v2"]

[node name="Control" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0

[node name="Label" type="Label" parent="."]
offset_right = 306.0
offset_bottom = 104.0
text = "Greetings from original data.pck
file! This should always be here;
if somehow this scene fails to
load, something messed up
`res://` lookup!"
23 changes: 23 additions & 0 deletions loading/pck_loading/project.godot
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=5

[application]

config/name="PCK loading"
config/description="This demo shows how you can load and unload exported PCK files at runtime."
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.1")
config/icon="res://icon.png"

[rendering]

quality/driver/driver_name="GLES2"
vram_compression/import_etc=true
vram_compression/import_etc2=false
Empty file.
Binary file added loading/pck_loading/screenshots/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Binary file added loading/pck_loading/sub_projects/1.pck
Binary file not shown.
17 changes: 17 additions & 0 deletions loading/pck_loading/sub_projects/1/a.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[gd_scene load_steps=3 format=3 uid="uid://cynxmx85sit6h"]

[ext_resource type="Texture2D" uid="uid://ciqig5rfb0qsj" path="res://icon.svg" id="1_v38uw"]
[ext_resource type="Script" path="res://script1.gd" id="2_u8kao"]

[node name="Node2D" type="Node2D"]

[node name="Label" type="Label" parent="."]
offset_right = 40.0
offset_bottom = 23.0
text = "Greetings from 1.pck!"

[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(64, 73)
scale = Vector2(0.5, 0.5)
texture = ExtResource("1_v38uw")
script = ExtResource("2_u8kao")
1 change: 1 addition & 0 deletions loading/pck_loading/sub_projects/1/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions loading/pck_loading/sub_projects/1/icon.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://ciqig5rfb0qsj"
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icon.svg"
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/bptc_ldr=0
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
Loading

0 comments on commit 04071f6

Please sign in to comment.