Skip to content

Commit

Permalink
Fixed bug in retrieving the player menu.
Browse files Browse the repository at this point in the history
The PlayerMenu is actually not attached to the game object representing
the local player but to another game object that is a child of the
local player (e.g., DesktopPlayer in a desktop environment).
Now we retrieve the player menu by way of the new method LocalPlayer.TryGetPlayerMenu.
  • Loading branch information
koschke committed Oct 12, 2023
1 parent cfcc4f7 commit 29439b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
28 changes: 27 additions & 1 deletion Assets/SEE/Game/LocalPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using SEE.GO.Menu;
using UnityEngine;

namespace SEE.Game
{
Expand All @@ -13,5 +14,30 @@ public static class LocalPlayer
/// executing on this local instance of Unity.
/// </summary>
public static GameObject Instance;

/// <summary>
/// Returns the <see cref="PlayerMenu"/> attached to the local player <see cref="Instance"/>
/// or any of its descendants (including inactive ones).
/// </summary>
/// <param name="playerMenu">the resulting <see cref="PlayerMenu"/>; <c>null</c> if none
/// could be found</param>
/// <returns>true if a <see cref="PlayerMenu"/> could be found</returns>
internal static bool TryGetPlayerMenu(out PlayerMenu playerMenu)
{
if (Instance == null)
{
Debug.LogError($"Local player is null'.\n");
playerMenu = null;
return false;
}
playerMenu = Instance.GetComponentInChildren<PlayerMenu>(includeInactive: true);
if (playerMenu == null)
{
Debug.LogError($"Couldn't find component '{nameof(PlayerMenu)}' "
+ $"on local player named '{Instance.name}'.\n");
return false;
}
return true;
}
}
}
4 changes: 2 additions & 2 deletions Assets/SEE/GameObjects/Menu/PlayerMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ private void Update()
/// <summary>
/// Sets the currently selected menu entry in PlayerMenu to the action with given <paramref name="actionName"/>.
/// </summary>
/// <param name="actionName">name of the menu entry to be </param>
/// <param name="actionName">name of the menu entry to be set</param>
private static void SetPlayerMenu(string actionName)
{
if (LocalPlayer.Instance.TryGetComponentOrLog(out PlayerMenu playerMenu))
if (LocalPlayer.TryGetPlayerMenu(out PlayerMenu playerMenu))
{
// We cannot use PlayerActionHistory.Current here
playerMenu.modeMenu.ActiveEntry = playerMenu.modeMenu.Entries.First(x => x.Title.Equals(actionName));
Expand Down

0 comments on commit 29439b1

Please sign in to comment.