Custom entity tooltips?

Place to get help with not working mods / modding interface.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Custom entity tooltips?

Post by Reika »

Or "right hand/info panel" or whatever you want to call it. Basically I have a simple entity which contains no data in and of itself, but has data corresponding to it in the global table, and want the tooltip to display that info.

Is this possible?

Basically I want to show the liquid level (as well as emission per cycle) in the right panel for this entity:
Image
Image
Image
Image
321freddy
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Sep 23, 2016 10:16 pm
Contact:

Re: Custom entity tooltips?

Post by 321freddy »

Since you cannot edit the basegames GUIs you have to rely on a workaround.
The only one I know of which would come very close to what you want only works for entities which support custom backer names:
Basically you add a newline to the backer name of the entitiy and add your text there:

Code: Select all

player.selected.backer_name = player.selected.backer_name .. "\r\n<your data goes here>"
Results in something like this:
Image

If your entitiy does not support backer names the you'd have to create your own gui inside the on_selected_entity_changed and put your data in there.
Or display a flying text entity.
Using your own gui gives you way more flexibility but is a bit detached from the base game gui. I did it like this in my attach notes mod:
Image
I can provide code examples for this if you want.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: Custom entity tooltips?

Post by Reika »

321freddy wrote:Since you cannot edit the basegames GUIs you have to rely on a workaround.
The only one I know of which would come very close to what you want only works for entities which support custom backer names:
Basically you add a newline to the backer name of the entitiy and add your text there:

Code: Select all

player.selected.backer_name = player.selected.backer_name .. "\r\n<your data goes here>"
Results in something like this:

If your entitiy does not support backer names the you'd have to create your own gui inside the on_selected_entity_changed and put your data in there.
Or display a flying text entity.
Using your own gui gives you way more flexibility but is a bit detached from the base game gui. I did it like this in my attach notes mod:

I can provide code examples for this if you want.
Since spilled fluid is a simple entity and does not support backer names, can you show me a sample of the custom GUI code? I have never done anything like that before.
Image
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Custom entity tooltips?

Post by darkfrei »

Reika wrote:can you show me a sample of the custom GUI code? I have never done anything like that before.
viewtopic.php?f=25&t=56317
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: Custom entity tooltips?

Post by Reika »

It seems that the only way to remove gui elements is to iterate the parent's entire contents, since the "children" array is index, not name based.

Since this is only called on selected entity change, is there a way to update this in realtime, performantly (ie without iterating across all players' entire gui contents each tick)?

Also, even that has issues, as if there are multiple spills, only one of which is being selected, the GUI elements to be updated cannot have identifiers so that the tick-based code can know which object the GUI element corresponds to.
Image
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Custom entity tooltips?

Post by eradicator »

Simple.
Create a gui element player.left (or top) and store the reference

Code: Select all

global[playerindex].mytooltip = player.gui.left.add{stuff/frame}.add{stuff/label}
in on_selected_entity_changed watch for your entities name, if your spill is selected start a dynamic tick handler.
In the tick handler just:

Code: Select all

mytooltip.mylabel.text = fetch_my_data(generate_identifier(player.selected))
When the player unselected the spill stop the tick handler.

Not sure if simple-entity has a unit_number property, so you might have to generate a UID based on the position coordinates. Also be aware that you get no control over where on the screen that tooltip is placed. It all depends on what other mods that add gui elements are loaded before yours.

(Dynamically starting/stopping the tick handler is obviously done for performance reasons only, to not waste cpu when unnessecary)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: Custom entity tooltips?

Post by Reika »

eradicator wrote:Simple.
Create a gui element player.left (or top) and store the reference

Code: Select all

global[playerindex].mytooltip = player.gui.left.add{stuff/frame}.add{stuff/label}
in on_selected_entity_changed watch for your entities name, if your spill is selected start a dynamic tick handler.
In the tick handler just:

Code: Select all

mytooltip.mylabel.text = fetch_my_data(generate_identifier(player.selected))
When the player unselected the spill stop the tick handler.

Not sure if simple-entity has a unit_number property, so you might have to generate a UID based on the position coordinates. Also be aware that you get no control over where on the screen that tooltip is placed. It all depends on what other mods that add gui elements are loaded before yours.

(Dynamically starting/stopping the tick handler is obviously done for performance reasons only, to not waste cpu when unnessecary)
These entities already have a tickhandler, so I can plug it into that. I also already have UIDs based on coordinates, for exactly this ability.
Image
Post Reply

Return to “Modding help”