Hi, and sorry for the delay.
On Mon, Sep 22, 2014 at 8:51 AM, Daniel Gillet <[email protected]>
wrote:
> Hello,
>
> I was wondering what would be a good way to have a Label showing the
> content of a variable, and updating automatically when the variable
> changes. I made something like this but I would love to hear if you have
> something better for me.
>
[...]
> What I don't like too much with this method is that I have to reference
> the timer.value to manipulate my variable, instead of having just a timer
> variable.
>
> Another solution I had, inspired by CocosNode make_property function was
> to create properties with the setter calling a callback.
>
>
>
[...]
> As I wrote in the code, this leads some problem of setting first the timer
> property or creating first the label.
>
You could use a third party publish-subscribe library to handle that,
publish a change in the setter and register a listener for the event
published.
blinker [0] is a good publish-subscribe library, it is available on pypi
Thanks for your comments and suggestions!
>
> Dan.
>
Tracking changes heavily depends on the use case.
If you need to detect only change by assigns, I got a nice idea inspired by
this thread in SO [1]
It intercepts __setattr__ and publish if desired, another layer subscribe
to the event and display the desired.
Uses blinker to do the publish-subscribe.
What I like about this solution is
- The observed container is kept clean
- easy to add or remove targets
Caveats: if an attribute changes by mutation instead of assign the change
will not be detected (ex: self.a = list() ... self.a.append('z'))
sample at http://pastebin.com/cGuBeh0Z
[0] https://pythonhosted.org/blinker/
[1]
http://stackoverflow.com/questions/676826/intercepting-changes-of-attributes-in-classes-within-a-class-python
--
You received this message because you are subscribed to the Google Groups
"cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/d/optout.