lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

William Roper wrote:

| Theres no reason you have to encapsulate code in a function unless you
| want to call it more than once.

Sure, there are a lot of reasons to just do that!
They all share a common heading: "Good programming style"

Anyway, you could make your functions local, which is a Good Thing (tm).

Example:

~    local function all_enemies()
~      -- return an iterator function
~      -- over all enemies
~    end

~    local function is_enemy_hit(emeny)
~      -- do some geometric calculations
~      -- to test for collision
~    end

~    local function explode_enemy(emeny)
~      -- initialize some timers and gfx library
~      -- to start a explosion animation
~    end

~    function some_event()
~      for enemy in all_enemies() do
~        if is_enemy_hit(enemy) then
~          explode_enemy(enemy)
~        end
~      end
~    end


If you don't like the buttom-up writing, you could easily switch to
top-down writing. Just declare your local functions first:


~    local all_enemies, is_enemy_hit, explode_enemy

~    function some_event()
~       ..
~    end

~    function all_enemies()
~      ..
~    end

~    function is_enemy_hit(enemy)
~      ..
~    end

~    function explode_enemy(enemy)
~      ..
~    end






-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBEfalSIrOxc3jOmoRAlciAJ9ZtbwJWL0S96lQg2TE7w+6P6XuuACgxHPU
x2+M5W17JfVgXr/+j2HQXVc=
=e2AN
-----END PGP SIGNATURE-----

Attachment: signature.asc
Description: OpenPGP digital signature