Scripting Resources & Utilities > OpenEUO Scripting Tutorials

Arrays -- What are Arrays ??? a nobama speacil

(1/3) > >>

Endless Night:
Arrays arrays array.. in order to use LUA your going to have to understand arrays.  The good news is everyone already knows what an array is they just dont know it.
Imagine a shopping list of things to buy such as
milk
eggs
bacon

Believe it or not but thats a bloody array. Its an array with 3 items (or Elements). Lets call our list/array ShoppingList... now whats the 2nd element/item  eggs .. or shoppinglist[2]

What that carnt be an array .. thats 2 dam easy. Lets turn that into lua  first we create our shopping list emtpy then add elements, then lets print the second item

--- Code: ---shoppinglist = {}
shoppinglist[1] = 'milk'
shoppinglist[2] = 'eggs'
shoppinglist[3] = 'bacon'

print('2nd item: '..shoppinglist[2])
--- End code ---

This can also written in lua be done in a simplular way... Note the  {} start and end of an array definition

--- Code: ---shoppinglist = { 'Milk' , 'eggs', 'bacon'}
print('2nd item: '..shoppinglist[2])
--- End code ---


_____________________________________________________________________
Now lets all be clever clogs... the word Bacon is an array ???!!!!!  You what you say... well it is .. tell me the 3rd letter of the word bacon....  3rd umm letter or did you mean element... well its c either way.  In lua to get an element of a string (array of characters) you have to use a speacil command.

--- Code: ---print('3rd element of bacon: '..string.sub(shoppinglist[3], 3,3))
--- End code ---


_____________________________________________________________________
Ok interesting... but what is a Table.. a table is an array of arrays... ow jimmy now where going crazy.. but wait you already know what an array of arrays is... its your dam schedule...

Monday
   mow lawn
   take out garbage
tue
  pay the bills ow lord
  start on fixing the roof
wed
  drink beer

In Lua

--- Code: ---MySchedule = {}    -- make an empty table/array
MySchedule[1] = { 'Monday' , 'mow lawn', 'take out garbage' }
MySchedule[2] = { 'tue' ,'pay the bills ow lord',' start on fixing the roof' }
MySchedule[3] = { 'wed','drink beer' }
--- End code ---

You could also represent this as

--- Quote ---MySchedule = { { 'Monday' , 'mow lawn', 'take out garbage' } , { 'tue' ,'pay the bills ow lord',' start on fixing the roof' } , { 'wed','drink beer' } }
--- End quote ---

Now tell me the second thing to do on tue and the 1st thing to do on the third day

--- Code: ---print('Whats the 2nd item to do on TUe: '..MySchedule[2][3]) -- note 2nd thing is 3rd element in array
print('Whats the 1st item to do on the third day: '..MySchedule[3][2])

--- End code ---


_____________________________________________________________________

Now thats arrays/list and tables  (arrays or arrays/lists of lists)  Simplified maybe but the gist of it..
Now to apply to some serious code....

scanitems(visible)   makes a array of Variables of all items found
each element of the array .. contains an list of varialbes that apply to that item found (id id,name, position)
to get each element of the talbe we use UO.getitem(INDEX/element number) and it returns all the values...


--- Code: ---itemsfoundcount = UO.ScanItems(true) -- visible
local nID,nType,nKind,nContID,nX,nY,nZ,nStack,nRep,nCol = UO.GetItem(5)
print('The fifth item found ID is: ..nID)
--- End code ---
 

_____________________________________________________________________
Please leave a comment to let me know if this was usfull or rocket science.

Scrripty:
I knew what an array was already, but the lua syntax was still a mystery.  Very helpfull. :)

NObama:
Yay!  A NObama special!

(Maybe this time, I'll actually understand...)

Thanks, EN!

Endless Night:

--- Quote from: NObama on October 14, 2010, 09:41:43 PM ---Yay!  A NObama special!

(Maybe this time, I'll actually understand...)

Thanks, EN!

--- End quote ---

LOL i wrote it for you as somewhere in some post you were bashings arrays and your inablity to be compatible with them logically. But arrays are essential for more complex lua stuff on just nice lua stuff.. just basic arrays.

12TimesOver:
Another great explanation EN! +rep from me for these write-ups!

X

Navigation

[0] Message Index

[#] Next page

Go to full version