Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PCK loading/unloading demo #751

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gui/input_mapping/ActionRemapButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ func _unhandled_key_input(event):


func remap_action_to(event):
# We first change the event in this game instance.
InputMap.action_erase_events(action)
InputMap.action_add_event(action, event)
# And then save it to the keymaps file
KeyPersistence.keymaps[action] = event
KeyPersistence.save_keymap()
text = "%s Key" % event.as_text()


Expand Down
45 changes: 45 additions & 0 deletions gui/input_mapping/KeyPersistence.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This is an autoload (singleton) which will save
# the key maps in a simple way through a dictionary.
extends Node

const keymaps_path = "user://keymaps.dat"
var keymaps: Dictionary


func _ready() -> void:
# First we create the keymap dictionary on startup with all
# the keymap actions we have.
for action in InputMap.get_actions():
keymaps[action] = InputMap.get_action_list(action)[0]
load_keymap()


func load_keymap() -> void:
var file := File.new()
if not file.file_exists(keymaps_path):
save_keymap() # There is no save file yet, so let's create one.
return
#warning-ignore:return_value_discarded
file.open(keymaps_path, File.READ)
var temp_keymap: Dictionary = file.get_var(true)
file.close()
# We don't just replace the keymaps dictionary, because if you
# updated your game and removed/added keymaps, the data of this
# save file may have invalid actions. So we check one by one to
# make sure that the keymap dictionary really has all current actions.
for action in keymaps.keys():
if temp_keymap.has(action):
keymaps[action] = temp_keymap[action]
# Whilst setting the keymap dictionary, we also set the
# correct InputMap event
InputMap.action_erase_events(action)
InputMap.action_add_event(action, keymaps[action])


func save_keymap() -> void:
# For saving the keymap, we just save the entire dictionary as a var.
var file := File.new()
#warning-ignore:return_value_discarded
file.open(keymaps_path, File.WRITE)
file.store_var(keymaps, true)
file.close()
4 changes: 4 additions & 0 deletions gui/input_mapping/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ config/description="A demo showing how to build an input key remapping screen.
run/main_scene="res://InputRemapMenu.tscn"
config/icon="res://icon.png"

[autoload]

KeyPersistence="*res://KeyPersistence.gd"

[display]

window/size/width=640
Expand Down
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)
78 changes: 78 additions & 0 deletions loading/pck_loading/Root.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 = Directory.new()
dir.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()
Binary file added loading/pck_loading/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions loading/pck_loading/icon.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://btpbmejdh00qx"
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icon.png"
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.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
Loading