Why isn’t this event handler working?

Place to get help with not working mods / modding interface.
Xeon257
Inserter
Inserter
Posts: 40
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: 4262
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: 40
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.
Post Reply

Return to “Modding help”