Git Product home page Git Product logo

gamefartsstatebroker's Introduction

Game Farts Unity State Broker

This is a small library for transactional state in Unity.

How it works

Each state value has an associated event broker and an action that an be subscribed to. When a value is changed it sets itself as dirty. Then when a state transaction is finished the state broker notifies all dirty subscribers that state has changed. Subscribers will only be notified once each, even if they are listening to multiple changed values.

Installation

You can install this in Unity as a Package by going to Window > Package Manager And then hitting the + and selecting Add Package from git URL.

Usage

  1. Create a class to hold your state variables. Each variable should implement the IObservableStateProperty interface.

  2. In your initialisation instantiate a StateBroker.

  3. Create your state variables with a default value and pass them into the broker.

  4. Instantiate your state class and inject your variables.

    StateBroker = new StateBroker();
    
    var stars = new ObservableStateProperty<int>(10);
    var coins = new ObservableStateProperty<int>(10);
    
    StateBroker.AddProperty(stars);
    StateBroker.AddProperty(coins);
        
    GameState = new GameState(stars, coins);
  5. You can subscribe and unsubscribe to the variable like this:

    GameState.Stars.Subscribe(ObserverCallback);
    GameState.Stars.Unsubscribe(ObserverCallback);
  6. To start a state transaction you tell the StateBroker.

    StateBroker.StartTransaction();
  7. To fire a notification after values have changed you can call the Commit() method on the StateBroker.

    StateBroker.Commit();
  8. If you want to change a state property outside a transaction you can manually call Fire() on them.

Tip: Use a ScriptableObject as your GameState and you can pass it around easier.

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.