OnTick listener as a class method

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

OnTick listener as a class method

Postby BackRaw » Thu May 05, 2016 6:53 pm

Hi,

just wanted to ask whether it's a good or bad idea to have a class method as an OnTick listener and why.

Thanks :D
User avatar
satoon101
Project Leader
Posts: 2703
Joined: Sat Jul 07, 2012 1:59 am

Re: OnTick listener as a class method

Postby satoon101 » Fri May 06, 2016 12:05 am

I guess it really doesn't matter. It will actually act as a staticmethod (regular function), anyway. Meaning, it will not have access to the class or the instance (if you make an instance of one). Now, if you use on_tick_listener_manager.register_listener on a method of the class/instance, it will have access to the specific object. Just test out this example:

Syntax: Select all

from listeners import OnTick
from listeners import on_tick_listener_manager

class Test(object):
@classmethod
def test_one(cls, *args):
print('test_one', cls, args)

@OnTick
def test_two(*args):
print('test_two', args)

def test_three(self, *args):
print('test_three', self, args)

var = Test()

on_tick_listener_manager.register_listener(Test.test_one)
on_tick_listener_manager.register_listener(var.test_three)

def unload():
on_tick_listener_manager.unregister_listener(Test.test_one)
on_tick_listener_manager.unregister_listener(var.test_three)
Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnTick listener as a class method

Postby BackRaw » Fri May 06, 2016 2:24 am

Great, thanks!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 71 guests