News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: (LUA) Arrays & Tables  (Read 5678 times)

0 Members and 1 Guest are viewing this topic.

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
(LUA) Arrays & Tables
« on: December 07, 2013, 12:33:55 PM »
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.

Arrays
An array is a type of variable, with multiple values. Here is an example:

Code: [Select]
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:

Code: [Select]
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.

Code: [Select]
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.



Tables
Warning: 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.
« Last Edit: June 06, 2015, 08:16:06 PM by DaBOSS54320 »

Offline MadmaN

  • Bully-Board Admin Team
  • Newbie
  • *
  • Posts: 0
  • Gender: Male
  • Biblio-Techa Mods (retired)
    • View Profile
Re: (LUA) Arrays & Tables
« Reply #1 on: December 08, 2013, 03:50:33 AM »
Very nicely written.

I am glad you decided to do a detailed tut on arrays and tables since I never got around to doing this myself and as such this will save a lot of people time trying to figure out new ways to add to their lua mods or ways to improve upon them.

I went ahead and made this a sticky for you as this is a keeper.

Offline c00ld0c26

  • The D0c
  • Moderator
  • Can't Get Enough
  • *****
  • Posts: 5,137
  • Gender: Male
  • Just a dood doing dood things.
    • View Profile
    • My channel.
Re: (LUA) Arrays & Tables
« Reply #2 on: December 08, 2013, 07:08:44 AM »
Great... Just great...
I sit and wait for permission to make a tutorial and you just go ahead and do it.
Ah well... Atlist the community finally got to learn tables :P

Offline MadmaN

  • Bully-Board Admin Team
  • Newbie
  • *
  • Posts: 0
  • Gender: Male
  • Biblio-Techa Mods (retired)
    • View Profile
Re: (LUA) Arrays & Tables
« Reply #3 on: December 08, 2013, 02:55:54 PM »
Great... Just great...
I sit and wait for permission to make a tutorial and you just go ahead and do it.
Ah well... Atlist the community finally got to learn tables :p

Don't need permission to do tutorials like this m8. There are only two I want to keep from being made tutorials for the time being and that is player model switching and timecycle freeroam scripting due to another method near ready to be released for tutorial making.

Offline c00ld0c26

  • The D0c
  • Moderator
  • Can't Get Enough
  • *****
  • Posts: 5,137
  • Gender: Male
  • Just a dood doing dood things.
    • View Profile
    • My channel.
Re: (LUA) Arrays & Tables
« Reply #4 on: December 09, 2013, 03:39:03 AM »
Great... Just great...
I sit and wait for permission to make a tutorial and you just go ahead and do it.
Ah well... Atlist the community finally got to learn tables :p

Don't need permission to do tutorials like this m8. There are only two I want to keep from being made tutorials for the time being and that is player model switching and timecycle freeroam scripting due to another method near ready to be released for tutorial making.

Well to avoid getting into trouble I have asked Red if I can release Tables to the community (due to the high competition and trouble releasing LUA did but on the other hand Hugh progress in modding).
Red said he prefers to finish anylaizing all the stuff the modding team got from you lately and then think which direction will go next.
Even tho don't get me wrong :

I Don't care who did it first second last and all that, it means nearly nothing to me.
The thing that bothered me is that he didn't ask for permission while I did just in case u or Red decide that the community is not ready or something.
On the other hand tho :
I'm glad the community learned tables and I will do all I can to expand the knowledge they have got from it.
I will update my LUA tut soon to teach how to use tables and to create menu's with them for the ones who don't know.

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: (LUA) Arrays & Tables
« Reply #5 on: December 09, 2013, 07:10:01 AM »
I'm currently trying to make a better way to make menus with LUA too. So the method you are using may soon become outdated. Don't know for sure though, it's not proving very successful atm. If I do get it to work though, will be amazing.

Offline c00ld0c26

  • The D0c
  • Moderator
  • Can't Get Enough
  • *****
  • Posts: 5,137
  • Gender: Male
  • Just a dood doing dood things.
    • View Profile
    • My channel.
Re: (LUA) Arrays & Tables
« Reply #6 on: December 09, 2013, 12:07:22 PM »
Hehe, good job on finding new ways to make menu's :P
Hopefully it'll work.

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: (LUA) Arrays & Tables
« Reply #7 on: December 09, 2013, 05:05:55 PM »
The idea, is right in the table you can list the name of the function. Then you'd call the function by just writing table[index].Function where Function would be the name of the function. haven't got it to work yet though :/