Why isn’t this event handler working?

Place to get help with not working mods / modding interface.
Xeon257
Inserter
Inserter
Posts: 46
Joined: Wed Sep 24, 2025 6:25 pm
Contact:

Why isn’t this event handler working?

Post by Xeon257 »

(Before I start my questions, I’d like to mention that I’ve used C, C++, and some basic JavaScript, but I’m new to Lua through this modding project.)

For example, when I register an event handler as shown below, it seems that the handler is not being called.

Code: Select all

local test_handler;

script.on_event("linox-ui-dialog-on-select", test_handler);

function test_handler(event)
	--event handling
end
When I directly assign the function like this, the handler is called.

Code: Select all

script.on_event("linox-ui-dialog-on-select", function(event)
	--event handling
end);


To summarize what I’d like to ask:

1. Why is it that the event registration part and the event handler code cannot be separated as in the first example? Or is it simply a syntax mistake on my part?

2. Are event handlers only allowed to be global functions?



I also have some additional questions:

3. Is it absolutely forbidden to use storage at the time control.lua is loaded?

4. Can I assume that my mod is completely isolated from other mods? For example, do I not need to worry about name conflicts in things like Entity Names, Custom Events, or GUI Elements?

5. If script.on_event() is called multiple times, can the same handler be registered multiple times?

6. Is it valid to store and continuously use objects such as LuaPlayer obtained through Event Handlers?

7. I plan to add a util module, but Factorio already provides a built-in one. Is it okay to add my own functions to the default util module?



Thank you for taking the time to read my questions.
I am not able to write in English myself, so I am using ChatGPT for help. Please excuse any awkward phrasing.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4298
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Why isn’t this event handler working?

Post by boskid »

You seem to be influenced by a compiled languages where function definition can be anywhere. My way of understanding lua is that a function definition works as a statement that must be executed first in order to set a global or local variable with an object that is that function. When your first code is executed top to bottom, event is being registered when test_handler variable is still empty and only after the test_handler gets a value assigned because of function definition.
Xeon257
Inserter
Inserter
Posts: 46
Joined: Wed Sep 24, 2025 6:25 pm
Contact:

Re: Why isn’t this event handler working?

Post by Xeon257 »

I’ve been studying the syntax, and it seems that Lua doesn’t have a separate function forward declaration syntax, or it was added only much later. For now, I’ve confirmed that it works when written as shown below.

Code: Select all

local on_elevator;

script.on_event(defines.events.on_lua_shortcut, function(event)
  if event.player_index ~= nil and event.prototype_name == "linox-shortcut-elevator" then
    on_elevator(game.players[event.player_index]);
  end
end)

script.on_event("linox-input-elevator", function(event)
  if event.player_index ~= nil then
    on_elevator(game.players[event.player_index]);
  end
end)

on_elevator = function(player)
......
end
It seems that this is because functions also need to be treated from the perspective of regular objects.
I am not able to write in English myself, so I am using ChatGPT for help. Please excuse any awkward phrasing.
User avatar
yaim904
Fast Inserter
Fast Inserter
Posts: 136
Joined: Wed Nov 17, 2021 11:26 pm
Contact:

Re: Why isn’t this event handler working?

Post by yaim904 »

Xeon257 wrote: Sun Oct 26, 2025 11:57 am
Since you have worked with other languages, you surely understand the basic concepts of programming, so I will get straight to the point.

In Lua, variables do not have a specific type, so you must be careful with values and take advantage of that in your code.
Functions can be declared in two ways (they are commented in the code), and you can also create/use anonymous functions.

Code: Select all


-- 1. Create the function
-- local on_elevator = function(player)
--     ......
-- end

-- local function on_elevator(player)
--     ......
-- end

script.on_event(defines.events.on_lua_shortcut, function(event)
    if event.player_index ~= nil and event.prototype_name == "linox-shortcut-elevator" then
        -- 2. Call the function when the event is triggered
        on_elevator(game.players[event.player_index]);
    end
end)

script.on_event("linox-input-elevator", function(event)
    if event.player_index ~= nil then
        -- 2. Call the function when the event is triggered
        on_elevator(game.players[event.player_index]);
    end
end)

Clarification
Part 1 is done when the game is loaded/created.
Part 2 is executed when the event is triggered (the game is already loaded).

That's why this works.

Code: Select all

local test_handler

script.on_event("linox-ui-dialog-on-select", function(event)
    --event handling
end)

But this doesn't.

Code: Select all

local test_handler -- Its current value is nil.

script.on_event("linox-ui-dialog-on-select", test_handler)  -- Its current value is nil.

-- From here down, the variable contains the function. 
function test_handler(event) 
    --event handling
end
Something apart from everything else
Stop using semicolons (;)

Maybe I didn't make myself clear, but if you ask questions, I'll try to answer them.
Solo entiendo español, pero si tu también lo entiendes, escríbeme
:D
Everything i write in English is translated by Deepl.
:D
Post Reply

Return to “Modding help”