Skip to content

Commit 188a883

Browse files
committed
fix(Interaction): prevent double unuse event
The Interact Use script was emitting two unuse events because the Interact Use script calls StopUsing on the Interactable Object and the Interactable Object script calls ForceResetUsing on the Interact Use script, which would then in turn emit the unuse event before the original StopUsing routine had finished. This would result in two unuse events being emitted. The fix for this is to add another check into the Interactable Object StopUsing method that can be used to bypass the ForceResetUsing call as it's not needed to reset the Interact Use object if the StopUsing on the Interact Use has been called as it will already reset the state anyway.
1 parent 896231d commit 188a883

File tree

7 files changed

+27
-23
lines changed

7 files changed

+27
-23
lines changed

Assets/VRTK/Examples/ExampleResources/Scripts/FireExtinguisher_Base.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace VRTK.Examples
22
{
3-
using System;
43
using UnityEngine;
54

65
public class FireExtinguisher_Base : VRTK_InteractableObject
@@ -10,15 +9,15 @@ public class FireExtinguisher_Base : VRTK_InteractableObject
109

1110
private VRTK_ControllerEvents controllerEvents;
1211

13-
public override void StartUsing(VRTK_InteractUse usingObject)
12+
public override void StartUsing(VRTK_InteractUse currentUsingObject = null)
1413
{
15-
base.StartUsing(usingObject);
16-
controllerEvents = usingObject.GetComponent<VRTK_ControllerEvents>();
14+
base.StartUsing(currentUsingObject);
15+
controllerEvents = currentUsingObject.GetComponent<VRTK_ControllerEvents>();
1716
}
1817

19-
public override void StopUsing(VRTK_InteractUse previousUsingObject)
18+
public override void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
2019
{
21-
base.StopUsing(previousUsingObject);
20+
base.StopUsing(previousUsingObject, resetUsingObjectState);
2221
controllerEvents = null;
2322
}
2423

Assets/VRTK/Examples/ExampleResources/Scripts/LightSaber.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ public class LightSaber : VRTK_InteractableObject
1414
private Color targetColor;
1515
private Color[] bladePhaseColors;
1616

17-
public override void StartUsing(VRTK_InteractUse usingObject)
17+
public override void StartUsing(VRTK_InteractUse currentUsingObject = null)
1818
{
19-
base.StartUsing(usingObject);
19+
base.StartUsing(currentUsingObject);
2020
beamExtendSpeed = 5f;
2121
bladePhaseColors = new Color[2] { Color.blue, Color.cyan };
2222
activeColor = bladePhaseColors[0];
2323
targetColor = bladePhaseColors[1];
2424
}
2525

26-
public override void StopUsing(VRTK_InteractUse usingObject)
26+
public override void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
2727
{
28-
base.StopUsing(usingObject);
28+
base.StopUsing(previousUsingObject, resetUsingObjectState);
2929
beamExtendSpeed = -5f;
3030
}
3131

Assets/VRTK/Examples/ExampleResources/Scripts/UseRotate.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public class UseRotate : VRTK_InteractableObject
2020

2121
private float spinSpeed = 0f;
2222

23-
public override void StartUsing(VRTK_InteractUse usingObject)
23+
public override void StartUsing(VRTK_InteractUse currentUsingObject = null)
2424
{
25-
base.StartUsing(usingObject);
25+
base.StartUsing(currentUsingObject);
2626
spinSpeed = activeSpinSpeed;
2727
}
2828

29-
public override void StopUsing(VRTK_InteractUse usingObject)
29+
public override void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
3030
{
31-
base.StopUsing(usingObject);
31+
base.StopUsing(previousUsingObject, resetUsingObjectState);
3232
spinSpeed = idleSpinSpeed;
3333
}
3434

Assets/VRTK/Examples/ExampleResources/Scripts/Whirlygig.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ public class Whirlygig : VRTK_InteractableObject
77
float spinSpeed = 0f;
88
Transform rotator;
99

10-
public override void StartUsing(VRTK_InteractUse usingObject)
10+
public override void StartUsing(VRTK_InteractUse currentUsingObject = null)
1111
{
12-
base.StartUsing(usingObject);
12+
base.StartUsing(currentUsingObject);
1313
spinSpeed = 360f;
1414
}
1515

16-
public override void StopUsing(VRTK_InteractUse usingObject)
16+
public override void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
1717
{
18-
base.StopUsing(usingObject);
18+
base.StopUsing(previousUsingObject, resetUsingObjectState);
1919
spinSpeed = 0f;
2020
}
2121

Assets/VRTK/Scripts/Interactions/VRTK_InteractUse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ protected virtual void UnuseInteractedObject(bool completeStop)
354354
VRTK_InteractableObject usingObjectCheck = usingObject.GetComponent<VRTK_InteractableObject>();
355355
if (usingObjectCheck != null && completeStop)
356356
{
357-
usingObjectCheck.StopUsing(this);
357+
usingObjectCheck.StopUsing(this, false);
358358
}
359359
ToggleControllerVisibility(true);
360360
OnControllerUnuseInteractableObject(interactTouch.SetControllerInteractEvent(usingObject));

Assets/VRTK/Scripts/Interactions/VRTK_InteractableObject.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,15 @@ public virtual void StartUsing(VRTK_InteractUse currentUsingObject = null)
421421
/// The StopUsing method is called automatically when the object has stopped being used. It is also a virtual method to allow for overriding in inherited classes.
422422
/// </summary>
423423
/// <param name="previousUsingObject">The object that was previously using this object.</param>
424-
public virtual void StopUsing(VRTK_InteractUse previousUsingObject = null)
424+
/// <param name="resetUsingObjectState">Resets the using object state to reset it's using action.</param>
425+
public virtual void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
425426
{
426427
GameObject previousUsingGameObject = (previousUsingObject != null ? previousUsingObject.gameObject : null);
427428
OnInteractableObjectUnused(SetInteractableObjectEvent(previousUsingGameObject));
428-
ResetUsingObject();
429+
if (resetUsingObjectState)
430+
{
431+
ResetUsingObject();
432+
}
429433
usingState = 0;
430434
usingObject = null;
431435
}

DOCUMENTATION.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,12 +3456,13 @@ The Ungrabbed method is called automatically when the object has stopped being g
34563456

34573457
The StartUsing method is called automatically when the object is used initially. It is also a virtual method to allow for overriding in inherited classes.
34583458

3459-
#### StopUsing/1
3459+
#### StopUsing/2
34603460

3461-
> `public virtual void StopUsing(VRTK_InteractUse previousUsingObject = null)`
3461+
> `public virtual void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)`
34623462

34633463
* Parameters
34643464
* `VRTK_InteractUse previousUsingObject` - The object that was previously using this object.
3465+
* `bool resetUsingObjectState` - Resets the using object state to reset it's using action.
34653466
* Returns
34663467
* _none_
34673468

0 commit comments

Comments
 (0)