Proper gui usage and entity based variables

Place to get help with not working mods / modding interface.
Post Reply
revol_
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sun Oct 30, 2016 12:31 pm
Contact:

Proper gui usage and entity based variables

Post by revol_ »

I've found out about the prototypes and the events, but none of them seem to suit my need. I am currently making my first mod (mod should add a sign with editable text that can be edited by opening the gui and viewed by hovering the sign) I have so far managed to make a gui pop up anytime a player places a sign. I've also made it so, every time a player picks up or destroys or walks too far from the last sign they placed the gui will close. This was done by a really crappy system of checking for each of the conditions mentioned above. I'm also unable to figure out how to save or how to display the text the user inputs to the box.

TLDR: What is the correct way to add a gui to an object? How to store variables on entities?

Edit1: I also forgot to mention that I have not yet found a way to open the gui after the placement of the object, so a little help would be appreciated :)

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Proper gui usage and entity based variables

Post by aubergine18 »

For opening GUI upon entity placement, listen to the on_built_entity event (assuming you only want the gui to open when player places the entity). In the event handler, check the event.created_entity.type and .name to determine if it's your entity that was built.

As for proper way to do entity GUI, there isn't one.

You have to check to see if a player's .selected == yourObject every few ticks in an on_tick event handler. Alternatively define a custom-input (keyboard shortcut) and when that event fires check to see if player has .selected == yourObject. Same goes for player walking away, the .selected will automatically leave the object if player is too far away (in fact, it will change as soon as player mouse is not over object). Checking via distance between player and object is another way to auto-close GUI.

To store data against objects, you can use LuaEntity.unit_number - a unique ID that most player-creatable entities have.

There's also Factorio Stdlib which has some useful features for storing data against entities.

EDIT: For adding text to game map, check out flying-text as a potential solution.

There are a bunch of existing mods that do stuff like you are attempting, you could take a look at their source code to see how they do it.

* https://mods.factorio.com/mods/JohnnyDee/JD_Signposts
* https://mods.factorio.com/mods/binbinhfr/StickyNotes
* https://mods.factorio.com/mods/icedevml/Signposts
* https://mods.factorio.com/mods/Nexela/CircuitAlerter
* https://mods.factorio.com/mods/Supercheese/Map%20Ping
* https://mods.factorio.com/mods/kij336/checkpoint
* https://mods.factorio.com/mods/Kasp3r/LogisticsSigns
* https://mods.factorio.com/mods/binbinhfr/GroundPrinter
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.

revol_
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sun Oct 30, 2016 12:31 pm
Contact:

Re: Proper gui usage and entity based variables

Post by revol_ »

aubergine18 wrote:For opening GUI upon entity placement, listen to the on_built_entity event (assuming you only want the gui to open when player places the entity). In the event handler, check the event.created_entity.type and .name to determine if it's your entity that was built.

As for proper way to do entity GUI, there isn't one.

You have to check to see if a player's .selected == yourObject every few ticks in an on_tick event handler. Alternatively define a custom-input (keyboard shortcut) and when that event fires check to see if player has .selected == yourObject. Same goes for player walking away, the .selected will automatically leave the object if player is too far away (in fact, it will change as soon as player mouse is not over object). Checking via distance between player and object is another way to auto-close GUI.

To store data against objects, you can use LuaEntity.unit_number - a unique ID that most player-creatable entities have.

There's also Factorio Stdlib which has some useful features for storing data against entities.
This is very helpful, I just want to ask Is there a way to re-open the gui after the item was placed and the gui was closed. (like regular guis in the game)

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Proper gui usage and entity based variables

Post by aubergine18 »

Possibly. You could check player.opened to detect when the normal gui of the entity is opened, then open your gui. I'm not sure if the vanilla gui can be programatically closed though. You can prevent the game gui from opening by setting entity.operable = false, but then there probably won't be any way to detect when player has clicked on an entity (which is why some mods use a custom-input keyboard shortcut instead).
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.

Post Reply

Return to “Modding help”