Pages

Friday, June 22, 2012

Scripting Help #1

Hello, BCGames here. I'm going to be posting daily scripting help. The problem with most people trying to learn to code Lua is not that they can't, but rather they try to speed through it. I have been coding Lua for three (3) years now. And I'm still learning! Today's topic will be about the different types of loops. The most commonly used loop is the while loop. While there isn't a problem with it, the way most people use it, does result in issues. Here is the typical useage:


while true do
   wait() 
   -- code 
end

Now, that's fine and dandy, only that could create massive lag. Here is a better version:

while true do
   wait(2)
   -- code
end

The first example will run about 2,000 time a minute, while the second will only run 30 times a minute.

The next type of loop is called the for do loop. This loop will make it run a certain amount of times. Here is an example:


for i=1,10 do
   wait()
   print(i)
end


This will print the numbers 1-10, then stop. There are many ways to use the for do but those are more advanced and I'll get to that in a later post.

The final loop is the repeat until loop. This will repeat a piece of code until a criteria is met. Here is a simple example:


x=10
y=0
repeat
y = y +1
wait(5)
until y == x



This will continue the loop until the value of y is the same as the value of x.

I hope this helped!

BCGames out! Remember to follow me on twitter! I'm @Zynnar!

No comments:

Post a Comment

Write your comment here... OR Flingi will eat you