• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Scripting Guide

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.

example:
Code:
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:
Code:
for var = -1,-10 and var = 1,10 do
correct version:
Code:
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.
Code:
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.


This Part Is Too Hard Dude
 
I 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"
 
I 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"
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 learn
 
Got things solved out, mark enhanced text length to 25k, expect this guide to be re written once I have it completly sorted out
Great to hear you will be doin a re-work! Keep the good work up :) Heroland forever in my dreams btw^^
 
Still remember when I read this for the first time, my introduction to this world. Awesome guide.
 
I 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 :oops:
I hope they aren't over-written with newer 1.2 stuff. :eek: :rolleyes:
 
Welcome to my Scripting Guide!

I hope you'll enjoy your stay and learn much from it :D

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 :rolleyes:

This Thread is based on my old Tutorial

Thread History

I suggest that you go step by step, so nothing will stay unknown to you.

thanks by this tutorial men,it will be finished ?
 
Thanks.
After reading this I managed to solve a script. Maybe one day you'll continue with this tutorial.
 
Back
Top