on_entity_clicked

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

on_entity_clicked

Post by aubergine18 »

Doing custom GUI for an entity is a PITA.

There are two approaches currently available, both of which suck:

custom-input

This is useful if we need to retain existing gui, but want optional alternative gui. We define custom-input keyboard shortcut, and when its event is triggered, we check game.players[event.player_index].selected to see if it's applicable entity and if so show the GUI.

.opened

We can check player.opened every so often (putting more stuff in on_tick is good for performance, compared to events), and if its not nil check to see if the opened entity is something we are interested in and then show our custom GUI. As far as I know there's no way to hide the vanilla GUI for the entity though.

If we set entity.operable = false for the entity, it will stop vanilla GUI opening but then as far as I know the player.opened will never get set because the player can't "open" the entity.

Proposed solution

When an entity is clicked, trigger an on_entity_clicked event, with the entity as a property of the event table.

This way we could set .operable = false, to prevent default GUI, and use the evnet to trigger our GUI.

While our GUI is open, we can check player proximity to the entity (again, via on_tick, because that's good for performance compared to events) and if they get too far away we can close the GUI (It would be nice to have that event driven also, but not sure if it would be feasible to implement - the game can and does already do it for vanilla entities, but I imagine that an event to allow mods to do the same would impact performance).

As for manual closing of our GUI, we'd likely need to create a custom-input, or put a little X button on the GUI to close it with mouse click. As far as I know the standard ways of closing GUI are not going to be available to mods (they are reserved for vanilla GUI) so we have to provide alternate user interaction for modded GUIs. Different interaction models for vanilla and modded GUI are good UX design.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1196
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: on_entity_clicked

Post by Mooncat »

hm... it reminds me of this: viewtopic.php?f=28&t=24669
But I know they are different.

I wonder if on_entity_clicked will be useful, because, in your case, the clicked entity is inoperable, the player can click another entity to open its GUI (assuming this 2nd entity is not controlled by your mod), thus your GUI and the vanilla GUI of that entity is overlapped. To solve this problem, you will still need to check player.opened. :?
apriori
Filter Inserter
Filter Inserter
Posts: 275
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: on_entity_clicked

Post by apriori »

I'm in! Vote for on_entity_clicked event!
Now I'm trying to realize "custom gui" for entities, and the only way to do that (which I use) is by hovering a mouse over an entity for some time. on_entity_clicked would be VERY HELPFUL!
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.
Post Reply

Return to “Modding interface requests”