Skip to content

Commit 386e961

Browse files
committed
fix: Invocation time in first frame
1 parent e2659af commit 386e961

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

Assets/JCSUnity/Scripts/Actions/JCS_AdjustTimeTrigger.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
4141
[ReadOnly]
4242
private bool mDidAction = false;
4343

44+
[Separator("Initialize Variables (JCS_AdjustTimeTrigger)")]
45+
46+
[Tooltip("Run immediately on start.")]
47+
[SerializeField]
48+
private bool mRunImmediate = true;
49+
4450
[Separator("Runtime Variables (JCS_AdjustTimeTrigger)")]
4551

4652
[Tooltip("Is this component active?")]
@@ -67,6 +73,7 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
6773

6874
/* Setter & Getter */
6975

76+
public bool RunImmediate { get { return this.mRunImmediate; } set { this.mRunImmediate = value; } }
7077
public bool Active { get { return this.mActive; } set { this.mActive = value; } }
7178
public float TimeZone { get { return this.mTimeZone; } set { this.mTimeZone = value; } }
7279
public float AdjustTimeZone { get { return this.mAdjustTimeZone; } set { this.mAdjustTimeZone = value; } }
@@ -75,6 +82,13 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
7582

7683
/* Functions */
7784

85+
private void Start()
86+
{
87+
// Run immediately on the first frame.
88+
if (mRunImmediate)
89+
Invoke(nameof(ExecuteAction), JCS_Constants.FIRST_FRAME_INVOKE_TIME);
90+
}
91+
7892
private void Update()
7993
{
8094
if (!mActive)
@@ -109,14 +123,19 @@ private void DoAction()
109123
if (mRealTimeZone > mTimer)
110124
return;
111125

112-
// active actions.
113-
if (onAction != null)
114-
onAction.Invoke();
115-
116-
if (mOnAction != null)
117-
mOnAction.Invoke();
126+
ExecuteAction();
118127

119128
mDidAction = true;
120129
}
130+
131+
/// <summary>
132+
/// Execute the action.
133+
/// </summary>
134+
private void ExecuteAction()
135+
{
136+
onAction?.Invoke();
137+
138+
mOnAction?.Invoke();
139+
}
121140
}
122141
}

Assets/JCSUnity/Scripts/JCS_Constants.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ public static class JCS_Constants
1616
{
1717
/* Variables */
1818

19+
// The time delay to be invoked for the first frame.
20+
//
21+
// First frame invocation can't be set to 0; therefore, we set
22+
// to somthing really small. Generally has to be smaller than
23+
// the 1/60 frame time.
24+
//
25+
// See https://docs.unity3d.com/6000.1/Documentation/ScriptReference/MonoBehaviour.Invoke.html
26+
public const float FIRST_FRAME_INVOKE_TIME = 0.001f;
27+
1928
// The minimum friction value.
2029
public const float FRICTION_MIN = 0.01f;
2130

Assets/JCSUnity/Scripts/Managers/JCS_GameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void Awake()
6666

6767
SetSpecificGameTypeGameManager();
6868

69-
Invoke("OnFirstFrame", 0.0f);
69+
Invoke(nameof(OnFirstFrame), JCS_Constants.FIRST_FRAME_INVOKE_TIME);
7070
}
7171

7272
/// <summary>

docs/ScriptReference/JCS_Constants.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Place general game constant here.
44

55
## Variables
66

7-
| Name | Description |
8-
|:---------------|:-----------------------------------------------|
9-
| FRICTION_MIN | The minimum friction value. |
10-
| FRICTION | The default effect friction value. |
11-
| NEAR_THRESHOLD | Below this threshold is consider close enough. |
7+
| Name | Description |
8+
|:------------------------|:--------------------------------------------------|
9+
| FIRST_FRAME_INVOKE_TIME | The time delay to be invoked for the first frame. |
10+
| FRICTION_MIN | The minimum friction value. |
11+
| FRICTION | The default effect friction value. |
12+
| NEAR_THRESHOLD | Below this threshold is consider close enough. |

0 commit comments

Comments
 (0)