ScriptUO
Scripting Resources & Utilities => OEUO => OpenEUO Scripting Tutorials => Topic started 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
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
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.
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
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
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
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...
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.
-
I knew what an array was already, but the lua syntax was still a mystery. Very helpfull. :)
-
Yay! A NObama special!
(Maybe this time, I'll actually understand...)
Thanks, EN!
-
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.
-
Another great explanation EN! +rep from me for these write-ups!
X
-
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
-
<----- Click on the heart underneath the posters name
-
<----- Click on the heart underneath the posters name
that explains why i missed it i dont see any hearts
-
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.
-
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
-
Also, you can make a dynamic table
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
for i = 1,#schedule
sDay, sFirst, sSecond, sThird, sFourth = unpack(schedule(i))
-- code for checking/doing things here
end
-
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:
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
-
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
-
Ah yes, my bad tm. I see that now in my own script
local sSkill, nMaxLevel, nMinLevel, nSpell, bTarget, nDelay, nMana = unpack(spells)