Edit: This tutorial is old, and a lot of it isn't 100% accurate. See my new LUA tutorial here for the new "Tables" tutorial for more accurate information.Important: You must know basic LUA for Bully before attempting this tutorial.
In this tutorial, I will be teaching how to use arrays and tables. They can be VERY useful is used right, and I don't see them used by many people except for me and 2 other modders.
ArraysAn array is a type of variable, with multiple values. Here is an example:
donut = {}
donut[1] = 42
donut[2] = 9000
donut[3] = 150
donut[4] = 200
donut[5] = 396
TextPrintString(donut[3],1,1)
Now let's dig in, the first part "donut = {}" made it an array. When you want to make an array, make it equal "{}".
Remember, an array is a variable with multiple values. So to access one of the values use the [valueNum] after the name. So if you wanted to access the 4th part of the "donut" array we made, you'd use "donut[4]". In the example you can see that we made the 4th value of donut equal 200. So anytime we used donut[4] it would be 200. Then the TextPrintString is a Bully LUA function, which if you know Bully's LUA you know what it does. It prints on screen what is first in the (), then the number after it (Separated by a comma) is the time shown, and then last number is top or bottom (1 = top, 2 = bottom) of the screen. So it'd print 150, since donut[3] = 150.
You could also use a repeat code to set many values to the array. Example:
cheese = {}
x = 0
repeat
x = x + 1
cheese[x] = x*2
Wait(0)
until x >= 500
That code first makes the variable x equal 0, then repeats the code until x is 500 or more. Every time it repeats, it will first add 1 to x and then set the part of the array x is equal to to double x. So, if x equaled 1 (Which it will the first time it runs) then it'd be like this: "cheese[1] = 1*2" then does that for the next 500 parts of the array. So eventually when it is at 500, it'd be like: "cheese[500] = 500*2". There are many other things that you can do with an array that are a bit more useful. Here is one use:
Note: Learn the 'character' variable type before looking at the below example. If you need to learn about the character variable type then just keep reading. Otherwise skip down to the end of the next paragraph.
A character variable type is similar to a string (A string is text, with "" around it, for example: crap = "yo bro this a string right here" would be the variable "crap" equal to the string of text "yo bro this a string right here") except with a character, it is just a single character. Not a whole string of text, a string is multiple characters and a character is just 1 character. Also, you use the 'single quotes' not the "double quotes" for a character. Example: donut = 'f', would be a variable named "donut" that equals the character f. A character can also be a space ' '.
So now we can continue with arrays, here is an example of an array using character types. I also show you a new thing about arrays that is very useful.
word = {'h','e','y',' ','w','h','a','t',' ','i','s',' ','u','p','?'}
x = 0
currentText = ""
repeat
x = x + 1
currentText = currentText..word[x]
TextPrintString(currentText,1,1)
Wait(100)
until x >= table.getn(word)
2 new things to explain here. First off, you notice I didn't use "word[1] = 'h'" and so on for the rest to assign values, but instead put them all inside the {} to begin with. Just separate values via commas and you can put the array in the {}. You can still make more values with array[element] still of course. Then also, the "table.getn(word)" is new to you, it may look like it's just for tables but it applies to arrays too. It will get the length of the array you put inside the (). So, now to run through the entire code. It produces a very cool effect. It will write a word 1 letter at a time. Simulate typing on screen. I'll go through the whole process of the code now.
First, we made our "word" array with multiple characters in it. All the characters together saying "hey what is up?"
We make a variable 'x' that is just a number to use with the repeat loop.
Then, a string called currentText that is just a blank string at first.
We repeat the code to add 1 to x,
make add the next character of the array "word" based on what x equals, because x increases each time.
Then print on screen how much of the word is made so far, and wait 1 second before the next character is added.
Once the whole array is printed, it stops. Pretty nice effect right?
So, that's arrays! If there are any questions, please leave a reply about it.
TablesWarning: Assure that you understand arrays before attempting tables.
Tables, are much like arrays. So this lesson should be a bit shorter. A table is like an array, but with another variable on it. That is why it is a table. For example, we could have a table of all the peds in Bully. We would need the model, fighting style, fighting style file, name and possibly other information depending on what you are doing. So like an array, to make a table you just make the name equal {}. (tableName = {}). Tables work nearly identical to arrays. Except, we have another variable on each part of the table. Used by a . then the variable name. Example: (Peds[23].Name = "Johnny")
The example has a table named "Peds" and at the 23rd element (Elements are the parts of the arrays/tables) we have the variable Name equal "Johnny". We could have more variables in the 23rd element as well. For example:
Peds[23].Name = "Johnny"
Peds[23].Model = "GRlead_Johnny"
Peds[23].Style = "/Global/G_Striker_A"
Peds[23].StyleFile = "Act/Anim/G_Striker_A.act"
That is all the info we would probably need for Johnny. We could also do that for all the other 257 peds for example of a good use.
You can use the table.insert() function to add multiple variables to a table. Here is an example that is the same as the example above with Johnny.
table.insert(Peds,{Name = "Johnny",Model = "GRlead_Johnny",Style = "/Global/G_Striker_A",StyleFile = "Act/Anim/G_Striker_A.act"})One problem with this function though, the function will add the the first element that doesn't already have a value. So if we used the function, it'd go to the first element, not the 23rd like we wanted. So to get it to the 23rd we'd need to use more table.insert functions before that. If we made a table with all the peds, that wouldn't be an issue anyway. So, going into more detail about the function. Inside the () you have first, the name of the table you are putting it into, a comma, then a {} with all the variables, separated by commas that you want for that element. Then we can use the parts of the table just the same as arrays and setting them. If we wanted to set Johnny's style on player for example, we could do this:
PedSetActionTree(gPlayer,Peds[23].Style,Peds[23].StyleFile)
That concludes the tables tutorial! Hope it helps you, feel free to reply with your questions/comments.