Import character from a little while ago (only works with .txt files)

This commit is contained in:
bakhtaward 2022-09-25 11:53:39 -04:00
parent ff0782460f
commit 1b55da71e9
3 changed files with 77 additions and 28 deletions

View File

@ -39,7 +39,7 @@ next_scene_path = "res://Screens/MenuScreen.tscn"
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer2"] [node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer2"]
margin_top = 70.0 margin_top = 70.0
margin_right = 1024.0 margin_right = 1024.0
margin_bottom = 114.0 margin_bottom = 118.0
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
@ -56,11 +56,12 @@ __meta__ = {
margin_top = 24.0 margin_top = 24.0
margin_right = 800.0 margin_right = 800.0
margin_bottom = 374.0 margin_bottom = 374.0
rect_min_size = Vector2( 400, 140 )
window_title = "Open a File" window_title = "Open a File"
mode = 0 mode = 0
access = 2 access = 2
current_dir = "/Users/BD/Documents/Godot/rpg" current_dir = "/Users/BD_1/Documents/Godot/mainmenu:impchar/rpg"
current_path = "/Users/BD/Documents/Godot/rpg/" current_path = "/Users/BD_1/Documents/Godot/mainmenu:impchar/rpg/"
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
@ -83,5 +84,26 @@ margin_right = 1024.0
margin_bottom = 44.0 margin_bottom = 44.0
text = "Save" 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="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="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"]

View File

@ -19,6 +19,8 @@ __meta__ = {
[node name="Background" type="Panel" parent="."] [node name="Background" type="Panel" parent="."]
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
margin_top = 2.0
margin_bottom = 2.0
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
@ -31,9 +33,9 @@ custom_fonts/font = ExtResource( 3 )
[node name="VBoxContainer" type="VBoxContainer" parent="."] [node name="VBoxContainer" type="VBoxContainer" parent="."]
anchor_left = 0.5 anchor_left = 0.5
anchor_top = 0.5 anchor_top = 0.503333
anchor_right = 0.5 anchor_right = 0.5
anchor_bottom = 0.5 anchor_bottom = 0.503333
margin_left = -119.0 margin_left = -119.0
margin_top = -152.0 margin_top = -152.0
margin_right = 119.0 margin_right = 119.0

View File

@ -1,35 +1,60 @@
extends Control extends Control
var charFilePath #charFilePath will hold the file path for the character file, fileText will hold the text in the file
var a var charFilePath = ""
#GRAB FOCUS: simple script for temp files to grab focus var fileText
func _ready() -> void: func _ready() -> void:
#grab focus will make it so tab can be used to move between options
$VBoxContainer2/Title/But_ChangeScene.grab_focus() $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(): func _on_Button_pressed():
$VBoxContainer2/VBoxContainer/FileDialog.popup() $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): 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() var file = File.new()
#read the text in the file, save it in the variable a file.open(charFilePath, File.WRITE)
file.open(path, File.READ)
# a = int(file.get_line()) #store the text from the textbox back into the file
a = file.get_as_text() var content = ""
#split by the spaces so the individual pieces can be separated into textboxes var currentWord = $VBoxContainer2/VBoxContainer/VBoxContainer3/textBox.text
var b = a.split(" ")
# for i in b.size(): content = currentWord
var i = 0 file.store_string(content)
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.close() 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()