Page 1 of 1

making entity-preview work

Posted: Sat Apr 06, 2019 6:45 pm
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

Re: making entity-preview work

Posted: Sat Apr 06, 2019 7:25 pm
by theRustyKnife
This seems to be a bug, I made a report about it here.

Re: making entity-preview work

Posted: Sat Apr 06, 2019 7:28 pm
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.

Re: making entity-preview work

Posted: Sat Apr 06, 2019 7:34 pm
by theRustyKnife
Doesn't work for me...

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

Re: making entity-preview work

Posted: Sat Apr 06, 2019 7:36 pm
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.

Re: making entity-preview work

Posted: Sat Apr 06, 2019 7:39 pm
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.

Re: making entity-preview work

Posted: Sat Apr 06, 2019 7:43 pm
by eduran
You are correct, visibility i indeed fine. The issue is probably that it default to the "empty-widget" style.

Re: making entity-preview work

Posted: Sat Apr 06, 2019 8:31 pm
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?

Re: making entity-preview work

Posted: Sat Apr 06, 2019 8:54 pm
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.

Re: making entity-preview work

Posted: Sat Apr 06, 2019 9:02 pm
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