WSRP Chat on Discord
Open Chat in New Window

Professor Togglebutton's Wildstar Lua 101

2 replies
Posts:
372
Stars:
+386
Let's start at the beginning. Lua is scripting language used in many games. “Gee Professor, what's a scripting language?” Well Timmy, a scripting language is a programming language that is not compiled like traditional programs. They are used to interface with other software. “That's not true, it is complied!” Well Jane, we all know you're technically inclined, but you don't have to shove it in our face. A scripting language is compiled before it run. This means that the raw code is what is distributed to users of addons in Wildstar.

Lua is also an extremely flexible language. Meaning things are a lot less strict than common languages such as C++ or Java. Lua, is not technically Object Oriented, however Wildstar makes use of it in this manner. “What does Object Oriented mean, Professor?” Good question Timmy. Object Oriented programming is a way of compartmentalizing the program. It keeps things organized, and helps with security of the system. “That's not totally correct, Professor!” What did I say, Jane? I'm trying to help the other students understand these concepts. They are all intelligent and can do outside research into Object Oriented Programming on their own. Class, I recommend you do a little research into Object Oriented Programming. It is a much deeper concept than I alluded to, but our little description should help you as we learn actual programming.

So, let's get down into the basics of programming with Lua. There are four concepts you will need to begin: Variables, Comments, Functions, and Operators. “This sounds a lot like Algebra class, Professor.” You're right Timmy, it does. In fact, a lot of programming is just like Algebra. It's a great way to improve your math scores! We'll start with Variables.

A Variable is essentially a container. It is an empty box for us to put information in. Variables, like boxes come in all sorts of shapes and sizes for different things they can hold. At least that is the case in other languages. Variables in Lua are non-typed. Meaning a variable can contain anything you want. You can even change that kind of information is stored in them on the throughout the program. This is very powerful, but as they say in the comics: “With great power comes great responsibility.” In other languages, programmers often name their variables in a way that lets people know what kind of box they are. This is really important in languages that can only have one kind of information in one kind of box. It makes it possible for people who didn't write the code to look at it and understand what is going on.

“Wildstar uses Hungarian Notation, Professor!” Put your hand down Jane, I'm getting to that. Wildstar's addon code, that is the ones written by Carbine Studios for the default user interface, uses a MODIFIED version of Hungarian Notation. “But Professor, I only know English.” That's okay Timmy, it's not actually Hungarian. You should do some independent research on that too. Get out your notebooks class, it's time for some raw information. We're going to discuss types of information that can be stored in variables and how they are written in “Nexian Notation.”

Strings:
Lets begin with the type of variable data called strings. A string is a “string” of alpha-numeric characters. Anything you can type on a keyboard, plus more. A String is basically text. It is one of the most common variable types you will ever use. You store things like character names, location names, or even numbers you don't need to do math with in them. “Professor that's a terrible description of strings!” Jane, stop that or you'll have to stay after school and collect the garbage.
String examples:
Copy
strText = "This is a string of text." strText2 = 'This is also a string of text.' strText3 = [[this is also a string of text, but it is special. you won't use this that often.]] strFibonacci = "11235813" strName = "Professor Togglebutton"

The first thing in each entry is the name of the variable. You'll see “str” at the beginning, this shows that what I intend to have here is a string. Behind that you give a name to your variable, one that is unique to it and describes what it is you want to use it for. You'll notice that I put marks around my string to tell Lua that it is a string. You have a couple options for this: Double Quotes ( " "), Single Quotes or Apostrophes ( ' ' ), or Double Brackets ( [[ ]] ). If you want to have any of these things occur in your string, you have to do a little work around. If you want a string with normal quotation marks, you can designate the string in single quotes. You have two additional options.

Option 1 is to “escape” the quotation mark. You do this by putting a backslash ( \ ) in front of the character. This tells Lua to ignore the special meaning of quotation marks or any character directly after the backslash. “But what if you want a backslash in the string, Professor?” Oh Jane, I hoped you could figure that out. Timmy, can you answer that? “You backslash the backslash?” That is correct Timmy. Good job. Escaping things is more complicated than this, and you should do some independent research on this as well.

Option 2 is to use the double brackets method. A string inside double brackets ( [[ ]] ) will be used exactly as it is written there. This means you CAN NOT escape strings inside it. A backslash is just a backslash inside of double brackets. This can be bad if you want to use a special character like © or ¢, characters you may want but can only be inserted by escaping. This can also be powerful for long strings that would take a lot of escaping, like a lot of text that has quotations inside it. Be careful using the double bracket method.

More String Examples:
Copy
strDoubleQuoteText = "This is some double quoted text. It can have conjunctions with apostrophes in in, like ' it's ' " strDoubleQuoteTextEsc = "This is text in double quote form, with quotations that are escaped. \"Do it like this\"" strSingleQuoteText = ' "But Professor" is totally acceptable here for easy interruption by a program version of Jane. ' strSingelQuoteText = ' If we want to write a conjunction here we do it this way: it\'s. This is odd because we use more apostrophes than quotation marks.' strDoubleBracketText = [[this string is done exactly as it is written. it can have all kinds of characters with no problems. \ " ' are all free game. except for other double brackets.]]

Now, lets see you all write some strings like I did using what we've learned. Next time we'll be discussing numbers, and Tables.

Booleans:
Lets move on to Booleans. Does anyone know what those are? “Is that a ghost with a wooden leg?” Nice try Timmy, but no. a Boolean is a special type of variable. They have only two possible values. “True or False!” Yes Jane, that's right. But no one asked you. Besides, you spoke them with capital letters. That's incorrect, if you write them in your code with capitals, you'll get an error. They need to be written in all lower case. When we write a Boolean in Nexian Notation, we use the letter b to signify it. Like this:
Copy
bTruth = true bFalse = false

“Professor, what about nil?” Jane, that's the best question you've asked yet. nil is a special data type. Every variable we have discussed, and some we still haven't can be nil. “Why did you say that with lower case letters, Professor?” Well Timmy, it's because that's how you have to write it. Just like true and false. “But what does it mean?” Well Timmy, it means nothing. Literally nothing. The absence of anything. A variable that is nil contains nothing, not 0 or false. Nothing at all. Setting a variable to nil clears out any data of any type in that variable. The variable still exists, but for all intents and purposes, it is empty.
Copy
bTruth = nil strName = nil

That's enough on Booleans. Lets move on to numbers.

Numbers:
Now, lets talk numbers, people. Name types of numbers for me. “Whole numbers!” Good one Jane. “Roman Numerals!” Not so good Timmy. Since we've listed two numbers and there are only two real number types in Lua, we'll stop there and go over the two types. “But I read that there is only one type of number.” Yes, that's true Jane, but it's more complex than that. Lua only has one type of number, you can signify it in notation as a variable starting with n. In fact there are two main types of numbers. It's helpful to know the difference and keep track of what type you are using.

In Lua we use Integers, these are all positive or negative whole numbers, including 0. This means no decimal places. For sanity's sake, it's good to use the notation “i” before variable names that you intend to use only integers for. For numbers that need decimal precision, we use what are call floating point numbers, or floats. “Is a list of floats called a parade?” No Timmy, no it's not. We use the letter f to show a variable is meant to be a floating point number.
Copy
nFibonacci = 112358 fPi = 3.14 iPiecesOfPie = -4

These are all numbers, and can be interchangeable, because, to Lua, they are all the same. You could say Lua is number blind. *Groan* *Sigh*.

Tables:
Alright, now all the sissy stuff is done, lets get to the most powerful and most complicated topic in Lua data types. Tables. Who knows what a table is? “That thing we eat on.” Well Jane, I never expected a joke form you. Kudos. How about an array? “We learned about those in math class. It's like a list of numbers.” You're on the right path there. Tables are a list of items. They can contain any kind of data, strings, numbers, Booleans, Other Tables, and even functions. They are very powerful. There are two types of tables. First there are arrays. Arrays are just a list of items. Nothing special.
Copy
arArrayExample = {"One", 2, 3.14, false,}

You'll notice I used “ar” to reference that this is an array. You can do this if you only intend to use a table as an array. The other type is a keyed table, or dictionary. This gives every entry in the table a special name to refer to that by. Basically, it is a set of variables inside the table. We use the notation “t” to begin a table name. This can be for keyed tables or arrays.
Copy
tTableExample = { strOne = "One", iTwo = 2, fPi = 3.14, bFalse = false, }

You'll notice that between each entry in a table is a comma. This lets Lua know that an entry has ended. If you do not put a comma between each entry, there will be errors and you will pull your hair out. You can always have a comma after the last entry, even if it is the last. Lua will not find an error with that. It is good practice to do this so you don't miss a comma elsewhere. In addition to the comma, you will also see that the beginning and end of a table uses curly brackets. “Wasn't that an old black and white movie comedian?” No Timmy, Besides what could you know about old-timey comedians? Anyway. Tables can be written on multiple lines to make them more readable. Like this:
Copy
tTableExample = { strOne = "One", iTwo = 2, fPi = 3.14, bFalse = false, tFibonacci = {1,1,2,3,5,8,13}, }

Here's the crazy thing about tables. They can be both arrays, and keyed at the same time. This is really interesting when we get to referencing the data in a table. “When will we get there Professor?” Right now, Jane.

There are two main ways to reference the contents of a table. One works with arrays and keyed tables, the other, only with keyed tables. All tables can be referenced by calling the name of the table, followed by square brackets with the desired entry's reference. For an array or array entry in a mixed table, this is a number, or index. For a keyed table, it is a quoted string. A variable containing a number or string can also be used. These brackets can also be stacked to go deeper into a table inside a table. “Tableception.” Timmy.... Timmy that's a good analogy but lets not use such an overused reference.

Here's an example:
Copy
tTableExample = { strOne = "One", iTwo = 2, fPi = 3.14, bFalse = false, tFibonacci = {1,1,2,3,5,8,13}, } local fPi = tTableExample.fPi -- This references the entry in the table called fPi local iFibonacciOne = tTableExampletfibonacci"][1] --This refrences the first entry in the array designated tFibonacci

The other way to access table entries is only used for keyed tables. This way is using a dot between the table name, and the key. “You can dive multiple levels this way too?” Yes Timmy, you can. You can also mix the two methods.
Copy
local tTableExample = { strOne = "One", iTwo = 2, fPi = 3.14, bFalse = false, tFibonacci = {1,1,2,3,5,8,13}, } local strOne = tTableExample.strOne local iFibonacciOne = tTableExample.tFibonacci[1]

"Professor, what's with the word local there in front? Why'd you do that?" Timmy, that's a good observation. Local is a concept of scope. We'll get to this more in depth later. But for now, when you declare a variable for the first time, you should put local in front of it. All lower case.

Comments:
Lets move on to comments. If you noticed in my previous examples, I put two dashes before certain lines of text. Those two dashes are one of the ways to “comment” your Lua code. This is so you can leave little notes for yourself or others when they read through it. “Like foot notes in a paper?” Yes Jane, that's a good analogy. Comments have another purpose. They allow you to deactivate part of your program. If a piece of code is not ready to be run and could cause a problem, you can add the comment marks before to cause it not to run. “Professor, that could take FOREVER if it's really long.” That's a good observation Timmy. There is a second way to comment your code. Remember our old friends double brackets? Well when you combine the comment double dashes with them, they become a block comment. Anything between the double brackets will be commented.
Copy
-- This is a commented -- [[ This is a block comment. Anything I put here, including code will be ignored. local arArrayExample = {"One", 2, 3.14, false,} ]]

That's all we're going to cover for now. “But we didn't get to functions and operators.” I know Timmy, those are more advanced topics and this lesson has gone on long enough now. You homework is to write some variables out in the correct notation, with comments for each one about the contents.
EIlMTaR.png
Posted Oct 17, 15 · OP · Last edited Nov 1, 15
Star
x 8
x 8
List
Undo
Soey Benefactor
Posts:
155
Stars:
+117
Your Tableception example is a little broken. Otherwise very good post!
Soey Flamepaw | Aurin, Stalker, Cuddle Consort, Pillow Enthusiast, Hugvertising Expert | Jabbit (EU)
Drakaar Flamebearer | Draken, Warrior, Son of Razak the Oxian, Lord of Stormskull Clan | Jabbit (EU)
Tumblr: OOC | Soey Flamepaw
Posted Nov 1, 15 · Last edited Nov 1, 15
Posts:
267
Stars:
+171
Caretaker
This is some really great information!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~Chef Noriack, owner of Noriack's Cafe Dominion Side. Available for catering of events!
~Noriack's Cafe, 10pm est Saturdays. Where the Science of Eating is Practiced on your Tastebuds!
Posted Nov 1, 15
NoticeNotices