I'm late to the party, but oh well.
Command items:
#3 See also [[the other string syntax]]. And [===[ [[]]] '"\ ]]===] and so on.
--[[ and the
multiline
comments]]
Strictly speaking, if you're separating between command and general lua, this belongs in the other section.
#4, #5
Should these be separate?
#6
Is it worth mentioning that the command print function is non-conformant? It should be print ('Event 1: ', unit) but that just gets you a nasty error message.
#7
I believe a better approach will be
Code: Select all
function getAiUnit(side_in, unit_in)
local result = ScenEdit_GetUnit({side=side_in, name=unit_in})
if result == nil or ScenEdit_GetSideIsHuman('NATO') then -- not player controlled
result = false;
end
return result
end
You should then omit your ==true and it will work the same, except this one allows you to store the return value and use it without doing another unit lookup inside the then block. Some functions seem to want a modified version of that return table.
Another benefit of this is that you can build up a library of common functions that you can use in other scenarios just by having it loaded at scenario start.
Before making that recommendation/suggestion, please try actually doing it, and tell us how.
- Trigger on scenario loaded reliably does not fire when loaded in editor.
- When you save+load, all variables, including all your functions, are wiped.
Presuming a 'scenario loaded' trigger in a repeatable event loads every time the user loads outside of the editor, a special action for editor mode may solve the problem. Just... remember to update the event action too.
#8
This is all opinion:
Ideally, the fact that you are using lua is an implementation detail. The action name should describe what the action does, and what it does under the covers - lua or not - should not matter.
With that said, given the bug-prone nature of flexible scripting, marking them with lua (aka "bugfixing attention here") isn't a bad idea, but I'm keeping that at the end of the name.
#9 has me asking what audience you are writing this for. Perhaps setting the expectations in a preface of sorts?
Lua general:
#1
--[[You say to use comments, and through all your examples use comments in a way that is generally considered worse than not having any comments.
When I say "generally" I mean "among programmers". The assumption is that you know, or will learn, how to read code. The code should say what, and the comments should say why. When comments say what, changing it will require changing two things. So when (not if) that does not happen,
for i=1,24 do -- scan thru numbers 1 to 16
I hope you don't rely on the comments to know what's going on there!
Again, who are your audience? If they are beginners, and I believe most are, they may look at the comments shown by the guy who just told them to use comments, and think it's good.
If your guide needs an emphasis on what, it may be better to include separate pseudocode. Or "just" write it more readably.
]]
#2 It gets worse - even if you store a variable globally, you cannot rely on it being there after your script exits. If the user saved and loaded in between, it's gone. So when you actually want to do that, ScenEdit_SetKeyValue and ScenEdit_GetKeyValue appears to be required.
It is possible to make creating (writing) or reading a global variable an error, letting you spot these cases. Misspelled names included. Or make these automatically call setkeyvalue and getkeyvalue when used.
It'll be a piece of code that needs to be run in the lua console before code it will be checking for.
#3
When the user doesn't have control over his display (e.g. a text file displayed in a web browser...) tabs may expand to 8 spaces, which can make it look very messy. I'm generally a fan of tabs for indentation precisely because it is customizable, but for this particular purpose I would recommend you use two spaces.
#4, in its first example, does not follow #3.
Also, to follow up my criticism in #1
Code: Select all
-- all aircraft of the squadron
for i=1,16 do
local unit = {Side='Germany', Name="Steinhoff #"..i,}
-- must be alive
if ScenEdit_GetUnit(unit) then
ScenEdit_SetUnitSide(unit, newside='NATO'})
-- Human side doesn't have this mission
if not ScenEdit_GetSideIsHuman('NATO') then
ScenEdit_AssignUnitToMission("Steinhoff #"..i, "NATO CAP")
end
end
end
And the mandatory nitpick: Lua is a word (translates moon) and supposed to be capitalized as such. Lua or lua. LUA implies an acronym.