From e406e9e44c3af06491332cc1ee8056c7fd8cfdc3 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 4 Dec 2023 22:19:23 +0100 Subject: [PATCH] open SQLite from Godot --- godot/global.gd | 28 ++++++++++++++++++++++++++++ godot/main.gd | 8 +++++++- godot/main.tscn | 1 + godot/project.godot | 8 ++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 godot/global.gd diff --git a/godot/global.gd b/godot/global.gd new file mode 100644 index 0000000..7817cb4 --- /dev/null +++ b/godot/global.gd @@ -0,0 +1,28 @@ +extends Node +# https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html + +var current_scene = null +var db : SQLite = null + +func _ready(): + var root = get_tree().root + current_scene = root.get_child(root.get_child_count() - 1) + +func goto_scene(path): + call_deferred("_deferred_goto_scene", path) + +func _deferred_goto_scene(path): + # It is now safe to remove the current scene. + current_scene.free() + + # Load the new scene. + var s = ResourceLoader.load(path) + + # Instance the new scene. + current_scene = s.instantiate() + + # Add it to the active scene, as child of root. + get_tree().root.add_child(current_scene) + + # Optionally, to make it compatible with the SceneTree.change_scene_to_file() API. + get_tree().current_scene = current_scene diff --git a/godot/main.gd b/godot/main.gd index 7db7f51..ce83834 100644 --- a/godot/main.gd +++ b/godot/main.gd @@ -1,8 +1,8 @@ extends Control - # Called when the node enters the scene tree for the first time. func _ready(): + $FileDialog.mode = FileDialog.FILE_MODE_OPEN_FILE $FileDialog.access = FileDialog.ACCESS_FILESYSTEM $FileDialog.use_native_dialog = true @@ -13,3 +13,9 @@ func _process(delta): func _on_button_pressed(): $FileDialog.show() print("button pressed") + +func _on_file_dialog_file_selected(path): + Global.db = SQLite.new() + Global.db.path = path + Global.db.verbosity_level = SQLite.VERBOSE + Global.db.open_db() diff --git a/godot/main.tscn b/godot/main.tscn index 00ed960..59546ee 100644 --- a/godot/main.tscn +++ b/godot/main.tscn @@ -35,3 +35,4 @@ text = "Load database" [node name="FileDialog" type="FileDialog" parent="."] [connection signal="pressed" from="MarginContainer/CenterContainer/Button" to="." method="_on_button_pressed"] +[connection signal="file_selected" from="FileDialog" to="." method="_on_file_dialog_file_selected"] diff --git a/godot/project.godot b/godot/project.godot index d74de34..8fbccb2 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -15,11 +15,19 @@ run/main_scene="res://main.tscn" config/features=PackedStringArray("4.2", "GL Compatibility") config/icon="res://icon.svg" +[autoload] + +Global="*res://global.gd" + [display] window/size/viewport_width=600 window/size/viewport_height=900 +[editor_plugins] + +enabled=PackedStringArray("res://addons/godot-sqlite/plugin.cfg") + [rendering] renderer/rendering_method="gl_compatibility"