I am destroying any instances of GameObject.Find() to save the world (#5)

lgtm
Co-authored-by: Nympfonic <arys.steam@gmail.com>
Co-committed-by: Nympfonic <arys.steam@gmail.com>
This commit is contained in:
Nympfonic 2024-04-08 07:09:43 +00:00 committed by Terkoiz
parent 4839e050f8
commit ac56754db2
3 changed files with 13 additions and 18 deletions

View File

@ -13,7 +13,7 @@ namespace Terkoiz.Freecam
private GameObject _mainCamera; private GameObject _mainCamera;
private Freecam _freeCamScript; private Freecam _freeCamScript;
private BattleUIScreen _playerUi; private EftBattleUIScreen _playerUi;
private bool _uiHidden; private bool _uiHidden;
private GamePlayerOwner _gamePlayerOwner; private GamePlayerOwner _gamePlayerOwner;
@ -24,14 +24,22 @@ namespace Terkoiz.Freecam
[UsedImplicitly] [UsedImplicitly]
public void Start() public void Start()
{ {
// Find Main Camera // Get Main Camera
_mainCamera = GameObject.Find("FPS Camera"); _mainCamera = GetLocalPlayerFromWorld().GetComponent<PlayerCameraController>().Camera.gameObject;
if (_mainCamera == null) if (_mainCamera == null)
{ {
FreecamPlugin.Logger.LogError("Failed to locate main camera"); FreecamPlugin.Logger.LogError("Failed to locate main camera");
return; return;
} }
// Get Player UI
_playerUi = Singleton<CommonUI>.Instance.EftBattleUIScreen;
if (_playerUi == null)
{
FreecamPlugin.Logger.LogError("Failed to locate player UI");
return;
}
// Add Freecam script to main camera in scene // Add Freecam script to main camera in scene
_freeCamScript = _mainCamera.AddComponent<Freecam>(); _freeCamScript = _mainCamera.AddComponent<Freecam>();
if (_freeCamScript == null) if (_freeCamScript == null)
@ -124,18 +132,6 @@ namespace Terkoiz.Freecam
if (GetLocalPlayerFromWorld() == null) if (GetLocalPlayerFromWorld() == null)
return; return;
// If we don't have the UI Component cached, go look for it in the scene
if (_playerUi == null)
{
_playerUi = GameObject.Find("BattleUIScreen").GetComponent<BattleUIScreen>();
if (_playerUi == null)
{
FreecamPlugin.Logger.LogError("Failed to locate player UI");
return;
}
}
_playerUi.gameObject.SetActive(_uiHidden); _playerUi.gameObject.SetActive(_uiHidden);
_uiHidden = !_uiHidden; _uiHidden = !_uiHidden;
} }
@ -240,7 +236,6 @@ namespace Terkoiz.Freecam
{ {
// Destroy FreeCamScript before FreeCamController if exists // Destroy FreeCamScript before FreeCamController if exists
Destroy(_freeCamScript); Destroy(_freeCamScript);
Destroy(this);
} }
} }
} }

View File

@ -7,7 +7,7 @@ using KeyboardShortcut = BepInEx.Configuration.KeyboardShortcut;
namespace Terkoiz.Freecam namespace Terkoiz.Freecam
{ {
[BepInPlugin("com.terkoiz.freecam", "Terkoiz.Freecam", "1.4.2")] [BepInPlugin("com.terkoiz.freecam", "Terkoiz.Freecam", "1.4.3")]
public class FreecamPlugin : BaseUnityPlugin public class FreecamPlugin : BaseUnityPlugin
{ {
internal new static ManualLogSource Logger { get; private set; } internal new static ManualLogSource Logger { get; private set; }

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net472</TargetFramework> <TargetFramework>net471</TargetFramework>
<Version>1.4.2</Version> <Version>1.4.2</Version>
<Authors>Terkoiz, Kobrakon, CWX</Authors> <Authors>Terkoiz, Kobrakon, CWX</Authors>
<RepositoryUrl>https://dev.sp-tarkov.com/Terkoiz/Freecam</RepositoryUrl> <RepositoryUrl>https://dev.sp-tarkov.com/Terkoiz/Freecam</RepositoryUrl>