Spotter | Creating the Gui script.
- In the assets panel, right click, 'main' and select, 'New...', 'Gui script'.
- Name the script, 'title' to match the other related components and click, 'Create Gui Script'.
- In the assets panel, double click the file, 'title.gui_script'.
The script looks just like any ordinary script with the boilerplate function stubs. However, abstracted from you is that this script is pulling in additional functions from a library that give you commands to use with the nodes in a Gui file.
- Change the functions to respond to clicking the buttons on the Gui.
function init(self)
msg.post(".", "acquire_input_focus")
end
function final(self)
msg.post(".", "release_input_focus")
end
function on_input(self, action_id, action)
if action.pressed and gui.pick_node(gui.get_node("button_level1"), action.x, action.y) then
msg.post("main:/controller", "show level 1")
elseif action.pressed and gui.pick_node(gui.get_node("button_level2"), action.x, action.y) then
msg.post("main:/controller", "show level 2")
end
end
When the Gui is shown it attains the input focus. When the Gui is closed it will release the input focus.
When the left mouse button is pressed on top of the node "button_level_1", send a message to the main.collection controller game object script with the string, "show level 1".
Note how we do not need to use collision objects to detect the mouse press in a region of the screen when using a Gui script. The key bindings have a default for the left mouse button (Button 1) already set in the game.input_binding file.
Button_level_1 is the Id of the node on the Gui file. In the next step we will attach this script to the Gui. It knows we are referring to the nodes on the attached script.
"show level 1" is a string that we are listening out for in the main.script on_message function: elseif message_id == hash("show level 1") then...
The same approach is then used for the level 2 button.
Finally we need to attach the Gui script to the Gui object. This is done in a slightly different way to other game objects because the name of the script file is a property of the Gui.
- In the assets panel, in the 'main' folder, double click, 'title.gui'.
- In the properties panel, change the 'Script' property to, '/main/title.gui_script'. You can use the three dots to select this from a list.
- Save the changes by pressing CTRL-S or 'File', 'Save All'.
- Run the program by pressing F5 or choosing, 'Debug', 'Start / Attach' from the menu bar. You should be able to click on a button and it will show that level of the game. Of course the levels are just blank screens at this stage.
That's the basic interface complete. Now we need to make a game. Stage 3 >