making entity-preview work

Place to get help with not working mods / modding interface.
Post Reply
cyfrov
Inserter
Inserter
Posts: 25
Joined: Thu May 03, 2018 7:09 pm
Contact:

making entity-preview work

Post by cyfrov »

I'm trying to recreate the combinator gui, and in the process, I can't get the entity-preview element to work... at all


Simple test code below, guiImgEnt is the entity-preview element, and it doesn't do diddly.
What am I screwing up? HELP!

Code: Select all

function MakeGUI(player, entity)
	player.gui.center.clear()
	local guiFrame = player.gui.center.add{
		type="frame",
		name="guiFrame",
		direction="vertical"
	}

	local guiFrameImg = guiFrame.add{
		type="frame",
		name="guiFrameImg",
		direction="vertical",
		caption=entity.name
	}

	local guiImgEnt = guiFrameImg.add{
		type="entity-preview",
		name="guiImgEnt",
		entity=entity
	}
	guiImgEnt.entity = entity
	
	local guiFrameInp = guiFrame.add{
		type="frame",
		name="guiFrameInp",
		direction="vertical",
		caption="Inputs"
	}
	local guiTblInp = guiFrameInp.add{
		type="table",
		name="guiTblInp",
		column_count=3,
		draw_vertical_lines=true,
		draw_horizontal_lines=true,
		draw_horizontal_line_after_headers=true
	}

end

User avatar
theRustyKnife
Filter Inserter
Filter Inserter
Posts: 259
Joined: Thu Feb 26, 2015 9:26 pm
Contact:

Re: making entity-preview work

Post by theRustyKnife »

This seems to be a bug, I made a report about it here.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: making entity-preview work

Post by eduran »

For inexplicable reasons, entity previews are created invisible. Try adding .visible = true after creating it.

Code: Select all

  
  local center = game.players[1].gui.center
  local frame = center.add{type = "frame"}
  frame.style.width = 200
  frame.style.height = 200
  local ep = frame.add{type = "entity-preview"}
  log2(ep.style.maximal_width)
  ep.style.width = 150
  ep.style.height = 150
  ep.visible = true
  ep.entity = player.selected
 
Works for me.

Side-note: calling clear on a gui root element will delete all modded guis attached to that root element. Please don't do that.
Last edited by eduran on Sat Apr 06, 2019 7:34 pm, edited 1 time in total.

User avatar
theRustyKnife
Filter Inserter
Filter Inserter
Posts: 259
Joined: Thu Feb 26, 2015 9:26 pm
Contact:

Re: making entity-preview work

Post by theRustyKnife »

Doesn't work for me...

Edit: Also just checked and .visible actually returns true after creating it as well.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: making entity-preview work

Post by eduran »

I guess it's not just the visibility, but also width and height that need to be set. Example code above. Seems like a bug that this is necessary.

User avatar
theRustyKnife
Filter Inserter
Filter Inserter
Posts: 259
Joined: Thu Feb 26, 2015 9:26 pm
Contact:

Re: making entity-preview work

Post by theRustyKnife »

Ok, that works. It's kinda stupid the way it works, so I guess I'll just mention this in the report.

For the record: visibilty is fine, it's the width/height that's the issue.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: making entity-preview work

Post by eduran »

You are correct, visibility i indeed fine. The issue is probably that it default to the "empty-widget" style.

cyfrov
Inserter
Inserter
Posts: 25
Joined: Thu May 03, 2018 7:09 pm
Contact:

Re: making entity-preview work

Post by cyfrov »

Thanks, updating the styles seems to do it.
eduran wrote:
Sat Apr 06, 2019 7:28 pm
Side-note: calling clear on a gui root element will delete all modded guis attached to that root element. Please don't do that.
How would you recommend I do it? Also, is there an automated way to have the gui close when a player hits [Esc] or selects a different entity?

User avatar
theRustyKnife
Filter Inserter
Filter Inserter
Posts: 259
Joined: Thu Feb 26, 2015 9:26 pm
Contact:

Re: making entity-preview work

Post by theRustyKnife »

cyfrov wrote:
Sat Apr 06, 2019 8:31 pm
Thanks, updating the styles seems to do it.
eduran wrote:
Sat Apr 06, 2019 7:28 pm
Side-note: calling clear on a gui root element will delete all modded guis attached to that root element. Please don't do that.
How would you recommend I do it? Also, is there an automated way to have the gui close when a player hits [Esc] or selects a different entity?
Just call .destroy() on your guiFrame element to get rid of it.

For the other thing you want to look at player.opened.

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: making entity-preview work

Post by eduran »

cyfrov wrote:
Sat Apr 06, 2019 8:31 pm
Also, is there an automated way to have the gui close when a player hits [Esc] or selects a different entity?
First you need to register your UI as opened. Then you close your UI as a response to the on_gui_closed event.

Code: Select all

-- as part of the function that creates your UI
game.players[player_index].opened = my_ui_element

-- in control.lua
script.on_event(defines.events.on_gui_closed, close_my_ui)
close_my_ui needs to do something like this:

Code: Select all

function close_my_ui(event)
  if event.element and event.element.name == "my_ui_element_name" then
    event.element.destroy()
  end
end
cyfrov wrote:
Sat Apr 06, 2019 8:31 pm
How would you recommend I do it?
If you name your top-most UI element you can use .destroy() or .visible = false

Code: Select all

local my_ui_element = game.players[player_index].gui.center[my_ui_element_name]
my_ui_element.destroy()
-- OR
my_ui_element .visible = false

Post Reply

Return to “Modding help”