Data Realms Fan Forums
http://45.55.195.193/

A LITTLE HEADS UP
http://45.55.195.193/viewtopic.php?f=73&t=14737
Page 1 of 1

Author:  Grif [ Mon May 18, 2009 12:07 am ]
Post subject:  A LITTLE HEADS UP

On syntax.

Syntax is the most important thing in any programming language, scripting language, any logical language ever.

Everything stays mostly the same across languages, the ONLY things that change are syntax and functions. SO! Let's get a little intro to syntax.

Lua is EXTREMELY SIMPLE. Fantastically simple! It's a wonderful language to learn.

Let's start with the first thing any intro to logic should start with. An IF statement.

Code:
if x == y then


Bam. That's it. That's it. That simple. BUT! You're not quite done yet.

Code:
if x == y then
   y = y + 1
end


Now, this is just a little bit harder. If x is the same as y, increase y by 1. In Lua, you can refer to a variable inside its own definition. Now, notice the equals. In the if line, there's two of them. That means that it CHECKS if x is the same as y; it's a "traditional" equals. So what does a single equals sign do? It SETS something. y = y + 1 means that Y is set to itself, but one higher. Continually increasing.

AND THE MOST IMPORTANT THING: end. You absolutely have to make sure to end any logical statement. Otherwise, everything after the then will be DONE, rather than checked or whatever you want to have happen.

And now for the SECOND most important thing: nesting.

What if I wanted to have something checked, and then if that's true, having something else constantly done?

Code:
if x ~= y then
   while y < x then
      y = y + 1
   end
end


See how the while goes INSIDE the if? That's absolutely one of the most important things programmers need to learn to do, and even if Lua is a fairly limited scripting language, it's good to do it properly. It helps legibility (for both you and anyone else looking at your code), it helps debugging, and it just looks better.

This applies to ALL logical statements. If, while, for, and also for things like functions.

ENDS LESSON 1

Author:  Daman [ Mon May 18, 2009 12:18 am ]
Post subject:  Re: A LITTLE HEADS UP

you should post this in your on lua or whatever sticky

Author:  Miles_T3hR4t [ Mon May 18, 2009 4:52 am ]
Post subject:  Re: A LITTLE HEADS UP

thats very helpful, I am serious. I didn't even notice 'while' even existed. I don't know how I missed that.

Author:  Geti [ Mon May 18, 2009 5:03 am ]
Post subject:  Re: A LITTLE HEADS UP

im fairly sure its part of just about all readable languages. O_o
anyway, thanks for taking the time to post this grif. hopefully it'll make DRL's code more legible in general, and help ease people into Lua.

Author:  Miles_T3hR4t [ Mon May 18, 2009 5:15 am ]
Post subject:  Re: A LITTLE HEADS UP

Geti wrote:
im fairly sure its part of just about all readable languages. O_o
anyway, thanks for taking the time to post this grif. hopefully it'll make DRL's code more legible in general, and help ease people into Lua.


some of grif's posts are doing a good job of helping that along, I suck at lua. Grif you should totally try to do a tutorial, you know, reserve all the posts for the front page and fill them in as you go. sort of put this all in one place.

Author:  Grif [ Mon May 18, 2009 5:52 am ]
Post subject:  Re: A LITTLE HEADS UP

Naw I'll just get my mod buddies to remove all y'alls posts once I get motivated to do further parts.

Suggestions of things to cover?

Author:  Geti [ Mon May 18, 2009 6:37 am ]
Post subject:  Re: A LITTLE HEADS UP

for loops, how to reduce lag by using timers to limit how often functions are used, how to define functions, tables (and how to iterate them), using math.random and math.floor to get usable numbers, vectors (surprised at how many people dont get how to use them..), areas, how to give relative positions, how to use trig to find distances rather than just using "boxed" distance calculation, common syntax errors and how to fix them, how to use MovableMan:AddActor, MovableMan:AddParticle, etc, how to add stuff to peoples inventory, fiddle with health, whatever else, commenting and why it is important (as well as how it is useful in debugging).
all those would help get a lot of people into Lua faster.
oh, also, yell about Lua being Lua and not lua or LUA or whatever else.

Author:  Mind [ Mon May 18, 2009 3:04 pm ]
Post subject:  Re: A LITTLE HEADS UP

Maybe make a simple lua mod from scratch to show the basic method of it all.

Author:  Miles_T3hR4t [ Mon May 18, 2009 5:24 pm ]
Post subject:  Re: A LITTLE HEADS UP

The Mind wrote:
Maybe make a simple lua mod from scratch to show the basic method of it all.


This, step by step, to show the different times you can do things.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/