1. Code:
    def OnEntitySpawned(self,entity):
            def my_callback_func(self,arg):
                print arg
           print entity.OwnerID
           new= entity.GetEntity()
           timer.Once(1.0, my_callback_func(self,new), self.Plugin)
    
    any idea why i get this error
    Failed to run a 1.00 timer in 'test v1.0.0' (NullReferenceException: Object reference not set to an instance of an object)
     
  2. Wulf

    Wulf Community Admin

    Sounds like the entity doesn't exist, hence the NRE. You should likely have null checks before trying to use it.
     
  3. You are calling your callback function with self and new.
    self is your object reference, new doesn't exist, which causes an error.
    In addition to this, you are passing the return value of your callback function to the timer (which is None), instead of the function itself, which is also an error.
    The callback for timer.Once may not have any parameters, so this is also an error.
    Also, timer.Once expects an Action, not a Python function, which is an error as well.
    Code:
    from System import Action
    class Foo:
        def foo(self):
            def some_callback():
                print("hello, world")
            timer.Once(1.0, Action(some_callback), self.Plugin)
     
  4. thanks so there is no way to use timer for calling functions and giving arguments ?
    i tried sleep but .. yeah it was a really bad idea
     
  5. Of course there isn't.
    I think you have a fundamental misunderstanding about how this works: You pass a function to the timer and the timer calls your function.
    If you want your own args in the function you make use of a closure (which is fundamentally different from passing args to the function):
    Code:
    from System import Action
    class Foo:
        def foo(self):
            a = 2
            def some_callback():
                print("hello, world", a)
            timer.Once(1.0, Action(some_callback), self.Plugin)
    To mutate a, use a nonlocal closure:
    Code:
    from System import Action
    class Foo:
        def foo(self):
            a = 2
            def some_callback():
                nonlocal a
                a = 1
                print("hello, world", a)
            timer.Once(1.0, Action(some_callback), self.Plugin)
            # a is 1 after timer finishes
     
    Last edited by a moderator: Feb 28, 2016