← Back

Gui Basics

Written by Fagiano0
In this tutorial you’ll learn how to make a simple gui with a button that triggers code, while learning the basics in the process.

CONTENTS:

Installing MyGui Editor

Before we can create our guis we’re gonna need to install the MyGui editor.
You can download a compiled version for Scrap Mechanic 0.6.x from here (compiled and uploaded by QuestionableMark)

Once you’ve installed and unzipped it, open the release folder inside of it and open LayoutEditor.exe, you can also create a shortcut for easy access to it.

Creating a Basic Gui

Now that you’ve opened the editor, let’s create a basic gui. On the right you will see there are multiple tabs, including Widgets, click it. The buttons here are used to select the type of widget you want to add, then to actually create it you have to click and drag your mouse inside of the gui (checkerboard pattern)

Note: Some tabs like Default, Other, SM BG Panels, SM Background, etc. will only be visible if you expand the side panel.

WidgetTab

Again, for this tutorial we’ll be making a gui with a button that triggers some code, so:

Accessing Your Gui with Code

Now that we’ve made a gui, let’s access it and make it interact with our code.

The following example is a part script that opens the gui when interacted with and sends a message to the player when they press the button.

MyPart = class()

--Called when the part is created.
function MyPart:client_onCreate()
    --Creates the gui from the provided layout file, with some extra settings.
    --Change the path to where you put your gui layout file.
    self.gui = sm.gui.createGuiFromLayout( "$CONTENT_DATA/Gui/Layouts/CoolGui.layout", false, {
        isHud = false,
        isInteractive = true,
        needsCursor = true,
        hidesHotbar = false,
        isOverlapped = false,
        backgroundAlpha = 0.0,
    } )
    --Binds a callback to be called when the button is pressed.
    --Change "MyButton" to your button name.
    self.gui:setButtonCallback( "MyButton", "cl_onButtonPressed" )
end

--Called by the button and receives the buttonName.
function MyPart:cl_onButtonPressed(buttonName)
    --Sends a message to the player that pressed the button.
    sm.gui.chatMessage(buttonName.." pressed!")
end

--Called when the player interacts with this part.
function MyPart:client_onInteract(character, state)
    if state then
        --Opens the gui.
        self.gui:open()
    end
end

General Advice

Theme  Moonwalk