0 Favourites

[Suggestion] Observables

  • 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.

  • 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 stuff

  • 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.

  • hat means the event is triggered if and only if it changes.

    R0J0hound's event is only triggered when it changes. The only way to know if its changed is to check it all the time.

  • 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.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
random bugs and suggestions
90 4,515
Tom's avatar
Tom Construct Team Founder
Unread hot topic
73 5,920
part12studios's avatar
part12studios
Unread hot topic
63 3,819
eli0s's avatar
eli0s