perbone / luascript

Lua language support for Godot Engine
Apache License 2.0
629 stars 44 forks source link

Lua Signals Design #28

Open perbone opened 3 years ago

perbone commented 3 years ago

The idea is to design how signals will work in Lua and Godot. A signal will be an element in a table in the class object and will follow the definitions from the initial call for the implicit available signals method.

Following below is an excerpt for some of the the designs I have so far.

local class = require 'lua.class'       -- Import the system class library
local Sprite = require 'godot.Sprite'   -- Make sure to import the base class

local Main = class.extends(Sprite)      -- Create the user subclass

Main:signals {
    { name = 'sum_calculated', parameters = { name = 'sum', type = 'number' } },
    { name = 'completed' } },
    { name = 'new_title', parameters = { name = 'title' } },
}

function Main:ready()
    math.randomseed(os.time())          -- Randomize from whatever the time is
end

function Main:process(delta)
    local sum = math.random(100)
    emit_signal( 'sum_calculated', sum )
end

return Main

The signals in the code above have the following characteristics:

Please share your thoughts on this idea or show us a new idea that you come up about this topic.

-- Perbone

carabalonepaulo commented 3 years ago

A less verbose approach:

Main:signals {
    { 'sum_calculated',  { 'sum', 'number' } },
    'completed',
    { 'new_title', 'title' },
}

sum_calculated has one parameter defined as number completed has no parameters new_title has one parameter with no explicit type.