Skip to content

Commit 71ac122

Browse files
Input system events used in OnStart and OnDestroyed moved to OnEnabled and OnDisbaled #1221 (#1476)
* Implement input handling for toggling the runtime editor in OnEnable and OnDisable instead of Start function. * Refactor input handling to use OnEnable and OnDisable for attack actions * Refactor input handling to use OnEnabled and OnDisabled for input events * Refactor input handling in ConsolePanelView to use OnEnabled and OnDisabled for console actions * Refactor input handling in RadialInteractionSubSystem to use OnEnabled and OnDisabled for interaction events * Refactor input handling in CameraFollow to use OnAwake, OnEnabled, and OnDisabled for initialization and cleanup * Refactor input handling in GameScreensController to use OnEnabled and OnDisabled for menu toggle actions * Refactor input handling in SelectionCamera to use OnEnabled and OnDisabled for toggle debug mode * Refactor input handling in ConstructionHologramManager to use OnAwake, OnEnabled, and OnDisabled for initialization and cleanup * Refactor input handling in RagdollWhenPressingButton to use OnAwake, OnEnabled, and OnDisabled for initialization and cleanup * Refactor input handling in InteractionController to use OnAwake, OnEnabled, and OnDisabled for initialization and cleanup * Add function summary for EnableInput and DisableInput functions * Refactor input handling in HumanoidController to use OnAwake, OnEnabled, and OnDisabled for initialization and cleanup * Updated item prefabs' NetworkTransform's Client Authoritative property to false. * Revert "Updated item prefabs' NetworkTransform's Client Authoritative property to false." This reverts commit 6b1f00a. * Refactor input subscription logic in RagdollWhenPressingButton to handle ownership changes, and remove usage of coroutines. * Refactor input handling in HumanoidController to eliminate coroutines and manage input subscription based on ownership changes. * Refactor input handling in InteractionController to manage input subscription based on ownership changes, removing coroutine usage. * Update Assets/Scripts/SS3D/Hacks/RagdollWhenPressingButton.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Entities/Humanoid/HumanoidController.cs Added a comment to warn people why Awake shouldn't be removed Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Entities/Humanoid/HumanoidController.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Entities/Humanoid/HumanoidController.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Entities/Humanoid/HumanoidController.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Entities/Humanoid/HumanoidController.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Entities/Humanoid/HumanoidController.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Interactions/InteractionController.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> * Update Assets/Scripts/SS3D/Systems/Interactions/InteractionController.cs Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com> --------- Co-authored-by: iamteapot <77565416+iamteapot422@users.noreply.github.com>
1 parent 1b4ad79 commit 71ac122

12 files changed

Lines changed: 347 additions & 97 deletions

File tree

Assets/Scripts/External/RuntimeInspector/RuntimeEditorView.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DG.Tweening;
22
using SS3D.Core;
3+
using SS3D.Systems.Inputs;
34
using UnityEngine;
45
using UnityEngine.InputSystem;
56

@@ -20,11 +21,30 @@ public class RuntimeEditorView : MonoBehaviour
2021
// Start is called before the first frame update
2122
void Start()
2223
{
23-
SubSystems.Get<SS3D.Systems.Inputs.InputSubSystem>().Inputs.Other.ToggleRuntimeEditor.performed += HandleToggle;
2424
_hierarchyWidth = _runtimeHierarchy.sizeDelta.x;
2525
_inspectorWidth = _runtimeInspector.sizeDelta.x;
2626
}
2727

28+
private void OnEnable()
29+
{
30+
InputSubSystem inputSubSystem = SubSystems.Get<InputSubSystem>();
31+
32+
if (inputSubSystem)
33+
{
34+
inputSubSystem.Inputs.Other.ToggleRuntimeEditor.performed += HandleToggle;
35+
}
36+
}
37+
38+
private void OnDisable()
39+
{
40+
InputSubSystem inputSubSystem = SubSystems.Get<InputSubSystem>();
41+
42+
if (inputSubSystem)
43+
{
44+
inputSubSystem.Inputs.Other.ToggleRuntimeEditor.performed -= HandleToggle;
45+
}
46+
}
47+
2848
private void HandleToggle(InputAction.CallbackContext context)
2949
{
3050
_isShowed = !_isShowed;

Assets/Scripts/SS3D/Hacks/AttackBodyPartByClickingIt.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using UnityEngine;
55
using SS3D.Systems.Health;
6-
using System;
76
using System.Collections;
87
using UnityEngine.InputSystem;
98
using InputSubSystem = SS3D.Systems.Inputs.InputSubSystem;
@@ -34,14 +33,24 @@ public override void OnStartClient()
3433
if (!IsOwner) enabled = false;
3534
}
3635

37-
private void Start()
36+
private void OnEnable()
3837
{
39-
SubSystems.Get<InputSubSystem>().Inputs.Other.Attack.performed += CheckForAttack;
38+
InputSubSystem inputSubSystem = SubSystems.Get<InputSubSystem>();
39+
40+
if (inputSubSystem)
41+
{
42+
inputSubSystem.Inputs.Other.Attack.performed += CheckForAttack;
43+
}
4044
}
4145

42-
private void OnDestroy()
46+
private void OnDisable()
4347
{
44-
SubSystems.Get<InputSubSystem>().Inputs.Other.Attack.performed -= CheckForAttack;
48+
InputSubSystem inputSubSystem = SubSystems.Get<InputSubSystem>();
49+
50+
if (inputSubSystem)
51+
{
52+
inputSubSystem.Inputs.Other.Attack.performed -= CheckForAttack;
53+
}
4554
}
4655

4756
private void CheckForAttack(InputAction.CallbackContext callbackContext)

Assets/Scripts/SS3D/Hacks/RagdollWhenPressingButton.cs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SS3D.Core;
1+
using FishNet.Connection;
2+
using SS3D.Core;
23
using SS3D.Core.Behaviours;
34
using SS3D.Systems.Entities.Humanoid;
45
using SS3D.Systems.Inputs;
@@ -21,20 +22,68 @@ public class RagdollWhenPressingButton : NetworkActor
2122

2223
private Controls.OtherActions _controls;
2324

24-
public override void OnStartClient()
25+
public override void OnOwnershipClient(NetworkConnection prevOwner)
2526
{
26-
base.OnStartClient();
27-
if (!IsOwner) return;
27+
base.OnOwnershipClient(prevOwner);
2828

29-
_controls = SubSystems.Get<InputSubSystem>().Inputs.Other;
29+
if (IsOwner)
30+
{
31+
SubscribeToInput();
32+
}
33+
else if (prevOwner.Equals(LocalConnection))
34+
{
35+
UnsubscribeFromInput();
36+
}
37+
}
38+
39+
protected override void OnAwake()
40+
{
41+
base.OnAwake();
42+
43+
InputSubSystem inputSubSystem = SubSystems.Get<InputSubSystem>();
44+
45+
if (inputSubSystem)
46+
{
47+
_controls = inputSubSystem.Inputs.Other;
48+
}
49+
}
50+
51+
protected override void OnEnabled()
52+
{
53+
base.OnEnabled();
54+
55+
if (IsOwner)
56+
{
57+
SubscribeToInput();
58+
}
59+
}
60+
61+
protected override void OnDisabled()
62+
{
63+
base.OnDisabled();
64+
65+
if (IsOwner)
66+
{
67+
UnsubscribeFromInput();
68+
}
69+
}
70+
71+
private void SubscribeToInput()
72+
{
3073
_controls.Ragdoll.performed += HandleKnockdown;
3174
}
3275

76+
/// <summary>
77+
/// Unsubscribes from input events
78+
/// </summary>
79+
private void UnsubscribeFromInput()
80+
{
81+
_controls.Ragdoll.performed -= HandleKnockdown;
82+
}
3383

3484
private void HandleKnockdown(InputAction.CallbackContext context)
3585
{
3686
_ragdoll.Knockdown(_timeRagdolled);
3787
}
3888
}
39-
}
40-
89+
}

Assets/Scripts/SS3D/Systems/Entities/Humanoid/HumanoidController.cs

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using Coimbra.Services.Events;
22
using Coimbra.Services.PlayerLoopEvents;
3-
using System;
3+
using FishNet.Connection;
44
using SS3D.Core;
55
using SS3D.Core.Behaviours;
66
using SS3D.Systems.Inputs;
77
using SS3D.Systems.Screens;
8+
using System;
89
using UnityEngine;
910
using UnityEngine.InputSystem;
1011
using Actor = SS3D.Core.Behaviours.Actor;
@@ -58,14 +59,56 @@ public abstract class HumanoidController : NetworkActor
5859
public bool IsRunning => _isRunning;
5960
#endregion
6061

61-
protected override void OnStart()
62+
public override void OnOwnershipClient(NetworkConnection prevOwner)
63+
{
64+
base.OnOwnershipClient(prevOwner);
65+
66+
if (IsOwner)
67+
{
68+
SubscribeToInput();
69+
}
70+
else if (prevOwner.Equals(LocalConnection))
71+
{
72+
UnsubscribeFromInput();
73+
}
74+
}
75+
// Must have, Unity doesn't invoke Awake() in NetworkActor and therefore doesn't call OnAwake() without it
76+
77+
protected void Awake()
6278
{
63-
base.OnStart();
64-
if (!Owner.IsLocalClient) return;
79+
base.Awake();
80+
}
81+
82+
protected override void OnAwake()
83+
{
84+
base.OnAwake();
85+
6586
Setup();
6687
}
6788

68-
protected void Setup()
89+
protected override void OnEnabled()
90+
{
91+
base.OnEnabled();
92+
93+
if (IsOwner)
94+
{
95+
SubscribeToInput();
96+
}
97+
}
98+
99+
protected override void OnDisabled()
100+
{
101+
base.OnDisabled();
102+
103+
if (IsOwner)
104+
{
105+
UnsubscribeFromInput();
106+
}
107+
108+
TargetMovement = Vector3.zero;
109+
}
110+
111+
private void Setup()
69112
{
70113
_camera = SubSystems.Get<CameraSubSystem>().PlayerCamera;
71114
_entity.OnMindChanged += HandleControllingPlayerChanged;
@@ -76,25 +119,32 @@ protected void Setup()
76119

77120
MovementControls = controls.Movement;
78121
HotkeysControls = controls.Hotkeys;
79-
MovementControls.ToggleRun.performed += HandleToggleRun;
80-
81-
_inputSystem.ToggleActionMap(MovementControls, true);
82-
_inputSystem.ToggleActionMap(HotkeysControls, true);
83122

84123
AddHandle(UpdateEvent.AddListener(HandleUpdate));
85124
}
86125

87-
protected override void OnDisabled()
126+
private void SubscribeToInput()
88127
{
89-
base.OnDisabled();
90-
TargetMovement = Vector3.zero;
128+
if (!_inputSystem)
129+
{
130+
return;
131+
}
132+
133+
MovementControls.ToggleRun.performed += HandleToggleRun;
134+
135+
_inputSystem.ToggleActionMap(MovementControls, true);
136+
_inputSystem.ToggleActionMap(HotkeysControls, true);
91137
}
92138

93-
protected override void OnDestroyed()
139+
private void UnsubscribeFromInput()
94140
{
95-
base.OnDestroyed();
96-
UnityEngine.Debug.Log("destroying controller " + gameObject.name);
141+
if (!_inputSystem)
142+
{
143+
return;
144+
}
145+
97146
MovementControls.ToggleRun.performed -= HandleToggleRun;
147+
98148
_inputSystem.ToggleActionMap(MovementControls, false);
99149
_inputSystem.ToggleActionMap(HotkeysControls, false);
100150
}

Assets/Scripts/SS3D/Systems/Gamemodes/UI/GamemodeObjectivePanelView.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public class GamemodeObjectivePanelView : Actor
2626
protected override void OnAwake()
2727
{
2828
base.OnAwake();
29+
30+
InputSubSystem inputSystem = SubSystems.Get<InputSubSystem>();
31+
32+
if (inputSystem)
33+
{
34+
_controls = inputSystem.Inputs.Other;
35+
}
2936

3037
_gamemodeObjectiveItems = new Dictionary<int, GamemodeObjectiveItemView>();
3138

@@ -37,14 +44,19 @@ protected override void OnStart()
3744
base.OnStart();
3845

3946
_fade.SetFade(false);
40-
_controls = SubSystems.Get<InputSubSystem>().Inputs.Other;
47+
}
48+
49+
protected override void OnEnabled()
50+
{
51+
base.OnEnabled();
52+
4153
_controls.Fade.performed += HandleFadePerformed;
4254
_controls.Fade.canceled += HandleFadeCanceled;
4355
}
44-
45-
protected override void OnDestroyed()
56+
57+
protected override void OnDisabled()
4658
{
47-
base.OnDestroyed();
59+
base.OnDisabled();
4860

4961
_controls.Fade.performed -= HandleFadePerformed;
5062
_controls.Fade.canceled -= HandleFadeCanceled;

Assets/Scripts/SS3D/Systems/IngameConsoleSystem/ConsolePanelView.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,43 @@ protected override void OnStart()
5454
_inputSystem = SubSystems.Get<InputSubSystem>();
5555
_controls = _inputSystem.Inputs;
5656
_consoleControls = _controls.Console;
57-
_consoleControls.Close.performed += HandleClose;
58-
_consoleControls.Open.performed += HandleOpen;
59-
_consoleControls.SwitchCommand.performed += HandleSwitchCommand;
60-
_consoleControls.Submit.performed += HandleSubmit;
6157
_inputSystem.ToggleAction(_consoleControls.Open, true);
6258

6359
AddHandle(UpdateEvent.AddListener(HandleUpdate));
6460
}
6561

66-
protected override void OnDestroyed()
62+
protected override void OnEnabled()
6763
{
68-
base.OnDestroyed();
69-
70-
_consoleControls.Close.performed -= HandleClose;
71-
_consoleControls.Open.performed -= HandleOpen;
72-
_consoleControls.SwitchCommand.performed -= HandleSwitchCommand;
73-
_consoleControls.Submit.performed -= HandleSubmit;
64+
base.OnEnabled();
65+
66+
InputSubSystem inputSystem = SubSystems.Get<InputSubSystem>();
67+
68+
if (inputSystem && inputSystem.Inputs != null)
69+
{
70+
Controls.ConsoleActions consoleInputs = inputSystem.Inputs.Console;
71+
72+
consoleInputs.Close.performed += HandleClose;
73+
consoleInputs.Open.performed += HandleOpen;
74+
consoleInputs.SwitchCommand.performed += HandleSwitchCommand;
75+
consoleInputs.Submit.performed += HandleSubmit;
76+
}
77+
}
78+
79+
protected override void OnDisabled()
80+
{
81+
base.OnDisabled();
82+
83+
InputSubSystem inputSystem = SubSystems.Get<InputSubSystem>();
84+
85+
if (inputSystem && inputSystem.Inputs != null)
86+
{
87+
Controls.ConsoleActions consoleInputs = inputSystem.Inputs.Console;
88+
89+
consoleInputs.Close.performed -= HandleClose;
90+
consoleInputs.Open.performed -= HandleOpen;
91+
consoleInputs.SwitchCommand.performed -= HandleSwitchCommand;
92+
consoleInputs.Submit.performed -= HandleSubmit;
93+
}
7494
}
7595

7696
private void HandleUpdate(ref EventContext context, in UpdateEvent updateEvent)

0 commit comments

Comments
 (0)