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)
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)
I'm also joining the complete mod in a zip folder.