Coder Social home page Coder Social 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 PlayerJumpEvent(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<BulletCollisionEvent>(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 zcabjro avatar

Stargazers

Sebastien Vermeille avatar  avatar Julian Sievers avatar Isma Navarro avatar Kristian Hedeholm avatar ThomasBergersen avatar JT Tirado avatar Pedro Belluzzo avatar  avatar Andreas Schallwig avatar Lars Hedlund avatar  avatar Cody Selman avatar  avatar Lad avatar  avatar Robert Coomber avatar Nguyễn Tuấn Minh avatar Alexey Pozdnyakov avatar Hasan Talha Yalmanbaş avatar  avatar Gerard Madorell avatar Retyek avatar  avatar Dzianis Belabrotski avatar  avatar  avatar Karri Väänänen avatar  avatar  avatar InvisibleButter avatar Miguel Medina Ballesteros avatar George Newton avatar  avatar David avatar Ward Boumans avatar Osman Zeki avatar Anthony Tamez avatar Mr. Paparoz avatar Jacobsky avatar Youngkyoung Lee avatar  avatar Picker Weng avatar  avatar Sean McAllister avatar  avatar

Watchers

James Cloos avatar Oli 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.