[Suggestion] Observables

Discussion and feedback on Construct 2

Post » Sat Feb 16, 2013 3:42 pm

I think it would be nice if you could have onchange events for variables. For example, I have an object with a state value associated with it. It would be pretty sweet to be able to say On State changed.
B
7
Posts: 24
Reputation: 590

Post » Sat Feb 16, 2013 6:40 pm

If you create another variable and call it old_state then you can get the same result with an event like this:

Sprite.state != Sprite.old_state
   set sprite.old_state to sprite.state
   do other stuffR0J0hound2013-02-16 18:40:44
B
79
S
24
G
54
Posts: 4,749
Reputation: 40,761

Post » Sat Feb 16, 2013 10:02 pm

Yes one can test if a value has changed on every loop but it requires you to store the old state and it requires you to test on every loop. Observables are event driven. That means the event is triggered if and only if it changes. You don't have to store previous state, the hook is embedded in the set function, and you don't have to test the value every tick. You could make every property observable just by hooking into the set function.
B
7
Posts: 24
Reputation: 590

Post » Sun Feb 17, 2013 1:10 am

[quote]That means the event is triggered if and only if it changes.[/quote]
R0J0hound's event is only triggered when it changes. The only way to know if its changed is to check it all the time.
B
134
S
65
G
16
Posts: 1,766
Reputation: 19,190

Post » Sun Feb 17, 2013 12:21 pm

I can execute an event if and only if it changes, but the syntax is horrible. As I said, you have to a) make a comparison every tick, b) store the old value in a variable every tick. It's ugly not the most efficient approach.

You don't have to check every time in an observable pattern. In the observable pattern there is a check in the setter to see if anything is observing the value, and to trigger that event from the setter.

When you call Set Player.State, that is when you know that the value changed. Here is where the flag is raised. I do not check it on every tick and store the value every tick, I just trigger an event when someone tries to change it.
B
7
Posts: 24
Reputation: 590

Post » Sun Feb 17, 2013 7:47 pm

You could use the funtion object and make a function that sets the state variable. Then use that function to set the state. It would look something like this:

+on function "setState"
+pick sprite with UID function.param(0)
--- set state to function.param(1)
--- do other stuff

+some condition
+for each sprite
--- call function "setState", sprite.uid, 1337

Put your onchanded events in the setState function and it would only be triggered when state is changed.
B
79
S
24
G
54
Posts: 4,749
Reputation: 40,761

Post » Mon Feb 18, 2013 5:52 pm

Well that's effectively how it would work, except I think it's far more elegant and easier to implement a generic observable pattern. It's actually fortunate in this case as setters are not universally supported in JavaScript, but every setter is a function in construct. Every action in construct is a function (but I may be wrong).

I assume it would be no trouble adding observable logic to these actions inside the runtime, and it would be far cleaner than writing functions manually for each value that you wish to observe. It would be easier to code in an event driven way with this.

If construct objects were event emitters, the value setting function would emit a change event with the property name that had changed. This would then drive any events listening for the change. I might take a look to see if this is possible to update the construct objects to do this myself.
B
7
Posts: 24
Reputation: 590


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 9 guests