LUA ComonPitfalls: Difference between revisions

From Spring
Jump to navigationJump to search
m Created page with "This is not a tutorial page. This is the sort of last ressort page, you will visit when you are knee deep into the swamp. Just bookmark it and forget it if you search for a tutor..."
 
No edit summary
Line 44: Line 44:




7.  
7. Use Includes:
Parts of your unitscript are used allover again? In diffrent units? Have sort of a function toolkit, you want to reuse, without copy and pasting it?


include "MyToolkit.lua"
will add all the functions of the MyToolkit.lua to the program(only used ones). Good way to seperate overcomplex programs into easier understandbalbe pieces.


8.More to come.. thats for certain.
8. Locals:
You can write local 60 times into every unitScript before Variables and/or functions. What does this mean? It means you have sixt superfast slots. This stuff will be kept in memory, never ever take by the Lua-Virtual Maschine and pushed back to the harddrive(super-slow in computer time, a timescale in which a human is sort of a glacier). Handle with care. Use it on functions (but what good is a localized function that has to wait for not localized variables.. so concentrate it on timecritical stuff.
 
9. Check your cases:
If you compare stuff, there is always the possibility of a rare case, you missed to think off. Plan this failure in. Add a Error Message "Exotic Tank does fly-Error" as the case to go in at last. 
 
 
More to come.. thats for certain.

Revision as of 20:28, 13 March 2012

This is not a tutorial page. This is the sort of last ressort page, you will visit when you are knee deep into the swamp. Just bookmark it and forget it if you search for a tutorial.


I will also happily answer your questions in moddev or lua irc-channel. Keep your infolog ready when you approach, and a www.pastebin.com of the code in your pocket. Now on to the pitfalls, were many brave programers have come to death.

1. Silent new Declaration: Lua doesent recognize your typo.. instead it takes it for a new declared Variable and fills it with nil, and then it marches on without mercy.

How to avoid: Use Spring.Echo("MyVariableName:", variableName) to print out the Variables value. Use assert(variableName) to check for nils and false booleans. Do not use equals sounding Names. Do not allow typos, even in the coments. (Your autocompletion will pick them up and suggest them to you the next time you type - and they are off).

2. Unknown Function: Unlike Java, Lua expects you to have functions declared ahead (or atleast functionheads to be decleared ahead) otherwise it wont know what to do. This results in a Error-Message, and you will learn to love those, because the real mean errors come like thiefs in the night and murder parts of your programs in silence.

3. Necessary Sleeps: You need a Sleep(TimeIn1000/s) in every loop. These Sleeps are the "extraction" points, were the engine stops simulating your "thread" (which is a scheduled function, so no need to worry for threadsafety) and takes up another thread. So functions called in the UnitScript also need a Sleep in it, for them to work properly.

4. Overcomplex design: If its to big for you to understand (who built it), break it down. Do not copy and paste code. Doing that is wrong, wrong - and wrong. Get code out into special functions only dedicated to checking.

if (boolOne == true and x > 3.14) or (foo==bar) then can be converted into

function checkThem(boolOne,x,foo,bar) if (boolOne == true and x > 3.14) or (foo==bar) then return true else return false end end

Delete unecessary stuff.Do not keep code in your work that has reached its exparation date. Plan ahead, it might sound stupid to draw a sketch of a program, but you will come into sitatuion were you coded yourself into a corner, and 2 hours go to waste. So planning if you have a complex task is never wasted time.

5. Use available tools: You can code Lua Animations by hand. And to learn that, you do good to do that. But if you want to make a whole show, this approach is to slow. You need tools, so get yourself the toolbox and use springposer. It might take some time to setup, but you still will win time in the long run.

6. Step back: Some Ideas are to good to waste, and some will eat your hours for breakfast.. while resulting in mediocre stuff, that could have been done in half a hour (or worser, has been done by someone else). So if you want to implement a idea, look for some forerunners work, get your hands on it, modfiy it, melt it, bend it and mend it, until you got were you wanted to be.


7. Use Includes: Parts of your unitscript are used allover again? In diffrent units? Have sort of a function toolkit, you want to reuse, without copy and pasting it?

include "MyToolkit.lua" will add all the functions of the MyToolkit.lua to the program(only used ones). Good way to seperate overcomplex programs into easier understandbalbe pieces.

8. Locals: You can write local 60 times into every unitScript before Variables and/or functions. What does this mean? It means you have sixt superfast slots. This stuff will be kept in memory, never ever take by the Lua-Virtual Maschine and pushed back to the harddrive(super-slow in computer time, a timescale in which a human is sort of a glacier). Handle with care. Use it on functions (but what good is a localized function that has to wait for not localized variables.. so concentrate it on timecritical stuff.

9. Check your cases: If you compare stuff, there is always the possibility of a rare case, you missed to think off. Plan this failure in. Add a Error Message "Exotic Tank does fly-Error" as the case to go in at last.


More to come.. thats for certain.