User:Raiguard/Tutorial:Modding tutorial/GUI

From Official Factorio Wiki
< User:Raiguard
Revision as of 09:14, 11 June 2020 by Bilka (talk | contribs) (Bilka moved page Tutorial:Modding tutorial/GUI to User:Raiguard/Tutorial:Modding tutorial/GUI without leaving a redirect: WIP pages must be in your userspace)
Jump to navigation Jump to search

THIS PAGE IS A WORK-IN-PROGRESS.

While you cannot edit base game GUIs, the Lua API includes the ability to create your own, completely custom GUIs. This tutorial will get you up to speed with creating such GUIs.

Before you Begin

Be sure you have the LuaGui, LuaGuiElement, and LuaStyle pages handy.

Tutorial syntax

This tutorial will adapt some methods from Modding tutorial/Gangsir, namely:

Code tinted green like this should be included into the mod this tutorial is going to create; If the reader follows along with it. The best way to do this is to copy and paste, to ensure faithful reproduction.
Whenever code is added to the mod, a Lua comment with the file name will be at the beginning of the green box. Place the code in the box into that file. Eg:
--control.lua
Code tinted purple like this should not be included into the mod, it's just for educational/example purposes, and to boost understanding.

For the purposes of the tutorial, it would be a good idea to create a fresh mod, separate from the mod you created in Gangsir's tutorial.

In-game tool: GUI border view

File:Modding tutorial Ctrl F5.png

Hitting Control + F5 on your keyboard will bring up the "GUI border view". This surrounds all GUI elements with red borders, and can help you visualize how things are laid out.

In-game tool: GUI style inspector

File:Modding tutorial Ctrl F6.png

Hitting Control + F6 on your keyboard will allow you to use the "GUI style inspector". This allows you to view an element's style inheritances when you hover over it. Combined with the GUI border view, you can inspect normally hidden GUI elements.

Library: mod-gui

Add this require into your script file before you begin:

-- control.lua
local mod_gui = require("__core__.lualib.mod-gui")

The Root

To build a GUI, you must gain access to the GUI "root". The most common method of access is through LuaPlayer::gui:

-- control.lua
script.on_event(defines.events.on_player_created, function(event)
  local player = game.get_player(event.player_index)
  local player_gui = player.gui
end)

Adding elements

TO BE CONTINUED...