Today we’re learning hardcore ancient Egyptian algebra.
Just kidding. Today’s lesson is way easier than ancient Egyptian algebra… Today we’re learning about variables, lists, and tables. We learned about data nodes in a previous tutorial. Today we’re going to dive a bit deeper, and look at various ways of using them.
We’ve talked about variables, and mentioned that they can be both text and numbers. Most of the time, UBot Studio will automatically figure out which you intend for the variable to be. If you set a variable to “1”, then it will assume you mean for it to be a number. But if you set it to “party”, then it will assume that it is text. If you need the literal text “1”, then you can click the parameter’s quote button:
The command you’ll use most frequently with variables is the set command. This is a simple command that just takes a variable, and gives it a value.
If that value happens to be a number, you can use the increment and decrement commands.
These commands are particularly useful if you are running through a loop and you need to keep track of how many loops iterations have passed.
You’ll also want to take a look at the text functions. While they aren’t necessarily directly tied to variables, they provide a common way to modify the data in variables. We won’t go through each and every function, but here’s a quick example of the $replace function in action.
Learn about all the text functions in the wiki.
Variables are pretty straightforward. Let’s talk about lists. If a variable is like a post-it note, a list is like a pad of post-it notes. Lists are collections text or numbers. A list can also be a mix of text or numbers. To create a list, you’ll use the add item to list command. The first time you add something to a list, the list will exist.
There are also a number of functions that return lists. For instance, you can grab a list from a file with $list from file. In these situations, you’ll want to use use the add list to list command instead of add item to list.
UBot Studio provides an easy way to flip through your lists – the $next list item function. $next list item returns an item in the list, and keeps track of its position in the list. But be careful! If you use $next list item more times than there are items in your list, you’ll get an error. Also, note that lists in UBot Studio are zero-based, meaning the first item is item 0, the second item is item 1, and so on. Let’s look at add item to list and $next list item working together.
When using $next list item (or $previous list item), you can peek at the list’s current position with $list position. You can set the list position with the set list position command. This is most useful when you’ve reached the end of a script, and you need to start it over at 0.
There are many more useful list commands and functions, and I encourage you to explore the wiki to learn about each of them.
A table is like a spreadsheet of variables. It’s like a grid of post-it notes laid out on a surface. Like lists, a table is created when you first start adding to it. The primary way of adding items to a table is via the set table cell command.
You can later read a particular data cell with the $table cell function. Like lists, tables are also zero-based, so the upper left cell will be 0,0. UBot Studio also provides a number of useful functions for converting from lists into tables and back again. You can add a list to a table with add list to table as row or add list to table as column. To get a list based on a row or column of a table, you can use $list from table.
UBot Studio has a magical little command for all your data saving needs: save to file. With this command, you can place text or a variable in the “Content to Save” parameter and it will save to the specified file, just as expected. The magical part is that you can also also save lists and tables by simply dropping them into the same parameter.
When you save a list to a file with this method, it will save the list with one item on each line, like so:
The things I did
The things she did
The things neither of us did
When you save a table with this method, it saves to the csv format, which looks like this:
Eh, bee, see
Won, too, three
Dough, ray, me
Even more magical, you can load these files back into your lists and tables at a later time, by using $list from file for lists and create table from file for tables.
When your script finishes running, the data will still be in it. If you run the script again, your variables, lists, and tables will already exist with the old data. For this reason, it’s a good idea to clear your data at the beginning of each script. UBot Studio has several commands for this. For variables, you can just set them to some default value. For lists, use clear list and for tables use clear table. You can also clear all your data in one go with the clear all data command.
And in conclusion….
There is a lot of data out there on the back of that big hairy data beast called the internet. Hopefully this tutorial has offered you some insight into how you can begin to tame the beast, and turn some of that data into profit. Or at least into uBots. And then the uBots make you profit. Yeah.