#Mutual Interaction System

10 messages · Page 1 of 1 (latest)

frigid ivy
#

How do you handle mutual interactions? Lets assume we created **IInteractable interface with Interact(Player player); ** and we call it in Player script OnTrigger and pass the reference as itself.
One thing I generally have dependency problems is, Once your interaction also affects your player, too. Lets assume we are interacting with chest object. For example when we want to do something to player when chest is interacted.You need to call player.SetAnimation(chestAnim) in chest Interactable; That is feel like holding your right ear with your left hand by putting your hand above your head. Its like;
Player calls Interact method, Chest implements it, chest calls player's reaction to chest. How do you handle mutual interactions like that by both interactor and interacted object is affected?

    public interface IInteractable
    {
        public void Interact(Player player);
    }

    public class Chest : IInteractable
    {
        public void Interact(Player player)
        {
            GiveLoot();
            player.PlayChestAnim(chestAnimKey);
        }
    }

    public class Player
    {
        /// <summary>
        /// On Trigger or smth.
        /// </summary>
        public void InteractWith()
        {
            interactable.Interact(this);
        }
    }```
covert junco
#

Maybe all interactables also call a method called player.InteractedWith(type)

#

then the player can decide how to respond

#

or the player somehow fetches the type from the interaction

covert junco
atomic ravine
#

Alternatively, you could make the Interact method return something that would indicate what the player is interacting with to trigger the proper animation from the player script.

public interface IInteractable
{
    public enum InteractionType { Chest, Door, Etc }
    public InteractionType Interact(); //Player is no longer referenced
}

    public class Chest : IInteractable
    {
        public InteractionType Interact()
        {
            GiveLoot();
            return InteractionType.Chest; //return the type
        }
    }

    public class Player
    {
        /// <summary>
        /// On Trigger or smth.
        /// </summary>
        public void InteractWith()
        {
            var type = interactable.Interact();
            switch(type)
            {
                case InteractionType.Chest:
                  player.PlayChestAnim(chestAnimKey); //Call the proper animation
                  break;
                //More options for other cases
            }
        }
    }
covert junco
#

lol that's what I proposed just better written

frigid ivy
#

Well, that is a good idea, but its really against Open-Closed Principle. Everytime we create a new interaction, we need enum + go in player and extend switch case. And its not only animation ,we can do completely other stuff to player

covert junco
#

So you propose creating a bunch of classes to deal with each kind of interaction instead?

#

@frigid ivy