Giter Site home page Giter Site logo

gameeventbus's Introduction

GameEventBus

A simple EventBus library in C# for Unity

Basic Usage

// Create a unique class for each type of event
class PlayerJumpEvent : EventBase {
  public Vector3 jumpPosition;

  public PlayerMoveEvent(Vector3 pos) {
    jumpPosition = pos;
  }
}

private void TestMethod() {
  IEventBus eventBus = new EventBus();
  eventBus.Subscribe<PlayerJumpEvent>(OnPlayerJump); 
   
  eventBus.Publish(new PlayerJumpEvent(new Vector3(1,1,0)));// OnPlayerJump will be invoked

  eventBus.Unsubscribe(OnPlayerJump);
}

private void OnPlayerJump(PlayerJumpEvent event) {
  Console.WriteLine(playerJumpEvent.newPosition);
}

Each event subscription must be removed by calling EventBus.Unsubscribe(actionDelegate). This means in order to unsubscribe from an event, you must keep a reference to your action delegate, or use a function reference. Make sure to remove a subscription whenever it's script is going to be destroyed to avoid memory leaks.

Example in Unity

First, we can create a global instance of our EventBus in a global GameController or similarly scoped object.

using GameEventBus;

public class GameController : MonoBehaviour {
  public static EventBus Bus = new EventBus();
}

Lets say we have a Player game object and script that responds when a collider with the tag "bullet" collides with it. When your Player collides with a bullet, you can publish a BulletCollision event, with information about the collision.

public class Player : Monobehaviour {
  void OnCollisionEnter(Collision collision) {
    if(collision.gameObject.tag == "bullet") {
      GameController.Bus.Publish<BulletCollision>(new BulletCollision(collision));
    }
  }
}


using GameEventBus.Events;

public class BulletCollisionEvent : EventBase {
  public Collision collision;

  public BulletCollisionEvent(Collision collision) {
    this.collision = collision;
  }
}

All of our scripts that subscribe to the BulletCollision event will be notified whenever our Player is hit with a Bullet

public class Hud : Monobehaviour {
  void Start() {
    GameController.Bus.Subscribe<BulletCollision>(OnBulletCollision);
  }

  void OnBulletCollision(BulletCollisionEvent event) {
    Debug.Log("Our HUD was notified of a bullet collision!")
  }

  void OnDestroy() {
    GameController.Bus.UnSubscribe(OnBulletCollision);
  }
}

gameeventbus's People

Contributors

thomaskomarnicki avatar mxjones avatar tkomarnicki avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.