Simple GUI
This tutorial teaches you to make a simple Graphical User Interface, or "GUI", with a compound interface and buttons. Here we will make an agent that allows us to:
- teach the vocabulary to all the creatures in the game
- save the game without quitting
- quit the game without saving
First, we need a set of sprites. The images are arranged like this in the .c16 :
- sprite 0 : the main part of the sprite
- sprite 1 : the 'teach' button
- sprite 2 : the 'save' button
- sprite 3 : the 'quit' button
Next, we install the agent in the game using the NEW: COMP command to make a compound agent. For attributes, it will be floating (32), invisible to creatures (16), and pickupable (2).
inst new: comp 1 2 11111 "tutogui" 4 0 9900 attr 50
Next, we begin creating the buttons with the 'teach all creatures' button.
pat: butt 1 "tutogui" 1 4 1 1 0 [] 3000 0
Here is what the numbers mean:
- 1 - it is the number of the created part - the main part of the agent has part 0
- "tutogui" refers to the name of the sprite file used for the part
- 1 tells the game that the image used for the button is the image 1
- 4 tells the game that there are only 4 images in the sprite file
- 1 is the x position, relative to part 0 (the base of the agent)
- 1 is the y position, relative to part 0
- 0 is the plane difference between the base and the button
- [] - if you want an animation when you roll your mouse over the button, you can put it here
- 3000 - it is the number of the message sent by the button when pressed
- 0 means that the button will be activated even if you click on a transparent pixel of the button sprite
Next, we create the 'save game' button.
pat: butt 2 "tutogui" 2 4 22 1 0 [] 3001 0
And the 'quit' button.
pat: butt 3 "tutogui" 3 4 43 1 0 [] 3002 0
As you've seen, the number of the message sent changes with each button - you can use pretty much any unused number, but you should normally use different message number for different parts of an agent. This is because the message number tells the agent which script to run when the button is pressed; if two buttons use the same message number, they will run the same code when pressed.
mvto cmrx cmry
cmrx and cmry are the 'x' and 'y' position of the middle of the screen - mvto means that the agent will move to the camera's central (x, y) coordinates.
endm
This is the end of the install script.
Now we have to define what will happen when we will press the buttons, by using the message script numbers chosen for each button.
The 'teach' button:
scrp 1 2 11111 3000
The '3000' is the number of the called script - the message sent refers to a script number.
enum 4 0 0 vocb next
Here we use enum to go through all the agents matching the '4 0 0' classifiers - this means all creatures - then use 'vocb' to teach them their vocabulary. 'next' closes the enum command.
endm
Script for the 'save' button:
scrp 1 2 11111 3001 save endm
Script for the 'quit' button:
scrp 1 2 11111 3002 quit endm
And finally, the remove script
rscr enum 1 2 11111 kill targ next
Original tutorial by zareb.