From 1b55da71e92a0873c380ef57cebb9b85c8e9638b Mon Sep 17 00:00:00 2001 From: bakhtaward <78761693+bakhtaward@users.noreply.github.com> Date: Sun, 25 Sep 2022 11:53:39 -0400 Subject: [PATCH] Import character from a little while ago (only works with .txt files) --- .../screens/ImportCharacter.tscn | 28 +++++++- .../Import Character/screens/MenuScreen.tscn | 6 +- .../Import Character/screens/grabFocus.gd | 71 +++++++++++++------ 3 files changed, 77 insertions(+), 28 deletions(-) diff --git a/Phase2/Godot ===(Game Code)===/Import Character/screens/ImportCharacter.tscn b/Phase2/Godot ===(Game Code)===/Import Character/screens/ImportCharacter.tscn index 0cd64ed..16e19de 100644 --- a/Phase2/Godot ===(Game Code)===/Import Character/screens/ImportCharacter.tscn +++ b/Phase2/Godot ===(Game Code)===/Import Character/screens/ImportCharacter.tscn @@ -39,7 +39,7 @@ next_scene_path = "res://Screens/MenuScreen.tscn" [node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer2"] margin_top = 70.0 margin_right = 1024.0 -margin_bottom = 114.0 +margin_bottom = 118.0 __meta__ = { "_edit_use_anchors_": false } @@ -56,11 +56,12 @@ __meta__ = { margin_top = 24.0 margin_right = 800.0 margin_bottom = 374.0 +rect_min_size = Vector2( 400, 140 ) window_title = "Open a File" mode = 0 access = 2 -current_dir = "/Users/BD/Documents/Godot/rpg" -current_path = "/Users/BD/Documents/Godot/rpg/" +current_dir = "/Users/BD_1/Documents/Godot/mainmenu:impchar/rpg" +current_path = "/Users/BD_1/Documents/Godot/mainmenu:impchar/rpg/" __meta__ = { "_edit_use_anchors_": false } @@ -83,5 +84,26 @@ margin_right = 1024.0 margin_bottom = 44.0 text = "Save" +[node name="VBoxContainer3" type="VBoxContainer" parent="VBoxContainer2/VBoxContainer"] +margin_top = 48.0 +margin_right = 1024.0 +margin_bottom = 48.0 + +[node name="textBox" type="LineEdit" parent="VBoxContainer2/VBoxContainer/VBoxContainer3"] +visible = false +margin_right = 1024.0 +margin_bottom = 24.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="But_Remember" type="Button" parent="VBoxContainer2"] +margin_top = 122.0 +margin_right = 1024.0 +margin_bottom = 142.0 +text = "Remember File" + [connection signal="pressed" from="VBoxContainer2/VBoxContainer/But_OpenFile" to="." method="_on_Button_pressed"] [connection signal="file_selected" from="VBoxContainer2/VBoxContainer/FileDialog" to="." method="_on_FileDialog_file_selected"] +[connection signal="pressed" from="VBoxContainer2/VBoxContainer/Save_Button" to="." method="_on_Save_Button_pressed"] +[connection signal="pressed" from="VBoxContainer2/But_Remember" to="." method="_on_But_Remember_pressed"] diff --git a/Phase2/Godot ===(Game Code)===/Import Character/screens/MenuScreen.tscn b/Phase2/Godot ===(Game Code)===/Import Character/screens/MenuScreen.tscn index c968850..434c090 100644 --- a/Phase2/Godot ===(Game Code)===/Import Character/screens/MenuScreen.tscn +++ b/Phase2/Godot ===(Game Code)===/Import Character/screens/MenuScreen.tscn @@ -19,6 +19,8 @@ __meta__ = { [node name="Background" type="Panel" parent="."] anchor_right = 1.0 anchor_bottom = 1.0 +margin_top = 2.0 +margin_bottom = 2.0 __meta__ = { "_edit_use_anchors_": false } @@ -31,9 +33,9 @@ custom_fonts/font = ExtResource( 3 ) [node name="VBoxContainer" type="VBoxContainer" parent="."] anchor_left = 0.5 -anchor_top = 0.5 +anchor_top = 0.503333 anchor_right = 0.5 -anchor_bottom = 0.5 +anchor_bottom = 0.503333 margin_left = -119.0 margin_top = -152.0 margin_right = 119.0 diff --git a/Phase2/Godot ===(Game Code)===/Import Character/screens/grabFocus.gd b/Phase2/Godot ===(Game Code)===/Import Character/screens/grabFocus.gd index b463586..b2370e3 100644 --- a/Phase2/Godot ===(Game Code)===/Import Character/screens/grabFocus.gd +++ b/Phase2/Godot ===(Game Code)===/Import Character/screens/grabFocus.gd @@ -1,35 +1,60 @@ extends Control -var charFilePath -var a -#GRAB FOCUS: simple script for temp files to grab focus - +#charFilePath will hold the file path for the character file, fileText will hold the text in the file +var charFilePath = "" +var fileText func _ready() -> void: + #grab focus will make it so tab can be used to move between options $VBoxContainer2/Title/But_ChangeScene.grab_focus() + + var save_game = File.new() + if save_game.file_exists("user://savegame.save"): + save_game.open("user://savegame.save", File.READ) + var tempPath = save_game.get_as_text() + displayInfo(tempPath) + + save_game.close() +func displayInfo(path): + var file = File.new() + #read the text in the file, save it in the variable fileText, display in textBox + file.open(path, File.READ) + + fileText = file.get_as_text() + + $VBoxContainer2/VBoxContainer/VBoxContainer3/textBox.visible = true + $VBoxContainer2/VBoxContainer/VBoxContainer3/textBox.text = fileText +# + + #save the file path in the global var to use it in the other function + charFilePath = path + file.close() + func _on_Button_pressed(): $VBoxContainer2/VBoxContainer/FileDialog.popup() -#this is going to take information from the file the player chose and put the individul parts into textboxes so it can be edited and then saved func _on_FileDialog_file_selected(path): + displayInfo(path) + +#when they've made the changes and pressed save: +func _on_Save_Button_pressed(): + #open the same file so that changes can be written to it var file = File.new() - #read the text in the file, save it in the variable a - file.open(path, File.READ) -# a = int(file.get_line()) - a = file.get_as_text() - #split by the spaces so the individual pieces can be separated into textboxes - var b = a.split(" ") -# for i in b.size(): - var i = 0 - while i < b.size(): - #make a new textbox for each piece of information - var textLine = Label.new() - $VBoxContainer2/VBoxContainer.add_child(textLine) - textLine.text = b[i] - i += 1 - var textBox = LineEdit.new() - $VBoxContainer2/VBoxContainer.add_child(textBox) - textBox.text = b[i] - i += 1 + file.open(charFilePath, File.WRITE) + + #store the text from the textbox back into the file + var content = "" + var currentWord = $VBoxContainer2/VBoxContainer/VBoxContainer3/textBox.text + + content = currentWord + file.store_string(content) file.close() + + +func _on_But_Remember_pressed(): + #save the path of the file so that it can be automatically opened next time + var save_game = File.new() + save_game.open("user://savegame.save", File.WRITE) + save_game.store_string(charFilePath) + save_game.close()