Jump to content

Search the Community

Showing results for tags 'lua'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • Introductions and Resources
    • DarkMatters: Rules and Feedback
    • DarkMatters: Introductions.
  • Sacred - Underworld, Fallen Angel , Citadel, Sacred 3
    • Sacred 2 Mods and Modding - The new future for Sacred 2!
    • Sacred 2 General Discussion (PC)
    • Sacred 2 Console (XBOX and PS3)
    • Sacred 2 Guides, Maps, Walkthroughs and Videos
    • Sacred Wiki
    • Sacred 2 Fan Art and Fiction
    • [D.a.r.k] Island Fortress
    • Sacred 2 Bugs, Feedback and Troubleshooting
    • Sacred Underworld
    • Unbended - Kickstarter project by former Ascaron employees who created Sacred!
  • DarkMatters General Public Forums
    • Key Largo Beach Resort and General Discussion
    • Media Matters
    • Tech Talk
    • New and General Gaming
    • The Dark Kitchen
    • The Creatrix
    • The Daily Grind: Real life right here!


  • Sacred 2 Demo (UK)
  • Fallen Angel
    • Fallen Angel Builds
    • Fallen Angel Items
    • Fallen Angel Patches
    • Fallen Angel Tools
    • Fallen Angel Miscellaneous
  • Ice & Blood
    • Ice and Blood Builds
    • Ice & Blood Items
    • Ice & Blood Patches and Mods
    • Ice & Blood Miscellaneous
  • Community Patch
    • Community Patch Builds
    • Community Patch Items
  • Sacred
  • Underworld
    • Sacred Mods
    • Underworld Builds
    • Underworld Items
    • Underworld Patches
  • Download Sacred 2 Gold
  • Weekly Featured Mods

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start




Website URL






Favorite pizza topping

Why do you want to join DarkMatters?

All time best video game ever played

Real Name

Found 1 result

  1. The goal of this thread, to provide you the reader with a basic foundation in the lua scripting language. Which is essentially the supporting language for almost every file in the scripts directory. This guide will not cover command line lua, if you use windows as I do then I recommend installing https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/luaforwindows/LuaForWindows_v5.1.4-46.exe Comments, Naming Convention and Structure Comments in every programming language give the programmer and the non-programmers a general idea of what is going on in the code, especially when that code becomes excessively large and or complex. There are 2 types of comments in lua Single Line Comment: -- This is a single line comment, everything til the end of the line will be commented out & ignored by the lua interpreter Multi-Line Comment: --[[ This is a multi-line comment, everything that is contained within the --[[ ]] is ignored by the lua interpreter ]] Technically speaking there is an unwritten 3rd type of comment in every language, which is called the naming convention although a naming convention falls under a consistent style of program (e.g. nameOfPlayer, name_of_player etc..) it can also be used as a comment style of programming, lets look at an example. local s, x = 18000, { p = 0.50, f = 0.10} What the heck is s, x, p & f? Nevermind what the values they hold but what are s, x, p & f used for? It is not very clear and it would take a bit time to go through the code to see what these things are used for. So lets rename these letters so that they describe their purpose. local storage, xpAdd = 18000, { bronze = 5, silver = 10} Now we have a general idea of what these things will be used for even if we aren't a programmer. Structure is also an important part of readability it allows us to see the level of execution, find bugs and make our less then savy code seem a bit sexy When we talk about structure we are referring to proper spacing & indentation of the code. local config = { xpAdd = { bronze = 5, silver = 10 }, storage = 18000 } Variables A variable is simply a container used to hold a value, think of a variable as a box where you can only hold 1 thing at a time in that box. A variable must start with a letter or underscore and can be 1 character to the entire alphabet in length, variables can also contain numbers, letters or underscores but absolutely no spaces, variables are case sensative meaning wow, WoW & wOw are all different variables. The way we declare a variable is by typing the variable's name followed by an equals sign ( = ) and the value you want to store in the variable, the equal sign is known as the assignment operator. local myVariable = 5 -- a variable with a default value of 5 The reason it is called the assignment operator is because we are taking the thing on the right and assigning it to the thing on the left. Tables A table is similar to a variable but instead of just holding 1 value it can hold multiple values & even other tables. The basic definition of a table is : local myTable = {} -- this is an empty table Tables can also be assigned properties at any time, either during its declaration or as we progress through the script. local myTable = { name = 'Codex' } The tables property value can then be accessed by simply using the name of the table followed by the dot operator ( . ) and then the property's name local myName = myTable.name -- assign the value stored in myTable.name to myName Tables can also be assigned a property based on its index value, think of a table as a bookshelf and its index value as one of its shelves. Let's create a new table. local myNewEmptyTable = {} And assign it a value of 10 at its 1st index, unlike other language lua's index by default starts at 1. myNewEmptyTable[1] = 10 This also could have been applied like this myNewEmptyTable[#myNewEmptyTable + 1] = 10 Or even this table.insert(myNewEmptyTable, 1, 10) Although the index value of a table normally starts with 1, you can force lua to start the index at 0 or any index you'd like simply by writing. local table_ = { [0] = 23, -- index 0 27, -- index 1 89 -- index 2 } local table_ = { [0] = 23, -- index 0 [5] = 27, -- index 5 [100] = 89 -- index 100 } Tables can also be assigned tables as properties. It is important to note that if you have more than 1 property in a table you must place a comma ( , ) prior to the last property listed. Example: local myNewEmptyTable = { 10, -- index 1 innerEmptyTable = {} -- index 2 } Lets say you wanted to add a property with a value to the innerEmptyTable of myNewEmptyTable, you would simple call myNewEmptyTable followed by the dot operator followed by the property innerEmptyTable followed by the dot operator followed by the new property name followed by the assignment operator followed by the value. Example: myNewEmptyTable.innerEmptyTable.someProperty = 34 To access the value stored in someProperty of the nested tables you call the nested tables just as you did above except omitting the assignment operator and the value. local x = myNewEmptyTable.innerEmptyTable.someProperty If tables have property names then they can be accessed either using the dot operator (table.property) or by using the the name of the property enclosed in single or double quotes (known as a string) enclosed in square brackets (known as a string index). Example: -- Using the dot operator ( . ) local x = myNewEmptyTable.innerEmptyTable.someProperty -- Using a string index local x = myNewEmptyTable['innerEmptyTable'].someProperty -- or local x = myNewEmptyTable['innerEmptyTable']['someProperty'] -- or local x = myNewEmptyTable.innerEmptyTable['someProperty'] Tables can also be assigned tables as indexes local myTable = { [{1,2}] = "hi there", [{3, 4, 7, 10}] = "weird eh?" } We won't go into how to access table's with table indexes for now until we cover functions a bit later. Tables can also be assigned tables with no property names. local tableOfTables = { {2, 4, 6}, -- index 1 {1, 3, 9}, -- index 2 {5, 8, 7} -- index 3 } Lets say we wanted to access the value of 9 from tableOfTables, we would use its table index value followed by that tables index. Example: local x = tableOfTables[2][3] -- this will store the value of 9 in x Anytime you see text enclosed in double ("") or single ('') quotes this type of data is known as a string, a string can be 1 character like "A" or can be many characters like "Hi my name is Codex :)" A table can also use a string as a property name in lua but it must be surrounded by single / double quotes and surrounded by square brackets ( [] ). Example: local myStringTable = { ['a short sentence with spaces :)'] = 56 } print(myStringTable['a short sentence with spaces :)']) -- prints 56 to the console Multiple Declarations Both tables and variables can be declared on the same line simply by using a comma ( , ) after each table or variable, the assigned values of each type (variable or table) must be assigned their values in the order they are declared. Example: -- Just tables local myTable1, myTable2, myTable3 = {}, {}, {} -- Just variables local myVar1, myVar2, myVar3 = 1, 'B', "some text" -- Both tables and variables local myVar, myTable = 33, {} Although we have not covered functions just yet, I wanted to exhaust the possibilities of tables. Tables can also be assigned properties which are functions. Example: local myTable = { [1] = function(x) print(x) end } The way we would execute our table's function is simply by calling the table name followed by its index or if it has a property name followed by an open and closed parentheses () and any argument(s) which we want to pass to the table's function's parameter(s). In this case our table's function does have a parameter labeled x (more on this later). print( myTable[1](10) ) -- prints 10 So far we have seen this word local used quite often through out the start of this tutorial, we will not explain its significance just yet, not until we cover functions.
  • Create New...
Please Sign In or Sign Up