PDA

View Full Version : [Tutorial] Lua Tutorial by Spectrus



Spectrus
06-22-2012, 05:23 AM
[Lua] tutorial
by Spectrus




Chapter 1: Introduction
Hey, I'm Spectrus. This is Lua. Here are some resources.



http://www.studentscomefirst.org/img/external_link.gif (http://forums.xenobot.net/showthread.php?4603-Lua-Tutorial-by-Spectrus&p=48910&viewfull=1#post48910)




Chapter 2: Variables
Dealing with storing, changing, and accessing values.



http://www.studentscomefirst.org/img/external_link.gif (http://forums.xenobot.net/showthread.php?4603-Lua-Tutorial-by-Spectrus&p=48911&viewfull=1#post48911)




Chapter 3: Tables
Branching from variables in to something a little more complex. Storing multiple values within a table and accessing them. Also exploring different possibilities with tables.



http://www.studentscomefirst.org/img/external_link.gif (http://forums.xenobot.net/showthread.php?4603-Lua-Tutorial-by-Spectrus&p=48912&viewfull=1#post48912)

Spectrus
06-22-2012, 05:23 AM
Chapter 1: Introduction

Hello!

Hello, my name is Spectrus. I'm a staff member here at XenoBot forums. I've written an introductory tutorial to the scripting language Lua. Lua is the scripting language of choice for XenoBot. It can be used to create advanced cavebot scripts with near limitless features (including depositors, refillers, anti-KS). The possibilities with Lua are extensive and I'm going to teach you how to use it. I will apologize in advance, this tutorial is aimed at people who have never looked at any programming or scripting language in their life and are not familiar with the general syntax and format. I will cover it all, and at times it may be boring to those who have prior knowledge. Tough it out, you might still learn something.

Basics

Lua is a scripting language that is designed and maintained by a team from Brazil. It's name, Lua, is the literal word for moon in Portuguese. In fact, it's the name for the moon. Because it's a name, it should be capitalized. Also, it's not an acronym. It should not be spelled LUA. It's correctly spelled Lua. That's one of those minor things that irritates the hardcore enthusiasts.

Moving on from what you can glean from the about section of Lua's homepage, let's discuss why it's important to understand the basics of Lua before jumping in to scripting for XenoBot.

It is completely possible to assemble scripts that do your wishes by simply copying and pasting sections of code that you find around the forums. And while this may be feasible, and acceptable to some, it is my hope that our users will learn to become more self-reliant. By learning the basics of the language in which scripts for XenoBot are written, it will be easy to create anything that you may have a need for without having to ask or search for it.

Resources

Lua Homepage (http://www.lua.org/)
Demo (http://www.lua.org/demo.html) (Good for testing code)
List of Xenobot functions (http://www.lualand.net/functions) (Incomplete)

Spectrus
06-22-2012, 05:23 AM
Chapter 2: Variables


What is a variable?

By far, the best analogy of a variable I have seen is that of a box. Let's pretend that a variable is like a box. You can put things in the box, you can see what's in the box, and you can change what's in the box. The same is true for variables. A variable is a storage place to put values. Also similar to a box, it's hard to know what's in it unless it's labelled. Every variable needs a name. So what does this look like in Lua?


myVariable = 12


Here, we've created a variable with the name myVariable and given it a value of 12.


I'll talk a lot about good programming practice throughout the tutorial. This is one of those times. Unless explicitly declared, a variable is global. We typically want to work with local variables, not global. We'll discuss what this means when we get in to the topic of scope. Anyways, it is good practice to declare variables like this:


local myVariable = 12


This does the same thing as the above example, except we've made the variable local.


Getting back to the analogy of the box, boxes can hold many things. They can hold shoes, they can hold computers, they can hold cups, they can even hold other boxes. Variables, once again, are the same way. They can hold many types of data. Such as... numbers, sentences (known as strings), true/false (known as boolean). Here is an example of each:


local myNumber = 12
local myNumber2 = 5.15125617
local myString = "Hello, XenoBot!"
local myBoolean = false


A whole number, a decimal number, a string, and a boolean in that order.


At this point, I'm going to introduce two functions which are a part of Lua. Don't worry, we'll go over functions later. The functions I'm going to introduce are print() and type().

print() is the default method of outputting information. If you want your script to have some sort of text output, you will need to use print() to see it.

type() is important to variables. If you provide it with a variable, it will tell you what kind of variable it is. Meaning it will tell you if it's a number, a string, or anything else.

Here is an example using these functions. What the script would output will be marked by a '>' character at the beginning of the line. With XenoBot, any use of the print() function will result in a green message in game.


local myNumber = 12
local myType = type(myNumber)
print(myType)
>number

local myString = "Hello, XenoBot!"
local myType = type(myString)
print(myString)
>string


Displaying what kinds of variables we have using type() and print().


Naming Variables

This is important. It has it's own section, even. A big part of understanding what is happening in a script is coherently named variables. For instance, if I wanted a script that healed at a certain health I could put something like:


local a = 100


but then when I try to read through it later, I need to remember what a is for. It would be much better if I did something like this:


local hpToHeal = 100


This way, I can clearly understand that whatever value I have put in that variable is intended to be for what hp I want to heal at. Variables can not have any spaces in their name. Thinking of good variable names is important. Also, it's important to make sure that they are readable and as short as possible (without, of course, jeopardizing understanding). Here are some examples of what to do, and what not to do:


local variableToRememberHPThatIShouldHealAt = 120
local a = 120
local hEAlthTOheaL = 120
local HEALTHTOHEAL = 120

local healthToHeal = 120
local HpToHeal = 120
local HealAtHP = 120


Variable name examples. Top are not a good idea, bottom are more recommended.


As you may have guessed, the top four are bad examples, and the bottom three are good examples. To learn more about how to form variable names some good conventions are Camel case and Pascal case. Some information can be found here (http://msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71).aspx) (microsoft.com), but for a more in-depth explanation try googling each one and reading up on it.

Accessing Variables

Alright, we now know how to make variables. The important question is now how do we use them? It's actually quite easy. Once we've made a variable that has a value, we can use it's name in place of the value. What I mean is that if you have a variable called myVariable that has a value of 12, you can then use myVariable in your code instead of writing 12.


local myVariable = 12
local anotherVariable = myVariable


Above, we've created the variable myVariable and set it's value to 12. Then, we've created the variable anotherVariable and set it's value to whatever myVariable is. That means that anotherVariable is going to equal 12.


An important note is that a variable must exist in order for you to access it. If you try to access a variable that does not yet exist, you will receive an error. An example of this would be:


local myVariable = myOtherVariables


This would result in an error. The variable myOtherVariable has not been created and therefore can not be found.


Now that we have the basics of variables understood, let's move on!

Spectrus
06-22-2012, 05:23 AM
Chapter 3: Tables

What is a table?

A table is a special kind of variable. That's right, I tricked you. You read all that stuff above and thought you had finished with variables once and for all. Well, guess again!

To make a table, you choose a name and instead of just one value, you put multiple values inside curly brackets. Each value needs to be seperated by a comma. In it's simplest form a table looks like this:


local myTable={}
print(type(myTable))
>table


Making a table and printing it's type. Note that you can nest functions so you don't need a variable to store the type.


Basically, a table is a bunch of different types of variables all stored under the same name. With tables, you can access each and every part of it as if it was it's own variable. Let's create a table with some values and then access them and print them out.


local myTable = {2, "hello!", true, 5.1}
print(myTable[1]) --Prints the first value of your table.
>2
print(mytable[2] -- Prints the second value of your table.
>hello!
print(myTable[3]) --Prints the third value of your table.
>true
print(myTable[4]) --Prints the fourth value of your table
>5.1


Accessing information in a table. Note that anything following the mark "--" is a comment. It is disregarded by the script.


In the same fashion, we can add things to our table.


local myTable = {}

myTable[1] = 5
myTable[4] = "Hello, XenoBot!"

print(myTable[1])
>5
print(myTable[4])
>Hello, XenoBot!

print(myTable[2])
>nil --There is nothing in the second spot of myTable.


Creating an empty table then inserting values at specific spots.

Extras

Included with the table variable are some important feature. The first of which is a method of finding how many things are in your table.

Incomplete.

Spectrus
06-22-2012, 05:23 AM
reserved...

Spectrus
06-22-2012, 05:23 AM
reserved....

Spectrus
06-22-2012, 05:23 AM
reserved.....

Spectrus
06-22-2012, 05:24 AM
reserved......

Spectrus
06-22-2012, 05:24 AM
reserved.......

Spectrus
06-22-2012, 05:24 AM
reserved........

Spectrus
06-22-2012, 05:30 AM
This has been sitting in staff section for some time, I haven't found the motivation to work on it. Figured I'd post it to at least get the beginning of it out there to help folks learn, and I'm thinking that if it's public then I'll be more motivated to finish it. Good luck everyone!

thorekz
06-22-2012, 07:58 PM
Its great until now. Loved the hp example, keeps you motivated

braedan13
06-23-2012, 06:54 AM
This was so nice and clear to read ... Thanks alot.. I hope to see more chapters soon, would love to keep learning thanks a lot

Xongiver
06-23-2012, 11:50 AM
Awesome job, I really appreciate it.

Hollander
06-23-2012, 03:27 PM
Finaly someone who's willing to explain.......

Keep up the good work,

Well done...:D

Y2Quake
06-23-2012, 03:35 PM
<333 i thought u were to lazy for make this kind of things.

forumname
06-23-2012, 03:55 PM
LMAO read this and WTF im so confused ^.^ still dont see how it makes me leave my spawn and deposit my items xD

Ropiderz
06-23-2012, 07:16 PM
I really liked your tutorial, hope you will work on it until hardcore Lua programming. Continue the good job mate.

Johnnybror
06-24-2012, 11:16 PM
Really nice, would loved to see it completed as it isn't sufficient for scripting by your own at the current moment! Think the current part are great reminders and your way of explaining is really really good man! Keep up the good work!

forumname : I'd personally recomend you to look at some finished scripts and begin by copy-pasting others, this will force you to understand the basics since every script has its own uniqueness! After understanding way of order and so on these tutorials are really easy to understand as long as they are as well written as this one!

forumname
06-25-2012, 01:47 AM
Yeah i'll do that also i found another tutorial on depositing that is really nice also. Tomorrow i'll start testing on copy/paste on already own scripts and try to figure this out I really want to understand how they are made :).

Davulo
06-25-2012, 07:28 PM
OmG... I love you man!
Thanks so much for this guide, helping out the newbies here. I appreciate so much ur hardwork bro. U are an amazing fella.
If u have any websites like Lua for Dummies, please don't forget to post them so that we can read them and learn more, so we don't have to leave you all the work.

Appreciate it ur time again man.

Matthew597
06-30-2012, 08:20 AM
First thank you. Second I am pretty good with basic programming languages. I would love a more complete intro tutorial on writing LUA scripts. I know I can analyze others scripts and maybe figure it out and ask questions, but a brief overview would be very much appreciated. Can anybody help you or point me in the right direction.

Thank you
Regards,
Matt

Lateralus
06-30-2012, 08:50 AM
Very usefull info! Thanks!

Spectrus
06-30-2012, 07:06 PM
Matthew597 Look no further than the Lua homepage, link is in Ch1. I will be going away this weekend, when I get back I'll put some time into adding on this.

bluu769
07-01-2012, 08:03 PM
hi is not like putting lua scripts in the xenobot

krychu280
07-03-2012, 07:54 PM
I can not cope with finding a script at lifting food from ground which lies before me.
Can you help? I want skill :)) !

Tepre
08-28-2012, 03:18 PM
This was Awesome hope you finish it.

nacho_125
10-17-2012, 09:38 PM
Matthew597 Look no further than the Lua homepage, link is in Ch1. I will be going away this weekend, when I get back I'll put some time into adding on this.

brother i do not know the reason why you stopped this, even though i can see it can be very lenghty and time consuming, but in my own opinion i think that besides the bot itself, this is the best project/thread that i have seen. "give a man some fish and feed him for a day, teach the man how to fish and feed him for life"

Abutaka
01-11-2013, 03:48 AM
Thanks for the tutorial, it was great and well explained!
I wonder when will you continue with it? And possible if you can do more advanced "teachings" if possible. Would appreciate it

Fuzzy
01-11-2013, 02:28 PM
good job spec . i wanna learn how to write LUA . HAHAHA i mean Lua scripts for the bot . i have learned some C# to write custom comabt routines for a WoW bot and im excited to learn how to write for Lua and love how you explain the hp part since it goes with the bot itself it helps me learn if u explain code that you use in a script that the bot uses .i understand alot with out knowing any Lua just knowing the tad bit i know from C# and its great to have some kind of understanding of something . keep it going would love to learn more and cant wait for u to add more

parrode
01-24-2013, 02:55 AM
Where can I find the list of Xenobot's functions and variables?

It will be a lot easier if I get this list to code my own scripts :)

Stusse
03-07-2013, 11:27 PM
Where can I find the list of Xenobot's functions and variables?

It will be a lot easier if I get this list to code my own scripts :)
C:\Programs\XenoBot\Data\XenoLuaLib.lua

Arwen
05-08-2013, 12:35 AM
Hey Forgee I have question for u ! Is it possible to make script ' Use with item on the target ? I want to make script which can tame midnight panther :)

malakz
06-18-2013, 03:01 PM
Awww, I thought I finally could learn how to use Lua.. Then it ended :[

DarkRoses
08-09-2013, 12:57 AM
Continue brah! :)

Linty
08-16-2013, 10:11 PM
More info on tables!

fl0w
09-23-2013, 10:39 PM
This is awesome, considering university doesn't start until 30th, I have plenty of time to work on my scripting. Highly appreciated, seems to be laid out well and well written.

Gahariet
09-24-2013, 12:22 AM
Great tutorial! It helped me a lot, thank you!
-edit-
Please continue!

dumdumpop
10-29-2013, 07:46 PM
This is extremely well written and useful! I only wish it was finished :( but either way great work I'll try to make a script with what I've learned so far :D

Adrstalik
08-20-2014, 04:49 PM
If you all want to learn some lua I can propose this ebook. It's exactly it what you want to know about variables, constants, tables and other. If you use your brain it means you CAN use knowledge from this book. It's in -- .mobi format. You can open it by programs like "Calibre" for example. Here's the scan CLICK ME (http://virusscan.jotti.org/pl/scanresult/a42b77e2b4f761ce38e722280748ba67869bd6d1), you scared little bitch. And here is link to download : CLICK ME TOO ! (http://www.speedyshare.com/9MT2f/Programming-in-Lua-3ed-Roberto-Ierusalimschy.mobi)

Tibiaperito
08-21-2014, 03:19 PM
It's such an outstanding tutorial, too bad it ended prematurely :) I've digged through many threads on this forum and must admit that this is best I've come across :) Never seen anybody explain so well and write in such good fashion! If only you found time to add some more informations.. And remember stick to the basics only - when basics are clear all other will become understandable :) Hope you'll continue :)

Syntax
08-21-2014, 09:34 PM
He's too busy streaming himself being bad at games :)

Tibiaperito
08-22-2014, 08:15 AM
Hehe, I won't lose hope, ever ;)