Almighty push all the noobs.
- Jul 28, 2009
- Reaction score
PART V -- For loops...(How do I use them? In which case are they suited the best?)
Here we are to take a first look at loops.
They are maybe confusing at the first time but from time to time you will get used to them.
For loops are most suited for a combination of loop and table, I'll show that in a later part.
For loops are basicly nothing different then a counter, they start at one point and stop on a different.
It is a bit hard to explain that (atleast for me) that is why I am going for an example and comment it, I think that is a way better explained then writing 200 lines about it.
function onUse(cid, item, fromPosition, itemEx, toPosition) for var = 1,10 do -- "var" stands for the variable of the two numbers (you can change the variable to others aswell), "1,10" means it's going to loop from 1 to 10 (10 loops in this case) print(var) -- will print "1 2 3 4 5 6 7 8 9 10" end return true end
each for loop has to have it's own variable, you can't do it like this:
for var = -1,-10 and var = 1,10 do
function onUse(cid, item, fromPosition, itemEx, toPosition) for a = -1,-10 and b = 1,10 do -- numbers can either be negative or positive, that doesn't matter, you can also loop from negative to positive and vice versa. print(a ..', ' .. b ..'' || ') -- will print "-1, 1 || -2, 2 ||......" end return true end
As you can see a for loop aint so hard to setup and use.
Let's go for another example to see how it works in a server based script.
Let's say we want to add the player a backpack and that backpack contains 3 times the same item.
function onUse(cid, item, fromPosition, itemEx, toPosition) local backpack = doPlayerAddItem(cid, 1988, 1) for a = 1,3 do doAddContainerItem(backpack, 2160, 100) end return true end
There's not really much more to say about the for loops.
We'll get back to them later on.
You could try using a "paste" site or making it a pdf, I would love to read it since I'm getting into ots back and there's a lot to learnI would love to re write my entire guide, the problem however is, beeing limited to 10k characters each post is quite a hindrance.
I cannot even edit 2 of the posts because they give me the "exceeding post length error"
For 0.3.6: https://github.com/peonso/forgottenserver036pl1/blob/master/src/luascript.cppI still use the function list in post #2, because it contains like 95% of the functions I use, and saves me the effort of looking through my source files for the simple lua functions. haha
I hope they aren't over-written with newer 1.2 stuff.
Welcome to my Scripting Guide!
I hope you'll enjoy your stay and learn much from it
This guide is completly about lua related scripting, you'll learn here everything from basics up to advanced stuff.
If you think there's something missing then tell me what and it'll be added.
Please don't leave without commenting!! thanks
This Thread is based on my old Tutorial
- Part I - Functions...(What are they? What are they for?)
[*]Part II - - If...Else...Elseif...Return...End...(In which Order do I use them?)
[*]Part III - - is greater as(>)....is smaller as(<)...is same as(==)...(How to use them correct?)
[*]Part IV - - Local and Global Statements...(What are they for? When shall I use them?)
[*]Part V - - For loops...(How do I use them? In which case are they suited the best?)
[*]Part VI - While loops...(How do I use them? In which case are they suited the best?)
[*]Part VII - Repeat loops...(How do I use them? In which case are they suited the best?)
[*]Part VIII - Simple Tables (coming soon)
[*]Part IX - Advanced Tables (coming soon)
[*]Part X - Tables + Loops (coming soon)
I suggest that you go step by step, so nothing will stay unknown to you.