Skip to content

Commit

Permalink
fix(Interaction): make force stop interactions wait for end of frame
Browse files Browse the repository at this point in the history
Previously, it was possible to get the force stop interactions into an
infinite loop by calling `ForceStopTouching` or `ForceStopInteracting`
from within an event listener from the Stop Using event which would
in turn could emit the Stop Using event again and therefore getting
stuck in an infinite loop.

The fix now ensures that any interactable object force stop events
are done at the end of the frame giving the controller scripts time
to correctly clean up.
  • Loading branch information
thestonefox committed Aug 19, 2016
1 parent 88dcf8e commit 6a94c07
Showing 1 changed file with 50 additions and 33 deletions.
83 changes: 50 additions & 33 deletions Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractableObject.cs
Expand Up @@ -11,6 +11,7 @@
namespace VRTK
{
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public struct InteractableObjectEventArgs
Expand Down Expand Up @@ -200,7 +201,10 @@ public virtual void StopTouching(GameObject previousTouchingObject)
{
OnInteractableObjectUntouched(SetInteractableObjectEvent(previousTouchingObject));
touchingObject = null;
StopUsingOnControllerChange(previousTouchingObject);
if(gameObject.activeInHierarchy)
{
StartCoroutine(StopUsingOnControllerChange(previousTouchingObject));
}
}

public virtual void Grabbed(GameObject currentGrabbingObject)
Expand All @@ -224,22 +228,9 @@ public virtual void Ungrabbed(GameObject previousGrabbingObject)
grabbedSnapHandle = null;
grabbingObject = null;
LoadPreviousState();
StopUsingOnControllerChange(previousGrabbingObject);
}

private void StopUsingOnControllerChange(GameObject previousController)
{
var usingObject = previousController.GetComponent<VRTK_InteractUse>();
if (usingObject)
if (gameObject.activeInHierarchy)
{
if (holdButtonToUse)
{
usingObject.ForceStopUsing();
}
else
{
usingObject.ForceResetUsing();
}
StartCoroutine(StopUsingOnControllerChange(previousGrabbingObject));
}
}

Expand Down Expand Up @@ -370,24 +361,9 @@ public bool IsValidInteractableController(GameObject actualController, AllowedCo

public void ForceStopInteracting()
{
if (touchingObject != null && touchingObject.activeInHierarchy)
{
touchingObject.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
forcedDropped = true;
}

if (grabbingObject != null && grabbingObject.activeInHierarchy)
{
grabbingObject.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
grabbingObject.GetComponent<VRTK_InteractGrab>().ForceRelease();
forcedDropped = true;
}

if (usingObject != null && usingObject.activeInHierarchy)
if (gameObject.activeInHierarchy)
{
usingObject.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
usingObject.GetComponent<VRTK_InteractUse>().ForceStopUsing();
forcedDropped = true;
StartCoroutine(ForceStopInteractingAtEndOfFrame());
}
}

Expand Down Expand Up @@ -691,5 +667,46 @@ private void OnTeleported(object sender, DestinationMarkerEventArgs e)
transform.position = grabbingObject.transform.position;
}
}

private IEnumerator StopUsingOnControllerChange(GameObject previousController)
{
yield return new WaitForEndOfFrame();
var usingObject = previousController.GetComponent<VRTK_InteractUse>();
if (usingObject)
{
if (holdButtonToUse)
{
usingObject.ForceStopUsing();
}
else
{
usingObject.ForceResetUsing();
}
}
}

private IEnumerator ForceStopInteractingAtEndOfFrame()
{
yield return new WaitForEndOfFrame();
if (touchingObject != null && touchingObject.activeInHierarchy)
{
touchingObject.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
forcedDropped = true;
}

if (grabbingObject != null && grabbingObject.activeInHierarchy)
{
grabbingObject.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
grabbingObject.GetComponent<VRTK_InteractGrab>().ForceRelease();
forcedDropped = true;
}

if (usingObject != null && usingObject.activeInHierarchy)
{
usingObject.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
usingObject.GetComponent<VRTK_InteractUse>().ForceStopUsing();
forcedDropped = true;
}
}
}
}

0 comments on commit 6a94c07

Please sign in to comment.