SS3D icon indicating copy to clipboard operation
SS3D copied to clipboard

Consumables script causes the game to crash

Open EikoBiko opened this issue 2 years ago • 1 comments

Summary

Attempting to utilize a consumable item (such as drinks) results in the game crashing.

Current Behavior

Attempting to drink any of the soda items results in a crash, due to the consumables script creating a null reference error.

Steps to Reproduce

Attempt to drink a soda by picking one up and clicking on yourself.

Expected Behavior

The soda should empty some amount of its contents into the target -- in this case, the player, and play a drinking sound.

Possible Solution

Issue is probably coming from some Network-related shenanigans. The error cites Assets/Engine/Interactions/InteractionSourceNetworkBehaviour.cs:101 as related to the problem. Someone who has experience with the networking code should check this out.

EikoBiko avatar May 12 '22 14:05 EikoBiko

@EikoBiko I think that the issue here is that the itemInHand variable isn't correctly set. It appears to me that the source is always going to be the player who initiates (the John Doe object), who doesn't have a Consumable component attached. The behaviour you have described can be resolved simply be changing the itemInHand assignment to use GetComponentInChildren rather than GetComponent, because the soda can in the players hand is within the John Doe hierarchy.

Relevant code excerpt in Consumable.cs:

protected override void StartDelayed(InteractionEvent interactionEvent)
{
     GameObject source = interactionEvent.Source?.GetComponentInTree<Entity>().gameObject;
     GameObject target = interactionEvent.Target.GetComponent<Transform>().gameObject;            
     Consumable itemInHand = source.GetComponent<Consumable>(); // <<<<< This should search children.

     // Item in hand and interacting with origin
     if (target == null) 
     {
         itemInHand.ConsumeAction(source);
     }
     // Item not in hand
     Consumable targetedItem = target.GetComponent<Consumable>();
     if (targetedItem)
     {
         targetedItem.ConsumeAction(source);
     }
     // Item in hand and interacting with other player
     if (target.GetComponent<Entity>())
     {
         itemInHand.ConsumeAction(source, target);  // <<<<< This is where the error occurs.
     }
}

Ryan089 avatar Jun 08 '22 11:06 Ryan089