luagodotgodot4

How to connect to a Timer timeout signal on Godot engine with Lua GDExtension


Here is a simple script attached to a Node2D that i try to connect a timer timeout without success:

    local test = {
     extends = Node2D,
    }
    function test:_ready()

        function _on_timeout()
            print("timeout end")
        end

        local timer = self:get_tree():create_timer(2)
        timer:connect("timeout", self, _on_timeout)
    end

    return test

I got the following error:

   E 0:00:00:894 connect: Cannot connect to 'timeout': the provided callable is null.
   <Erreur C++> Condition "p_callable.is_null()" is true. Returning: ERR_INVALID_PARAMETER
   core/object/object.cpp:1410 @ connect()

using Lua Gdextensions How to connect a signal with Lua GDExtension?


Solution

  • After reading the source code i finaly found the solution:

    local test = {
      extends = Node2D,
    }
    
    function test:_on_timeout()
        print("timeout end")
    end
    
    function test:_ready()
        local timer = self:get_tree():create_timer(2.0)
        --another way is : timer:connect("timeout", Callable(self, "_on_timeout"))
        timer.timeout:connect(Callable(self, "_on_timeout"))
    end
    
    
    return test