# RX + Defold

**URL:** <https://forum.defold.com/t/rx-defold/44611>\
**Category:** The Defoldmine\
**Created:** [February 26, 2019, 8:26am UTC](https://forum.defold.com/t/rx-defold/44611 "2019-02-26T08:26:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [February 26, 2019, 8:26am UTC](https://forum.defold.com/t/rx-defold/44611/1 "2019-02-26T08:26:37Z")

</div>

Make some experiments with [RxLua](https://github.com/bjornbytes/RxLua) and defold.

Rx it is a reactive programming. You can read more here [http://reactivex.io/](http://reactivex.io/).

#### Rx is based on **Observables** , **Observers,** and **Operators**

An Observer essentially **subscribes** to an Observable.

The Observable then emits streams of data which the Observer listens and reacts to, setting in motion a chain of operations on the data stream. The real power comes from Operators or “Reactive Extensions” (hence the term Rx) **.**

Operators allow you to transform, combine, manipulate, and work with the sequences of items emitted by Observables.

**My problem**  
I like mvc and observable pattern. But it hard to use it in defold, because when i worked with go or gui, i need to worked inside it context. I can’t subscribe in gui to model,and change gui when model change event emmited(yes i can, save events and then iterate them on\_update, i have lib for that, but it is not comfortable.  
I can use messages for it, but messages worked only with go/gui, not with lua modules(i can make module that will be emmit messages for modules, but it is not very comfortable).Also working with messages not very comfortable =)

**RXLua**  
I add 2 methods to RxLua lib.  
go and go\_distinct. They worked in context of go.  
go emmits all events  
go\_distinct only unique per frame.

Go is very simple. It emmits events when scheduler will be updated.I update it every frame in context of go.

```lua
function Observable:go(scheduler)
    return self:delay(0,scheduler)
end

```

**Rx Example**  
Model(emmit 5 equals events every frame)

```lua
function M:initialize()
	self.rx = RX.Subject()
end

local frame = 0
function M:update(dt, no_save)
	self.ecs_world:update(dt)
	for i=1,5 do
		self.rx:onNext(frame)
	end
	frame = frame + 1
end

```

Subscription go script

```lua
...
    WORLD.rx:go(SCHEDULER):subscribe(function(v)
        print("all:" .. v)
    end)
    WORLD.rx:go_distinct(SCHEDULER):subscribe(function(v)
        print("distinct:" .. v)
    end)
...

function Scene:update(go_self, dt)
    SCHEDULER:update(dt)
end

```

```lua
DEBUG:SCRIPT: [INFO 11:01:02] none: /scenes/logo/logo_scene.lua:21: distinct:45
[5]DEBUG:SCRIPT: [INFO 11:01:02] none: /scenes/logo/logo_scene.lua:18: all:45

```

I don’t show here power of Rx. When you can:receive value, change it, mix it with other and etc. It contains a lot of methods, you can read it in docs [RxLua/doc at master · bjornbytes/RxLua · GitHub](https://github.com/bjornbytes/RxLua/tree/master/doc).

When you read it, you can think that rx complex and redundant. But give it a chance.It is simple but powerful tool. I love it = )

**Project**  
It contains a lot of code, because i use my template for all project. You need  
libs/rx  
world/world.lua (model of world)  
init\_controller(update world model)  
scenes/logo/logo\_scene.lua.(scene controller.Subscribe to world)

> **[GitHub - d954mas/defold-rx-example: Defold-RX-Example](https://github.com/d954mas/Defold-RX-Example)**
>
> Defold-RX-Example

---

<div class="post-metadata">

**Author:** ![Infinyc](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/infinyc/32/30925_2.png) [@Infinyc](https://forum.defold.com/u/Infinyc)\
**Post date:** [February 7, 2023, 5:55am UTC](https://forum.defold.com/t/rx-defold/44611/2 "2023-02-07T05:55:03Z")

</div>

Hi @d954mas nice post, I would like to pass functions inside on\_message Defold functions, but it’s not possible, so I search more about it and find RXlua, but not clear how to add inside Defold. I try the dependencies link and fetch but not load, I’m new in Defold still not sure how to make it work.

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [February 7, 2023, 4:14pm UTC](https://forum.defold.com/t/rx-defold/44611/3 "2023-02-07T16:14:03Z")

</div>

Hi, it is not defold library. It is lua library, you need copy lua file.

Not sure how it help you to pass functions, but you can try.

Also look at flow [GitHub - britzl/ludobits: Utilities for game development using the Defold engine · GitHub](https://github.com/britzl/ludobits)  
Maybe it will be better work for you

---

<div class="post-metadata">

**Author:** ![Infinyc](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/infinyc/32/30925_2.png) [@Infinyc](https://forum.defold.com/u/Infinyc)\
**Post date:** [February 19, 2023, 7:36am UTC](https://forum.defold.com/t/rx-defold/44611/4 "2023-02-19T07:36:46Z")

</div>

Hi @d954mas I try the"ludobits.m.broadcast" but dont work like I spected  
I atach the script\_1 at 3 gameobjects.

```lua
--script_1
local broadcast = require "ludobits.m.broadcast"

function init(self)
	broadcast.register("foo")
end

function final(self)
	broadcast.unregister("foo")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("foo") then
		pprint(message)
	end
end

```

and script\_2 to one game object.

```lua
--script_2
local broadcast = require "ludobits.m.broadcast"

function init(self)
	broadcast.send("foo", { something = 123 })
end

```

I subscribe to the message “foo” in all 3 game objects, when send the message the code execute one time, instead of 3 times. Did you know why don’t execute the same message 3 times?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 19, 2023, 8:22am UTC](https://forum.defold.com/t/rx-defold/44611/5 "2023-02-19T08:22:49Z")

</div>

> [@Infinyc](#):
>
> Did you know why don’t execute the same message 3 times?

The order in which init() is called is not guaranteed. In your case the init() of script\_1 has probably been run on 1 game object. Then you hit the init() of script\_2 where you broadcast a message. And this will happen immediately, before the init() of the other two game objects using script\_1.

If you really want to broadcast something in init() you need to delay it. You can use a timer to delay it:

```lua
function init(self)
	-- delay until immediately after all init() functions have been run
	timer.delay(0, false, function()
		broadcast.register("foo")
	end)
end

```
