diff --git a/player_variables.cs b/player_variables.cs new file mode 100644 index 0000000..d644b44 --- /dev/null +++ b/player_variables.cs @@ -0,0 +1,16 @@ +using Godot; +using System; +using System.Text.RegularExpressions; + +public partial class player_variables : Node +{ + public static string PlayerName = "Yannik"; + + public static void ClearPlayerName() //normal getter setter crashes for some reason + { + PlayerName = Regex.Replace(PlayerName, @"\[[^]]+\]", ""); + PlayerName = Regex.Replace(PlayerName, @"<[^>]*>", ""); + if (PlayerName.Length > 12) + PlayerName = PlayerName.Substring(0, 12); + } +} diff --git a/project.godot b/project.godot index ed9090f..df7c6a8 100644 --- a/project.godot +++ b/project.godot @@ -17,7 +17,8 @@ config/icon="res://assets/textures/debug/dummy-player-normal.png" [autoload] -Essential="*res://src/essential.cs" +Essential="res://src/essential.cs" +PlayerVariables="*res://player_variables.cs" [display] diff --git a/scenes/overworld.tscn b/scenes/overworld.tscn index 7579988..d1750b8 100644 --- a/scenes/overworld.tscn +++ b/scenes/overworld.tscn @@ -44,7 +44,6 @@ layer_1/tile_data = PackedInt32Array(-262141, 0, 0, -196605, 0, 0, -196604, 0, 0 metadata/_edit_lock_ = true [node name="player" parent="." instance=ExtResource("2_0skrb")] -playerName = "Yannik" [node name="main_cam" parent="player" index="2"] limit_left = -1791 diff --git a/src/scene-scripts/console/console.cs b/src/scene-scripts/console/console.cs index cb2ff70..bd6331f 100644 --- a/src/scene-scripts/console/console.cs +++ b/src/scene-scripts/console/console.cs @@ -28,7 +28,7 @@ public partial class console : PopupPanel public void OnLineEditTextSubmitted(string command) { line.Clear(); - if (command.Length != 0) textblock.AddText(GetParent().playerName + " > " + command + "\n"); + if (command.Length != 0) textblock.AddText(player_variables.PlayerName + " > " + command + "\n"); Variant args; if (command.Split(' ').Length == 2 && commandDict.ContainsKey(command.Split(' ')[0].ToLower())) { @@ -102,12 +102,9 @@ public partial class console : PopupPanel } public void playername(string name) { - if (GetParent().Name == "player") - { - GetParent().playerName = name; - GetParent().ClearPlayerName(); - textblock.AddText("Your new name is now: " + GetParent().playerName + "\n"); - } + player_variables.PlayerName = name; + player_variables.ClearPlayerName(); + textblock.AddText("Your new name is now: " + player_variables.PlayerName + "\n"); } public void reload() => GetTree().ReloadCurrentScene(); } diff --git a/src/scene-scripts/player.cs b/src/scene-scripts/player.cs index 357aac3..46002cb 100644 --- a/src/scene-scripts/player.cs +++ b/src/scene-scripts/player.cs @@ -4,8 +4,6 @@ using System.Text.RegularExpressions; public partial class player : CharacterBody2D { - - [Export] public string playerName; [Export] public float speed = 1; public bool allowMovement = true; public Vector2 movement; @@ -15,18 +13,10 @@ public partial class player : CharacterBody2D public override void _Ready() { - ClearPlayerName(); animatedSprite = GetNode("animated_sprite_2d"); rotCenter = GetNode("rotation_center"); dialogRayCast = GetNode("rotation_center/ray_cast_2d"); } - public void ClearPlayerName() - { - playerName = Regex.Replace(playerName, @"\[[^]]+\]", ""); - playerName = Regex.Replace(playerName, @"<[^>]*>", ""); - if (playerName.Length > 12) - playerName = playerName.Substring(0, 12); - } public override void _PhysicsProcess(double delta) { if (allowMovement) movement = Input.GetVector("move_left", "move_right", "move_up", "move_down"); @@ -38,7 +28,7 @@ public partial class player : CharacterBody2D { if (Input.IsActionJustPressed("ui_accept") && dialogRayCast.IsColliding() && allowMovement) GetNode("dialog_bubble").GetDialog(dialogRayCast.GetCollider().Get("file").AsString(), dialogRayCast.GetCollider().Get("title").AsString(), - (Area2D)dialogRayCast.GetCollider(), playerName, dialogRayCast.GetCollider().Get("introducedVillager").AsBool()); + (Area2D)dialogRayCast.GetCollider(), player_variables.PlayerName, dialogRayCast.GetCollider().Get("introducedVillager").AsBool()); //animation system (with controller support wich cant get normalized vector) if (allowMovement == false)