Giter Site home page Giter Site logo

gredux's Introduction

gredux

Redux inspired addon for state management in Godot.

Similar to modern websites, games need to manage large, ever changing states. This libraries brings Redux to godot in an attempt to make state mutations predictable. By persisting the state of your game in one location you also get certain features for (almost) free, such as time travel (undo/redo) and saving/loading saved games.

Adding to a Godot project

We recommend cloning this repository inside an addons folder within your godot project.

Usage

With the files added you can create a redux store as shown below:

extends Node

const GRedux = preload("res://addons/gredux/gredux.gd")
const CounterContext = preload("res://redux/CounterContext.gd")

var initial_state = {
	"todos": []
}

var reducers = {
	"todos": funcref(CounterContext, "reducer")
}

var Store

func _ready():
	Store = GRedux.new(reducers, initial_state)

	Store.dispatch(CounterContext.add_todo("hello world"))

CounterContext in the example above contains both a reducer and its actions.

extends Node

const ADD_TODO = "ADD_TODO"

static func add_todo(name):
	return {
		"type": ADD_TODO,
		"name": name
	}

static func reducer(state, action):
	if action["type"] == ADD_TODO:
		var next_state = shallow_copy(state)
		next_state.append(action["date"])
		return next_state

	return state

Middlewares

The third argument to GRedux.new() is a middleware array. A middleware in this library works slightly differently than in redux. They still have access to the store and will execute one after the other before the reducers are invoked, but do not invoke the next middleware themselves. You can still use a middleware to transform the action perform side effects or call new actions.

You can find more usages in the examples folder.

License

MIT

gredux's People

Contributors

au-re 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.