From b803229d323bed19d965487dba7aaefb732efd56 Mon Sep 17 00:00:00 2001 From: Sage The DM Date: Tue, 18 Jun 2024 18:58:47 +0200 Subject: [PATCH 1/2] Commented your code --- scripts/content.gd | 8 +++-- scripts/createStats.gd | 47 +++++++++++++++++++--------- scripts/getStats.gd | 60 +++++++++++++++++++++++------------ scripts/load.gd | 41 +++++++++++++++--------- scripts/mapscript.gd | 67 ++++++++++++++++++++++++++-------------- scripts/menu.gd | 16 +++++----- scripts/player.gd | 65 ++++++++++++++++++++++++-------------- scripts/save.gd | 9 ++++++ testing/test/player.tscn | 36 +++++++++++++-------- 9 files changed, 232 insertions(+), 117 deletions(-) diff --git a/scripts/content.gd b/scripts/content.gd index dc918a3..09f41da 100644 --- a/scripts/content.gd +++ b/scripts/content.gd @@ -1,8 +1,12 @@ extends Node +# Variable to store the path from which JSON data is loaded var loadPath + +# Variable to store the loaded JSON data var data +# Called when the node is added to the scene func _ready(): - loadPath = "res://content/stats.json" - data = Load.loadJSON(loadPath) + loadPath = "res://content/stats.json" # Set the path to load JSON data from + data = Load.loadJSON(loadPath) # Load JSON data from the specified path diff --git a/scripts/createStats.gd b/scripts/createStats.gd index 7e34f11..51e438b 100644 --- a/scripts/createStats.gd +++ b/scripts/createStats.gd @@ -1,25 +1,42 @@ extends Node +# Reference to the inputOutput.tscn scene var field = preload("res://scenes/menu/inputOutput.tscn") + +# Variable to store loaded JSON data var data +# Called when the node is added to the scene func _ready(): - data = Load.loadJSON("res://content/stats.json") - for i in range(int(data.get("amount"))): - var fieldInstance = field.instantiate() - $"ScrollContainer/VBoxContainer".add_child(fieldInstance) - $"ScrollContainer/VBoxContainer".get_child(i).position = Vector2(16,(16+i*88)) - $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) - $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 + # Load JSON data from stats.json + data = Load.loadJSON("res://content/stats.json") + + # Instantiate input fields based on 'amount' from data + for i in range(int(data.get("amount"))): + var fieldInstance = field.instance() + $"ScrollContainer/VBoxContainer".add_child(fieldInstance) + + # Set position, placeholder text, and minimum size for each field + $"ScrollContainer/VBoxContainer".get_child(i).position = Vector2(16, (16 + i * 88)) + $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) + $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 +# Handler for when a button is pressed 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 - Save.saveJSON(savePath, saveData) + # Construct save path based on user input + var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json" + + # Prepare data to save + var saveData = {} + for i in range(int(data.get("amount"))): + if i > 0: + saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text + + # Save data to JSON file + Save.saveJSON(savePath, saveData) +# Process function called every frame func _process(delta): - if Input.is_action_pressed("escape"): - get_tree().change_scene_to_file("res://scenes/menu/main.tscn") + # Check if the Escape key is pressed to change scene to main.tscn + if Input.is_action_pressed("escape"): + get_tree().change_scene_to_file("res://scenes/menu/main.tscn") diff --git a/scripts/getStats.gd b/scripts/getStats.gd index b9e5fd2..bb1f83b 100644 --- a/scripts/getStats.gd +++ b/scripts/getStats.gd @@ -1,31 +1,53 @@ extends Node +# Reference to the inputOutput.tscn scene var field = preload("res://scenes/menu/inputOutput.tscn") + +# Variables to store data and amount var data var amount +# Called when the node is added to the scene func _ready(): - data = Content.data - for i in range(int(data.get("amount"))): - var fieldInstance = field.instantiate() - $"ScrollContainer/VBoxContainer".add_child(fieldInstance) - $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) - $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 - if i > 0: - $"ScrollContainer/VBoxContainer".get_child(i).editable = false - amount = data.get("amount") + # Get data from Content singleton + data = Content.data + + # Instantiate input fields based on 'amount' from data + for i in range(int(data.get("amount"))): + var fieldInstance = field.instance() + $"ScrollContainer/VBoxContainer".add_child(fieldInstance) + + # Set placeholder text and minimum size for each field + $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) + $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 + + # Make fields editable except the first one + if i > 0: + $"ScrollContainer/VBoxContainer".get_child(i).editable = false + + amount = data.get("amount") # Store the amount of fields +# Handler for when a button is pressed func _on_button_pressed(): - var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") - 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)) + # Construct save path based on user input + var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json" + + # Load JSON data from the constructed save path + data = Load.loadJSON(savePath) + + # Check if data is successfully loaded (type 27 corresponds to Dictionary in Godot) + if typeof(data) == 27: + # Populate fields with loaded data + 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)) +# Process function called every frame func _process(_delta): - if Input.is_action_pressed("escape"): - get_tree().change_scene_to_file("res://scenes/menu/main.tscn") - + # Check if the Escape key is pressed to change scene to main.tscn + if Input.is_action_pressed("escape"): + get_tree().change_scene_to_file("res://scenes/menu/main.tscn") + +# Function to hide the 'get' button func hideGet(): - $button.visible = false - $button.disabled = true + $button.visible = false + $button.disabled = true diff --git a/scripts/load.gd b/scripts/load.gd index 2218678..d96ffa6 100644 --- a/scripts/load.gd +++ b/scripts/load.gd @@ -1,17 +1,30 @@ extends Node +# Function to load JSON data from a file 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 + var data # Variable to store loaded data + + # Check if the file exists + if not FileAccess.file_exists(savePath): + return 1 # Return error code 1 if file does not exist + + # Open the file for reading + var fileAccess = FileAccess.open(savePath, FileAccess.READ) + + # Read the entire JSON string from the file + var jsonString = fileAccess.get_line() + + fileAccess.close() # Close the file + + # Create a new JSON instance + var json = JSON.new() + + # Parse the JSON string into a JSON object + var error = json.parse(jsonString) + + if error: + return 1 # Return error code 1 if there was an error parsing JSON + + data = json.data # Extract data from the parsed JSON + + return data # Return the loaded data diff --git a/scripts/mapscript.gd b/scripts/mapscript.gd index 5bc567e..31dd75f 100644 --- a/scripts/mapscript.gd +++ b/scripts/mapscript.gd @@ -1,37 +1,56 @@ extends Node2D -var playerAmount=2 -var playerIndex=0 +# Number of players and current player index +var playerAmount = 2 +var playerIndex = 0 + +# Variable to hold the current player's object name var objectName +# Called when the node is added to the scene func _ready(): - $"../characterViewer".hideGet() - next() + # Hide character viewer GUI initially + $"../characterViewer".hideGet() + # Start cycling through players + next() +# Function to switch to the next player func next(): - if playerIndex >= playerAmount: - playerIndex = 0 - objectName= "player%d" % playerIndex - print(objectName) - get_node(objectName).start() - playerIndex += 1 + if playerIndex >= playerAmount: + playerIndex = 0 + + # Construct object name based on player index + objectName = "player%d" % playerIndex + print(objectName) # Print current player's object name (for debugging) + + # Start the current player's activity + get_node(objectName).start() + + playerIndex += 1 # Move to the next player index +# Function to display statistics func stats(): - visible = false - $"../characterViewer".visible = true - get_node(objectName).stop() - $"../back".disabled = false - $"../back".visible = true - $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).text = str(playerIndex-1) - $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).editable = false - $"../characterViewer"._on_button_pressed() + visible = false # Hide current node (assuming this node should hide itself) + $"../characterViewer".visible = true # Show character viewer GUI + get_node(objectName).stop() # Stop current player's activity + $"../back".disabled = false # Enable back button + $"../back".visible = true # Make back button visible + + # Update player index display in character viewer + $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).text = str(playerIndex - 1) + $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).editable = false + + # Simulate button press in character viewer (assuming this triggers further actions) + $"../characterViewer"._on_button_pressed() +# Function to restart the node's state func restart(): - visible = true - $"../characterViewer".visible = false - get_node(objectName).start() - $"../back".disabled = true - $"../back".visible = false + visible = true # Show current node + $"../characterViewer".visible = false # Hide character viewer GUI + get_node(objectName).start() # Start current player's activity + $"../back".disabled = true # Disable back button + $"../back".visible = false # Hide back button +# Handler for when the back button is pressed func _on_back_pressed(): - restart() + restart() # Restart the node's state diff --git a/scripts/menu.gd b/scripts/menu.gd index 8c35282..9b9fa64 100644 --- a/scripts/menu.gd +++ b/scripts/menu.gd @@ -1,14 +1,16 @@ extends Node - - +# Function called when button 1 is pressed func _on_button_pressed(): - get_tree().change_scene_to_file("res://scenes/map/map.tscn") - + get_tree().change_scene_to_file("res://scenes/map/map.tscn") + # Change the scene to the map scene ("map.tscn") +# Function called when button 2 is pressed func _on_button_2_pressed(): - get_tree().change_scene_to_file("res://scenes/menu/view.tscn") - + get_tree().change_scene_to_file("res://scenes/menu/view.tscn") + # Change the scene to the view menu scene ("view.tscn") +# Function called when button 3 is pressed func _on_button_3_pressed(): - get_tree().change_scene_to_file("res://scenes/menu/create.tscn") + get_tree().change_scene_to_file("res://scenes/menu/create.tscn") + # Change the scene to the create menu scene ("create.tscn") diff --git a/scripts/player.gd b/scripts/player.gd index b1c9c40..50b9c58 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -1,39 +1,58 @@ extends CharacterBody2D +# Exported variable for movement speed @export var speed = 200 + +# Distance traveled accumulator var distanceTo = 0 + +# Flag to indicate if the character is active var active = false +# Function to handle user input func get_input(): - if Input.is_action_pressed("escape"): - get_tree().change_scene_to_file("res://scenes/menu/main.tscn") - var input_direction = Input.get_vector("left", "right", "up", "down") - velocity = input_direction * speed + # Change scene if Escape key is pressed + if Input.is_action_pressed("escape"): + get_tree().change_scene_to_file("res://scenes/menu/main.tscn") + + # Get directional input and set velocity accordingly + var input_direction = Input.get_vector("left", "right", "up", "down") + velocity = input_direction * speed +# Physics process function called every frame func _physics_process(delta): - var toCalculate = position - if active: - get_input() - move_and_slide() - distanceTo += position.distance_to(toCalculate) - print(distanceTo) - if distanceTo > 500 and active: - stop() - distanceTo = 0 - get_parent().next() + # Store current position for distance calculation + var toCalculate = position + + # Process input if the character is active + if active: + get_input() + move_and_slide(velocity) # Move the character based on current velocity + + # Accumulate distance traveled + distanceTo += position.distance_to(toCalculate) + print(distanceTo) # Print distance traveled (for debugging) + # Check if distance threshold is exceeded and the character is active + if distanceTo > 500 and active: + stop() # Stop character movement and reset distance traveled + distanceTo = 0 + get_parent().next() # Trigger next action in parent node +# Function to start the character's activity func start(): - $camera.enabled = true - active = true - $stats.disabled = false - $stats.visible = true + $camera.enabled = true # Enable camera + active = true # Set character to active state + $stats.disabled = false # Enable stats UI node + $stats.visible = true # Make stats UI node visible +# Function to stop the character's activity func stop(): - $camera.enabled = false - active = false - $stats.disabled = true - $stats.visible = false + $camera.enabled = false # Disable camera + active = false # Set character to inactive state + $stats.disabled = true # Disable stats UI node + $stats.visible = false # Hide stats UI node +# Handler for when the stats button is pressed func _on_stats_pressed(): - get_parent().stats() + get_parent().stats() # Call stats function in the parent node diff --git a/scripts/save.gd b/scripts/save.gd index c3abd93..f23022c 100644 --- a/scripts/save.gd +++ b/scripts/save.gd @@ -1,12 +1,21 @@ extends Node +# Function to save data in JSON format to a specified path func saveJSON(savePath, saveData): + # Convert the saveData dictionary to a JSON string var jsonString = JSON.stringify(saveData) + # Attempt to open a file for writing at the specified savePath var fileAccess = FileAccess.open(savePath, FileAccess.WRITE) + + # Check if the file was successfully opened if not fileAccess: + # If not, print an error message with the reason for the failure and return an error code print("An error happened while saving data: ", FileAccess.get_open_error()) return 1 + # Write the JSON string to the file fileAccess.store_line(jsonString) + + # Close the file to ensure all data is properly saved and resources are freed fileAccess.close() diff --git a/testing/test/player.tscn b/testing/test/player.tscn index 943ffa7..23b7608 100644 --- a/testing/test/player.tscn +++ b/testing/test/player.tscn @@ -1,31 +1,41 @@ [gd_scene load_steps=4 format=3 uid="uid://dv67vdgb4h44q"] +# External resource for the player texture [ext_resource type="Texture2D" uid="uid://dheqglouhkis6" path="res://testing/testAssets/player.png" id="1_cujcj"] + +# External resource for the player script [ext_resource type="Script" path="res://scripts/player.gd" id="1_oik63"] +# Sub-resource for the collision shape of the player [sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"] -size = Vector2(20, 18) +size = Vector2(20, 18) # Setting the size of the RectangleShape2D +# Root node of the scene, a CharacterBody2D named "player" [node name="player" type="CharacterBody2D"] -script = ExtResource("1_oik63") +script = ExtResource("1_oik63") # Assigning the external script to the player node +# Child node of the player, a Sprite2D named "sprite" [node name="sprite" type="Sprite2D" parent="."] -texture = ExtResource("1_cujcj") +texture = ExtResource("1_cujcj") # Assigning the external texture to the sprite node +# Child node of the player, a CollisionShape2D named "CollisionShape2D" [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource("RectangleShape2D_kf6qt") +shape = SubResource("RectangleShape2D_kf6qt") # Assigning the rectangle shape as the collision shape +# Child node of the player, a Camera2D named "camera" [node name="camera" type="Camera2D" parent="."] -enabled = false +enabled = false # Camera is initially disabled +# Child node of the player, a Button named "stats" [node name="stats" type="Button" parent="."] -visible = false -offset_left = 112.0 -offset_top = 152.0 -offset_right = 238.0 -offset_bottom = 187.0 -scale = Vector2(2, 2) -disabled = true -text = "Stats" +visible = false # Button is initially invisible +offset_left = 112.0 # Left offset of the button +offset_top = 152.0 # Top offset of the button +offset_right = 238.0 # Right offset of the button +offset_bottom = 187.0 # Bottom offset of the button +scale = Vector2(2, 2) # Scaling the button by a factor of 2 +disabled = true # Button is initially disabled +text = "Stats" # Text displayed on the button +# Connecting the "pressed" signal of the "stats" button to the "_on_stats_pressed" method in the current node [connection signal="pressed" from="stats" to="." method="_on_stats_pressed"] From 39f6b1e24713eebe214b61ef13e74fe6370df22e Mon Sep 17 00:00:00 2001 From: Sage The DM Date: Tue, 18 Jun 2024 19:31:08 +0200 Subject: [PATCH 2/2] uncomented .tscn --- testing/test/player.tscn | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/testing/test/player.tscn b/testing/test/player.tscn index 23b7608..8a64c0f 100644 --- a/testing/test/player.tscn +++ b/testing/test/player.tscn @@ -1,41 +1,32 @@ [gd_scene load_steps=4 format=3 uid="uid://dv67vdgb4h44q"] -# External resource for the player texture [ext_resource type="Texture2D" uid="uid://dheqglouhkis6" path="res://testing/testAssets/player.png" id="1_cujcj"] -# External resource for the player script [ext_resource type="Script" path="res://scripts/player.gd" id="1_oik63"] -# Sub-resource for the collision shape of the player [sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"] -size = Vector2(20, 18) # Setting the size of the RectangleShape2D +size = Vector2(20, 18) -# Root node of the scene, a CharacterBody2D named "player" [node name="player" type="CharacterBody2D"] -script = ExtResource("1_oik63") # Assigning the external script to the player node +script = ExtResource("1_oik63") -# Child node of the player, a Sprite2D named "sprite" [node name="sprite" type="Sprite2D" parent="."] -texture = ExtResource("1_cujcj") # Assigning the external texture to the sprite node +texture = ExtResource("1_cujcj") -# Child node of the player, a CollisionShape2D named "CollisionShape2D" [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource("RectangleShape2D_kf6qt") # Assigning the rectangle shape as the collision shape +shape = SubResource("RectangleShape2D_kf6qt") -# Child node of the player, a Camera2D named "camera" [node name="camera" type="Camera2D" parent="."] -enabled = false # Camera is initially disabled +enabled = false -# Child node of the player, a Button named "stats" [node name="stats" type="Button" parent="."] -visible = false # Button is initially invisible -offset_left = 112.0 # Left offset of the button -offset_top = 152.0 # Top offset of the button -offset_right = 238.0 # Right offset of the button -offset_bottom = 187.0 # Bottom offset of the button -scale = Vector2(2, 2) # Scaling the button by a factor of 2 -disabled = true # Button is initially disabled -text = "Stats" # Text displayed on the button +visible = false +offset_left = 112.0 +offset_top = 152.0 +offset_right = 238.0 +offset_bottom = 187.0 +scale = Vector2(2, 2) +disabled = true +text = "Stats" -# Connecting the "pressed" signal of the "stats" button to the "_on_stats_pressed" method in the current node [connection signal="pressed" from="stats" to="." method="_on_stats_pressed"]