ScriptUO

Scripting Resources & Utilities => OEUO => OpenEUO Scripting Tutorials => Topic started by: Endless Night on October 14, 2010, 06:02:27 AM

Title: Arrays -- What are Arrays ??? a nobama speacil
Post by: Endless Night on October 14, 2010, 06:02:27 AM
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: [Select]
shoppinglist = {}
shoppinglist[1] = 'milk'
shoppinglist[2] = 'eggs'
shoppinglist[3] = 'bacon'

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

This can also written in lua be done in a simplular way... Note the  {} start and end of an array definition
Code: [Select]
shoppinglist = { 'Milk' , 'eggs', 'bacon'}
print('2nd item: '..shoppinglist[2])


_____________________________________________________________________
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: [Select]
print('3rd element of bacon: '..string.sub(shoppinglist[3], 3,3))

_____________________________________________________________________
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: [Select]
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' }

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' } }

Now tell me the second thing to do on tue and the 1st thing to do on the third day
Code: [Select]
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])


_____________________________________________________________________

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

_____________________________________________________________________
Please leave a comment to let me know if this was usfull or rocket science.
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: Scrripty on October 14, 2010, 08:03:42 AM
I knew what an array was already, but the lua syntax was still a mystery.  Very helpfull. :)
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: NObama on October 14, 2010, 09:41:43 PM
Yay!  A NObama special!

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

Thanks, EN!
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: Endless Night on October 15, 2010, 10:01:27 AM
Yay!  A NObama special!

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

Thanks, EN!

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.
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: 12TimesOver on October 19, 2010, 07:55:04 AM
Another great explanation EN! +rep from me for these write-ups!

X
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: Endless Night on October 19, 2010, 09:07:11 AM
Another great explanation EN! +rep from me for these write-ups!
X

Did i miss it is something is the rep system working again.... and if so how do you +rep something .. or is this just a metaphorical +rep
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: 12TimesOver on October 19, 2010, 09:26:26 AM
<----- Click on the heart underneath the posters name
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: Endless Night on October 19, 2010, 09:34:15 AM
<----- Click on the heart underneath the posters name

that explains why i missed it i dont see any hearts
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: TrailMyx on October 19, 2010, 09:37:03 AM

that explains why i missed it i dont see any hearts

You won't see any hearts under your own posts, else you'll be able to give rep to yourself.  That's been known to cause blindness and an acute case of hairy palms.
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: Endless Night on October 19, 2010, 09:47:19 AM

that explains why i missed it i dont see any hearts

You won't see any hearts under your own posts, else you'll be able to give rep to yourself.  That's been known to cause blindness and an acute case of hairy palms.

I dont see hearts under any posts in any threads...   unless ive already got the blindness your speaking of I know ive got the hairy palms aready lol
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: manwinc on October 19, 2010, 09:49:01 AM
Also, you can make a dynamic table


Code: [Select]
schedule = {
{ "mon", "work", "eat", "hw", "sleep"},
{ "tue", "work", "mow", "run", "Cook"},
{ "Wed", "Pay Bills", "Errands", "Sleep", "Play Video Games"}
}

and then call it with unpack command

Code: [Select]
for i = 1,#schedule
sDay, sFirst, sSecond, sThird, sFourth = unpack(schedule(i))
-- code for checking/doing things here
end
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: TrailMyx on October 19, 2010, 11:20:12 AM
Be careful with the use of "{}", "[]" and "()".  They address identifiers in specific ways.  Since you are referencing a table, you're code should look like this:

Code: [Select]
schedule = {
{ "mon", "work", "eat", "hw", "sleep"},
{ "tue", "work", "mow", "run", "Cook"},
{ "Wed", "Pay Bills", "Errands", "Sleep", "Play Video Games"}
}

for i = 1,#schedule do -- added "do"
sDay, sFirst, sSecond, sThird, sFourth = unpack(schedule[i])  -- changed (i) to [i]
-- code for checking/doing things here
end
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: Endless Night on October 19, 2010, 11:34:57 AM

that explains why i missed it i dont see any hearts

You won't see any hearts under your own posts, else you'll be able to give rep to yourself.  That's been known to cause blindness and an acute case of hairy palms.

I dont see hearts under any posts in any threads...   unless ive already got the blindness your speaking of I know ive got the hairy palms aready lol


My sight has been restored I now see hearts.. thank goodness .. now what to do about these hariy palms umm
Title: Re: Arrays -- What are Arrays ??? a nobama speacil
Post by: manwinc on October 19, 2010, 03:21:50 PM
Ah yes, my bad tm. I see that now in my own script

local sSkill, nMaxLevel, nMinLevel, nSpell, bTarget, nDelay, nMana = unpack(spells)