Calling all functions with same name

Hi, so I’m making this game and I want to use an interface which I called “IGameStates” it holds pause, continue, die, and restart. Now, I want to implement these functions in different scripts and call all of them at the same time for example when the player dies, all scripts with the Die function will be called. I’m sorry for my bad England :smiley: and thanks in advance.

A simple, entry-level way to do this would be to use a centralized manager and OOP to do your lifting (as pointed out by @NoDumbQuestion) .

For a more elegant solution, you might want to look into events. Reflection can simplify your problem significantly, but it tends to make things difficult to read and I’m too ignorant to know how well it’s supported across platforms.

The interface:

public interface IGameStates
{
    void Die();
}

A poorly scaling “manager” example:

public class GameStateManager
{
    public static List<GameStateBehaviour> subscribers;
    public static List<GameStateBehaviour> Subscribers
    {
        get 
        {
            if (subscribers == null)
                subscribers = new List<GameStateBehaviour>();
            
            return subscribers;
        }
    }

    public static void Attach(GameStateBehaviour behaviour)
    {
        if (Subscribers.Contains(behaviour) == false)
            Subscribers.Add(behaviour);
    }

    public static void Detatch(GameStateBehaviour behaviour)
    {
        if (Subscribers.Contains(behaviour))
            Subscribers.Remove(behaviour);
    }

    public static void EmitDie()
    {
        for (int k=0; k<Subscribers.Count; k++)
        {
            Subscribers[k].Die();
        }
    }
}

Your behaviour:

public class GameStateBehaviour : MonoBehaviour, IGameStates
{
    protected internal void Awake() 
    {
        GameStateManager.Attach(this);
    }

    public void Die()
    {
        Debug.LogFormat("{0} received a death notification", this.name);
    }

    protected internal void OnDestroy() 
    {
        GameStateManager.Detatch(this);
    }
}

Some example listener:

public class GameStateListener : GameStateBehaviour 
{
	new protected internal void Awake()
    {
        // Allow the initial subscription to happen.
        base.Awake();
    }
}

Then some way of checking if it works:

public class GameStateExample : MonoBehaviour
{
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameStateManager.EmitDie();
        }
    }
}

In my example, I just made attached a GameStateExample to an empty object with 7 GameStateListeners. Pressing spacebar should produce 7 messages, which will be reduced / increased if you want to delete / copy them while the scene is running.

Make it Static.

Hello,

the solution for your problem are delegates.

 public interface IGameState
    {
        void Die();
    }
    
    public class Foo : IGameState
    {
        public void Die()
        {
         ...
        }
    }
    
    public class PlayerController : MonoBehaviour
    {
        public  event Action OnPlayerDied = delegate {  };
        public Foo foo;
        
        private void Awake()
        {
            PlayerDied += foo.Die;
        }
    
        private void PlayerDied()
        {
            ...
            PlayerDied.Invoke();
        }
    }

This way, when method PlayerDied is used, all methods that were registered to the Action OnPlayerDied will be invoked.

All the best,

The Knights of Unity