Learning Lua - A beginner's guide to scripting
Introduction
This is the first guide in the series. The second is Learning More Lua. The third is a collection of useful functions titled Learning Lua Functions.
Before The First Keystroke
Instructions on installation and setup of Atom[berserk-games.com]
Next you should bookmark the documentation knowledge base[berserk-games.com]. You will be referencing this site often once you start to write your own scripts. Its where you go to find Tabletop Simulator specific functions and how they work. You will most often use the API and Objects pages, at least in my experience.
Setting Up
- Set up the table the way you want it.
- Save the table.
- Load the table.
For this exercise, take a blank table and spawn two objects (I used a square block and rectangle block) as well as a red checker.
Remember to save/load then open up the scripting in Atom or go to Host > Scripting in Tabletop Simulator to begin.
1) Global.lua
It is also possible to write scripts and attach them to objects instead of Global. That way if you save and object it will save its LUA right along with it. You can perform most functions using either Global or Item scripts but we will be working in Global.
2) Functions
The message is called a string, and is always surrounded by quotes to indicate that. A string is a series of characters. (Ex: "This is a string." or 'So is this!')When you save and upload your script, it should now print "Hello, World." to chat.
Extra Credit: When you create your own function, you can also pass variables along with it for the function to use. Another way to write our starting exercise would be:3) Objects
Extra Credit: You may be curious as to why we didn't put the object GUIDs directly into getObject (EX: object1 = getObjectFromGUID('195868') ). We could have, it would work. This example was to show you that, sometimes, it is more convenient to set a variable early on so you can reference it later. That way, if that variable needs to change (new GUID) you don't have to try to track it down to fix it throughout your code. If you wanted to, for the checker, there is no reason you couldn't write it like:
4) Buttons
- click_function = String --The name of the function which will activate when this button is pressed.
- function_owner = Object --Determines where the function that the button activates lives (global or an object's script).
- labels = String --The name on the button.
- position = Table --X, Y and Z coordinates for where the button appears, from the center of the object it is attached to.
- rotation = Table --Pitch, Roll and Yaw in degrees, relative to the object it is attached to.
- width = Number --How wide the button is, relative to the scale of its object.
- height = Number --How tall the button is, relative to the scale of its object.
- font_size = Number --Size of the text on the button, relative to the scale of its object.
Now we have a table with the paramiters listed within it. So lets use the object function to create a button on the checker. Enter this inside of function onload() before its end.
After uploading our script, pressing the button should print our message once for each click.
EXTRA CREDIT: This is the perfect point to start playing with different things you can do with objects. Go to the Object page in the Knowledge Database and try stuff. Move the objects, make them switch positions, change their colors, whatever you can think of.
EXTRA CREDIT: Also, any time you press a button, its click_function triggers with 2 parameters. The first is an object reference, specifically the reference to the object the button is attached to. The second is a color (ex. "Blue") in string format of the color player who pressed the button.
5) Logic Statements
What you place in the area I labeled CONDITION in these examples are called relational and conditional operators.[www.tutorialspoint.com] Using them, you can compare many things to eachother. They produce what is called a boolian value (a variable value that is either "true" or "false").
When those lines are used and the button pressed, you will see that only the print functions located in the TRUE statement were printed. Also, because 5==0 is a false statement it activated the print function located in the "else" part of the logic.
Then, in buttonClicked, we will establish some logic to check if trueOrFalse is, well, true or false. If it is true, we'll print that it was true and switch it over to false. If the button is clicked again, it will print that it was false and switch it to true.
We could have also written that as "if trueOrFalse == true then" but that is unnecesary. Remember, the IF statement just needs to be fed True or False. And since trueOrFalse is already one of those, we can skip the operators.
6) Loops
There is another type is ipairs. Pairs is for tables with non-numeric keys, while ipairs is for sequential numeric keys (arrays). ipairs goes in order, while pairs can go in any order.
7) Scripting Outside of Global
When you write LUA here, it is just like global. Except if you need to reference the object the script is a part of, you simply write "self" without the quotes, all lower case. So to create a button on itself, you would use self.createButton(table_of_paramiters).
Closing
I hope this introduction to LUA has helped you better understand some of the underlying mechanics of scripting. If not, then I hope you get lost on your way to throttle me.
Remember, the Knowledge Base has information on all the functions which are a part of Tabletop Simulator. That, and some basic practice with if/else/then and for loops will let you accomplish most anything you want. Good luck.
Another guide has been created, if you are looking for more lessons. Learning MORE Lua is now up. It comes with many scripting examples for you to play with and, I hope, teaches some good coding practices.
Also, if you are on you way to coding, I have started a list of functions to take care of some more complex processes in Lua. You can find it here.
This guide was created by its original author on the Steam Community. Are you the author and want it removed? Request removal.