These are my old tutorials, that has gone through several forums and now it's time I put it here at OtLand!
I released those tutorials in the old OpenLua forum, and before it closed I saved them to my computer, too bad I didn't save the BBCode, I only saved the HTML files!
Contents:
SEE ATTACHMENT BELOW FOR DOWNLOAD!
Hope you enjoy them
Edit: For the Functions and return values tutorial, I must add this:
When a return value has been passed, the function is terminated, just like a loop is terminated by the break statement.
So, this means:
What the function itself does, doesn't really matter, but I'll explain that anyways; If x has the same value as y, then it will return the current values, but if it isn't (even though we have no else, we know that) then it will return the mininum value and the maximum value.
I think I haven't told you about multiple return values (without using arrays) either, so I'll do that now too:
When you return values like x, y it means that you'll have to pass it to another function. Here is an example of this:
First, in the function randomNumbers, it will check if both arguments are numbers. If they are, then it will create 2 random numbers and return both of them.
In the function sortBySize, it first checks if both are the same, if they are, it will return them both just like that, but if they aren't it will firt check which of them is smallest and then biggest and return both of them.
At last you see it will combine both functions in the print function, it doesn't have a second parameter, because function randomNumber returns 2 values! Which means, it does get a second parameter!
So, if the function randomNumbers created these numbers: 67 and 45, the print function would output:
Now, the last example (I suppose):
First of all, the "...", means it could have unlimited arguments, and you don't know how many, because there is no limit! The "..." will only return the first argument given, but as you see, I do loop "arg" and not "...", that's because the variable "arg" will automatically become a table of all the values of "..."
So just think of it as I loop a normal table, not harder than that.
The function tonumber will return the numbers if they are numbers and false if they are not numbers:
Do you understand? It will check if the variable only contain numbers, if it does then return the number... So "234234" as string would return "234234" as number... Or rather "234234" returns 234234 (note that there's no quotes ")...
Read tables tutorial (it's in this tutorial package) if you don't understand table.insert and unpack (unpack will be explained further here). But why I used the 2nd and 3rd arguments, is because the unpack was returning the size of the table >.> So I hade to make it not by removing the last value
To use this function combined with math.max bug free, I'd do it this way:
As I said before, if you don't understand this part: "(keepNumbers(t) and math.max(keepNumbers(t))) or 0", read this tutorial: CLICK HERE!
Edit: For the Tables tutorials I must also add this:
The function unpack will unpack the content in a table lol But somethings you might want to know about it:
This will print absolutely nothing, because unpack doesn't accept index that aren't numbers!
Another thing about that one:
Guess what this will print?
Sincerely,
Colandus
I released those tutorials in the old OpenLua forum, and before it closed I saved them to my computer, too bad I didn't save the BBCode, I only saved the HTML files!
Contents:
- [Beginner] Tables
- [Beginner-Intermediate] Functions and return values
- [Intermediate] Loops and some tricks
- Names of Spells
SEE ATTACHMENT BELOW FOR DOWNLOAD!
Hope you enjoy them
Edit: For the Functions and return values tutorial, I must add this:
When a return value has been passed, the function is terminated, just like a loop is terminated by the break statement.
So, this means:
Lua:
function doSomething(x, y)
if (x == y)
return x, y
end
-- We don't really need else here, since it will not be read if x == y,
-- because it has already returned a value and exited the function.
local small = math.min(x, y)
local big = math.max(x, y)
return small, big
end
I think I haven't told you about multiple return values (without using arrays) either, so I'll do that now too:
When you return values like x, y it means that you'll have to pass it to another function. Here is an example of this:
Lua:
function randomNumbers(min, max)
if (tonumber(min..max) == nil) then
return false
end
local n1 = math.random(min, max)
local n2 = math.random(min, max)
return n1, n2
end
function sortBySize(n1, n2)
if (n1 == n2)
return n1, n2
end
local small = math.min(n1, n2)
local big = math.max(n1, n2)
return small, big
end
print(sortBySize(randomNumbers(1, 100)))
In the function sortBySize, it first checks if both are the same, if they are, it will return them both just like that, but if they aren't it will firt check which of them is smallest and then biggest and return both of them.
At last you see it will combine both functions in the print function, it doesn't have a second parameter, because function randomNumber returns 2 values! Which means, it does get a second parameter!
So, if the function randomNumbers created these numbers: 67 and 45, the print function would output:
Lua:
45 67
Now, the last example (I suppose):
Lua:
function keepNumbers(...)
local numbers = {}
for k, v in pairs(arg) do
if tonumber(v) then
table.insert(numbers, tonumber(v))
end
end
return unpack(numbers, 1, #numbers-1)
end
r = math.max(keepNumbers(x, y, z))
So just think of it as I loop a normal table, not harder than that.
The function tonumber will return the numbers if they are numbers and false if they are not numbers:
Lua:
str = "234234" -- this will work in tonumber
str = 214324 -- will work too
str = "asdasd" -- will not work
str = "asd77878" -- will not work either
Read tables tutorial (it's in this tutorial package) if you don't understand table.insert and unpack (unpack will be explained further here). But why I used the 2nd and 3rd arguments, is because the unpack was returning the size of the table >.> So I hade to make it not by removing the last value
To use this function combined with math.max bug free, I'd do it this way:
Lua:
local x, y, z = "566", nil, 133
local t = {x, y, z} -- t = {x=x, y=y, z=z}, works aswell!
local result = (keepNumbers(t) and math.max(keepNumbers(t))) or 0
print(result) -- Prints 566!
Edit: For the Tables tutorials I must also add this:
The function unpack will unpack the content in a table lol But somethings you might want to know about it:
Lua:
t = {x="hihiho", ["asdsd"] = "lolz"}
print(unpack(t))
Another thing about that one:
Lua:
t = {"Hello", "Colandus", "is", "best", "not"}
unpack(t, 2, 4)
Why? Well, it's because 2 is the start value and 4 is the end value. That means it will only return the second to the fourth value of the table.Print said:Colandus is best
Sincerely,
Colandus
Attachments
-
lua_00011.rar634.1 KB · Views: 57 · VirusTotal
Last edited: