Strange behaviour with LuaGuiElement

Place to get help with not working mods / modding interface.
Sunreef
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sun Jun 26, 2016 7:45 pm
Contact:

Strange behaviour with LuaGuiElement

Post by Sunreef »

I recently created my first Factorio mod, with no previous experience in either Lua or modding in general. I'm using Factorio 0.12.35 gog-3 update.

I'm trying to create portals that can be used to teleport your character from one end of your factory to another. In order to do that, I need to register when the player clicks on the portal. Since there is no event that does that directly, I resorted to a hideous hack:
- I detect if a portal is selected (when the mouse hovers over the portal) in a script.on_event(events.on_tick, function(event)) event handler.
- If a portal is selected, I create an invisible screen ( a flow LuaGuiElement) in the center of my screen.
- Then, I detect a click on this invisible screen. Strangely enough, the left click doesn't work but the right click does.
- If I have nothing selected (I moved the mouse), then I destroy this invisible screen. If I leave the screen, I can't select objects on the quickbar anymore.

Here is the code inside my on_tick event handler:

Code: Select all

script.on_event(events.on_tick, function(event)
	if (game.player.gui.center["invisible-screen"] == nil) and (game.player.selected ~= nil) then
		if game.player.selected.name == "portal" then
			game.player.print("create invisible screen")
			game.player.gui.center.add{type = "flow", name = "invisible-screen", direction = "horizontal"}
			game.player.gui.center["invisible-screen"].style.minimal_width = 100
			game.player.gui.center["invisible-screen"].style.minimal_height = 100
		end
	end
	if game.player.selected == nil and game.player.gui.center["invisible-screen"] ~= nil then
		game.player.print("destroy invisible screen")
		game.player.gui.center["invisible-screen"].destroy()
	end
end)
And here is my on_gui_click event handler:

Code: Select all

script.on_event(events.on_gui_click, function(event)
	game.player.print("Clicked on " .. event.element.name)
	
	for k, child in pairs(game.player.gui.center.children_names) do
		game.player.print("Child in center: " .. child)
	end

	if event.element.name == "close-button" then
		game.raise_event(events.on_close_button_click, {entity = event.element})
		return
	end
	
	if(event.element.name == "invisible-screen") then
		if game.player.selected ~= nil then
			if game.player.selected.name == "portal" then
				game.raise_event(events.portal_opened, {entity = game.player.selected})
			end
		end
		return
	end
	
	
	if (string.find(event.element.name, "portal%-label") ~= nil) then
		game.raise_event(events.on_portal_chosen, {entity = event.element})
		return
	end
end)
The problem here is that, even after the destruction of this invisible screen, it seems that it is still there... I can't select objects on my toolbar and when I right click anywhere on the terrain, it prints "Clicked on invisible-screen" (which should be destroyed). Take a look at the screenshot below to see the prints in the console that seem strange to me.
factorio_bug.png
factorio_bug.png (2.87 MiB) Viewed 1564 times
I'm also joining the complete mod in a zip folder.
FirstMod_0.1.0.zip
(5.77 KiB) Downloaded 75 times
User avatar
Afforess
Filter Inserter
Filter Inserter
Posts: 422
Joined: Tue May 05, 2015 6:07 pm
Contact:

Re: Strange behaviour with LuaGuiElement

Post by Afforess »

I haven't tested, but does this have to do with the type of GuiElement you created (flow)? I think that may be treated like a modal popup and is why the quickbar is disabled. Have you tried creating a button type invisible element instead?
Sunreef
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sun Jun 26, 2016 7:45 pm
Contact:

Re: Strange behaviour with LuaGuiElement

Post by Sunreef »

Maybe the flow type behaves strangely. However, I don't know how I can create an invisible button. In the API documentation, I don't see any field where I can specify the GuiElement color. There's only the font color in the style of the element.

EDIT: It seems like it has something to do with parameters in the save file. I removed the invisible screen from my code entirely and reloaded my game. I didn't get the creation and destruction prints but I still had the "Clicked on invisible-screen" notification. Creating a new save removed this behaviour. I put the invisible screen again and created a new save. It works as expected...
Post Reply

Return to “Modding help”