Where to start…
- Coding methods.
Every programmer has different ways for doing things. In my first Corona Game, I used the enterFrame event and checked all of my objects to see if they were manually running into other objects (laser blasts vs. enemy ships). Others would have made physics objects out of them, even though physics wasn’t really needed for the game to take advantage of Event based collision detection.
Some people prefer to keep a player’s data separate from the player’s graphic on the screen, others combine them into one object. As they say, different strokes for different folks.
I’m glad you found code that was easier to understand, but that doesn’t mean the code you had trouble with isn’t as valuable. Perhaps its using different but valid techniques that you may need in the future once you learn more.
- Math and Trig.
Programming depends a lot on algebra, after all its a lot about writing formulas:
ship.x = display.contentWidth / 2
is simple algebra.
But when you get to a point that you want to rotate that ship (say for an asteroids game) well now your using degrees and you start getting into circle formulas. If you have an enemy on the screen and you need to know how far to rotate your player object so that its facing/pointing to the enemy, then you’re going to need things like right-angle triangle math, which can involve Sines’, Cosines and Arctangents to calculate your rotation.
Luckily many of these formulas are so common place in what we do, that a search in the forums can frequently find the exact answer your looking for, or a quick hit on google can refresh you. If you want to build a TableView app of all your store’s inventory, no you won’t need Trig. Wanna build games, expect some simple to some complex depending on what you’re trying to do.
- Why do programmers list all their variables and stuff 1st without having them = to something?
Well there are a couple of different reasons. One, if you are coming to Lua from a language like C, you have to declare all your variables at the top of the program and at the top of functions. So for some of us, its habit. Secondly, it makes the code more readable. If you have your variable declarations in the middle of your code, they can become hard to find or you might try to use a variable name that you’ve already used which you would have spotted had they all been declared in close proximity to each other.
However, others would argue, in particular coming from languages like C++ and JavaScript, that declaring variable when you actually use them makes more sense and easier to read.
In my case, I’m the old school C guy so I like mine together at the top, but I almost always initialize them, like:
local x = 0
In Lua however there is a very practical reason to put them at the top, and its called “Forward Declaration”. Basically if you have something like this:
local function a()
b()
end
local function b()
do other stuff
end
It will error on you because “b” has not been declared beforehand, so a has no clue what to do. So you end up using a different syntax for functions:
local b
local function a()
b()
end
b = function()
do other stuff
end
And now a knows that b exists. This doesn’t only impact functions but variables too.
local function a()
print(c)
end
local c = 12
Function a doesn’t have a clue what “c” is and it blows up on you. Hmmm, had “c” been declared at the top???
- I guess once you move from “newbie” to “otherThanNewbie” the bulk of your code doesn’t go into the main.lua anymore, this is to save memory correct?
It’s not to save memory. In fact, it probably uses a little more memory. The purpose is to save your sanity. Its a real pain in the rear to try to edit a single main.lua that has 5,000 lines of code in it. You want to break out what you can to keep your code in manageable chunks. And if you write your “modules” with common functionality grouped together, you start building a library of code that you can use on your next project.
- p.s. Semicolons
One of the rationals behind Lua is to cater to non-programmers. The more punctuation a language uses the harder it is for a novice to understand. Take for example:
BASIC:
IF a = 10 THEN
b = 20
ELSE
b = 30
END
Lua:
if a == 10 then
b = 20
else
b = 30
end -- notice BASIC and Lua are very similar...
Compare to C
if (a == 10) {
b = 20;
} else {
b = 30;
}
All three of those are identical code, but for the non-programmer, trying to read that block of C is scary.
What does this have to do with semi-colons? Well almost every civilized language uses semi-colons to separate statements so you can do more than one thing on a line.
C:
if (a == 10) { b = 20; c = 30; d = 40; }
Lua
if a == 10 then b = 20 c = 30 d = 40 end
From a lexical parser’s perspective, the second example is harder. Where does one statement end and the next begin? By putting in semi-colons, it’s easier to parse the data:
if a == 10 then b = 20; c = 30; d = 40; end
Having them to terminate lines just simply makes sense when you’re trying to parse the code.
Lua needs them in the example above, but does not required them if a line of code is on a line by itself (or before the closing “end”). It lets them be optional on any line of code.
That’s the practical side of it, but why do so many programmers do it?
Well we program in Objective C, JavaScript or PHP or a host of other languages besides Lua and in those languages the semi-colons are required. It’s a comfort spot for us to be able to maintain some commonalty with our other languages we work with.
Another example you might run into and ask yourself why is this:
C:
if ((somecondition \< 0) || (somecondition \> 100)) {
do stuff
}
Lua:
if somecondition \< 0 or somecondition \> 100 then
do stuff
end
First || in C is the same as the word “or” in Lua. But why all the parens? Well C requires the outer set. You can think of a the minimum if statement as:
if (condition) { code; }
The inner parens are to make sure things happen in the right order. In the above example I could have left the inner parens off because the “Or ||” has a higher presidence than the binary operators (less than, greater than, equals, not equals). There are many times where your conditions are complex enough, grouping using parens helps make sure the “algebra” is right in those conditions.
Hope this helps.
[import]uid: 19626 topic_id: 17173 reply_id: 64680[/import]