forked from interstellar_development/freettrpg
		
	Code refactoring: Part 1
This commit is contained in:
		
							parent
							
								
									e1ce830b19
								
							
						
					
					
						commit
						8bb83e987b
					
				
					 6 changed files with 67 additions and 68 deletions
				
			
		|  | @ -15,6 +15,12 @@ run/main_scene="res://scenes/menu/main.tscn" | |||
| config/features=PackedStringArray("4.2", "GL Compatibility") | ||||
| config/icon="res://icon.svg" | ||||
| 
 | ||||
| [autoload] | ||||
| 
 | ||||
| Load="*res://scripts/load.gd" | ||||
| Save="*res://scripts/save.gd" | ||||
| Content="*res://scripts/content.gd" | ||||
| 
 | ||||
| [display] | ||||
| 
 | ||||
| window/size/viewport_width=800 | ||||
|  |  | |||
							
								
								
									
										8
									
								
								scripts/content.gd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								scripts/content.gd
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,8 @@ | |||
| extends Node | ||||
| 
 | ||||
| var loadPath | ||||
| var data | ||||
| 
 | ||||
| func _ready(): | ||||
| 	loadPath = "res://content/stats.json" | ||||
| 	data = Load.loadJSON(loadPath) | ||||
|  | @ -1,26 +1,10 @@ | |||
| extends Node | ||||
| 
 | ||||
| var field = preload("res://scenes/menu/inputOutput.tscn") | ||||
| var data:Dictionary | ||||
| 
 | ||||
| func loadJSON(savePath): | ||||
| 	if not FileAccess.file_exists(savePath): | ||||
| 		return false | ||||
| 	var fileAccess = FileAccess.open(savePath, FileAccess.READ) | ||||
| 	var jsonString = fileAccess.get_line() | ||||
| 	fileAccess.close() | ||||
| 
 | ||||
| 	var json = JSON.new() | ||||
| 	var error = json.parse(jsonString) | ||||
| 	if error: | ||||
| 		print("JSON Parse Error: ", json.get_error_message(), " in ", jsonString, " at line ", json.get_error_line()) | ||||
| 		return false | ||||
| 
 | ||||
| 	data = json.data | ||||
| 	return true | ||||
| var data | ||||
| 
 | ||||
| func _ready(): | ||||
| 	if loadJSON("res://content/stats.json"): | ||||
| 	data = Load.loadJSON("res://content/stats.json") | ||||
| 	for i in range(int(data.get("amount"))): | ||||
| 		var fieldInstance = field.instantiate() | ||||
| 		$"ScrollContainer/VBoxContainer".add_child(fieldInstance) | ||||
|  | @ -28,26 +12,13 @@ func _ready(): | |||
| 		$"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) | ||||
| 		$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 | ||||
| 
 | ||||
| func saveJSON(savePath):	 | ||||
| func _on_button_pressed(): | ||||
| 	var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") | ||||
| 	var saveData = {} | ||||
| 	 | ||||
| 	for i in range(int(data.get("amount"))): | ||||
| 		if i > 0: | ||||
| 			saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text | ||||
| 	 | ||||
| 	var jsonString = JSON.stringify(saveData) | ||||
| 	 | ||||
| 	var fileAccess = FileAccess.open(savePath, FileAccess.WRITE) | ||||
| 	if not fileAccess: | ||||
| 		print("An error happened while saving data: ", FileAccess.get_open_error()) | ||||
| 		return | ||||
| 	 | ||||
| 	fileAccess.store_line(jsonString) | ||||
| 	fileAccess.close() | ||||
| 
 | ||||
| func _on_button_pressed(): | ||||
| 	var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") | ||||
| 	saveJSON(savePath) | ||||
| 	Save.saveJSON(savePath, saveData) | ||||
| 
 | ||||
| func _process(delta): | ||||
| 	if Input.is_action_pressed("escape"): | ||||
|  |  | |||
|  | @ -1,27 +1,11 @@ | |||
| extends Node | ||||
| 
 | ||||
| var field = preload("res://scenes/menu/inputOutput.tscn") | ||||
| var data:Dictionary | ||||
| var data | ||||
| var amount | ||||
| 
 | ||||
| func loadJSON(savePath): | ||||
| 	if not FileAccess.file_exists(savePath): | ||||
| 		return false | ||||
| 	var fileAccess = FileAccess.open(savePath, FileAccess.READ) | ||||
| 	var json_string = fileAccess.get_line() | ||||
| 	fileAccess.close() | ||||
| 
 | ||||
| 	var json = JSON.new() | ||||
| 	var error = json.parse(json_string) | ||||
| 	if error: | ||||
| 		print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) | ||||
| 		return false | ||||
| 
 | ||||
| 	data = json.data | ||||
| 	return true | ||||
| 
 | ||||
| func _ready(): | ||||
| 	if loadJSON("res://content/stats.json"): | ||||
| 	data = Content.data | ||||
| 	for i in range(int(data.get("amount"))): | ||||
| 		var fieldInstance = field.instantiate() | ||||
| 		$"ScrollContainer/VBoxContainer".add_child(fieldInstance) | ||||
|  | @ -33,7 +17,8 @@ func _ready(): | |||
| 
 | ||||
| func _on_button_pressed(): | ||||
| 	var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") | ||||
| 	if loadJSON(savePath): | ||||
| 	data = Load.loadJSON(savePath) | ||||
| 	if typeof(data) == 27: | ||||
| 		for i in range(int(amount)-1): | ||||
| 			$"ScrollContainer/VBoxContainer".get_child(i+1).text = data.get(str($"ScrollContainer/VBoxContainer".get_child(i+1).placeholder_text)) | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										17
									
								
								scripts/load.gd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								scripts/load.gd
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| extends Node | ||||
| 
 | ||||
| func loadJSON(savePath): | ||||
| 	var data | ||||
| 	if not FileAccess.file_exists(savePath): | ||||
| 		return 1 | ||||
| 	var fileAccess = FileAccess.open(savePath, FileAccess.READ) | ||||
| 	var jsonString = fileAccess.get_line() | ||||
| 	fileAccess.close() | ||||
| 
 | ||||
| 	var json = JSON.new() | ||||
| 	var error = json.parse(jsonString) | ||||
| 	if error: | ||||
| 		return 1 | ||||
| 
 | ||||
| 	data = json.data | ||||
| 	return data | ||||
							
								
								
									
										12
									
								
								scripts/save.gd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								scripts/save.gd
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | |||
| extends Node | ||||
| 
 | ||||
| func saveJSON(savePath, saveData): | ||||
| 	var jsonString = JSON.stringify(saveData) | ||||
| 	 | ||||
| 	var fileAccess = FileAccess.open(savePath, FileAccess.WRITE) | ||||
| 	if not fileAccess: | ||||
| 		print("An error happened while saving data: ", FileAccess.get_open_error()) | ||||
| 		return 1 | ||||
| 	 | ||||
| 	fileAccess.store_line(jsonString) | ||||
| 	fileAccess.close() | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Patrick_Pluto
						Patrick_Pluto