scar_coding
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
scar_coding [2024/01/21 14:28] – created rap6tor | scar_coding [2025/01/10 11:27] (current) – old revision restored (2024/01/21 14:49) admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== SCAR Coding ===== | ===== SCAR Coding ===== | ||
- | **The following Guide was written by modder KanKrusha on RelicNews Forums (now defunct), however it is one of the more comprehensive guides currently available. Modern notes are highlighted.** | + | **The following Guide was written by modder KanKrusha on RelicNews Forums (now defunct), however it is one of the more comprehensive guides currently available.** |
+ | ---- | ||
**[HOW TO] Common Lua and SCAR errors: Identification and Prevention** | **[HOW TO] Common Lua and SCAR errors: Identification and Prevention** | ||
Line 22: | Line 22: | ||
Lua is very sensitive to typos and errors. I check all my script by pasting it into a program called ScIte. It is a bit fussy to use but will identify syntax problems before I try the code out in game | Lua is very sensitive to typos and errors. I check all my script by pasting it into a program called ScIte. It is a bit fussy to use but will identify syntax problems before I try the code out in game | ||
- | https:// | + | https:// |
**COMMON LUA ERRORS** | **COMMON LUA ERRORS** | ||
Line 37: | Line 37: | ||
Code: | Code: | ||
+ | < | ||
arms = 1000 -- qqq temporary test value to see if function working | arms = 1000 -- qqq temporary test value to see if function working | ||
+ | </ | ||
You can also disable chunks of code by commenting it out with square brackets | You can also disable chunks of code by commenting it out with square brackets | ||
Code: | Code: | ||
+ | < | ||
--[[ putting 2 hyphens and 2 square brackets here comments out the following code | --[[ putting 2 hyphens and 2 square brackets here comments out the following code | ||
function thisfunctiondoesnothingnow(squad) | function thisfunctiondoesnothingnow(squad) | ||
- | killsquad(squad) | + | |
end | end | ||
]] -- the last 2 square brackets ends the commenting | ]] -- the last 2 square brackets ends the commenting | ||
+ | </ | ||
Prevention: Liberally use comments | Prevention: Liberally use comments | ||
Line 61: | Line 61: | ||
Code: | Code: | ||
+ | < | ||
-**- compare these 2** | -**- compare these 2** | ||
Line 82: | Line 82: | ||
function Indentexample() | function Indentexample() | ||
- | if count = 1 then | + | |
- | if alphabet = 26 then | + | |
- | if language = " | + | |
- | print ("25 to go") | + | print ("25 to go") |
- | end | + | end |
- | end -- so easy to see which if this end belongs to! | + | |
- | end | + | |
end -- end of function | end -- end of function | ||
+ | </ | ||
And yes, there is a missing end in the not indented example | And yes, there is a missing end in the not indented example | ||
Line 115: | Line 115: | ||
Code: | Code: | ||
+ | < | ||
function Check_SoldierHasTwoArms(soldier) | function Check_SoldierHasTwoArms(soldier) | ||
- | if arms = 2 then | + | |
- | print (" | + | |
- | end -- end of if chunk | + | |
end -- end of function. (I always put this comment here so I know I have closed the function properly) | end -- end of function. (I always put this comment here so I know I have closed the function properly) | ||
+ | </ | ||
Fix: go back and check each if for an end and then each function for an end (see indenting below). Document the ends that close a function | Fix: go back and check each if for an end and then each function for an end (see indenting below). Document the ends that close a function | ||
Line 133: | Line 133: | ||
Code: | Code: | ||
+ | < | ||
if (Squad_IsShooting(squad) and (Squad_IsInMeleeStance(squad) or Squad_IsUnderMeleeAttack(squad))then | if (Squad_IsShooting(squad) and (Squad_IsInMeleeStance(squad) or Squad_IsUnderMeleeAttack(squad))then | ||
- | Cmd_Retreat(squad.sgroup) | + | |
end | end | ||
Line 146: | Line 146: | ||
-- **we did not get to zero, omg a bracket is missing** | -- **we did not get to zero, omg a bracket is missing** | ||
- | Cmd_Retreat(squad.sgroup) | + | |
end | end | ||
+ | </ | ||
**Using = instead of == when making comparisons** | **Using = instead of == when making comparisons** | ||
Line 169: | Line 169: | ||
Code: | Code: | ||
+ | < | ||
mynewtable ={ | mynewtable ={ | ||
- | Scout, -- make sure you format tables like this so you can easily see your errors | + | |
- | Tac -- oops a missing comma | + | |
- | Devastator, -- it is a good idea to add a comma to the last entry so you can come back and add more entries later | + | |
} | } | ||
Line 185: | Line 185: | ||
-- | -- | ||
+ | </ | ||
**Mucking it up by cutting and pasting** | **Mucking it up by cutting and pasting** | ||
Line 235: | Line 235: | ||
Code: | Code: | ||
+ | < | ||
Squad_GetHealth(squad) -- this is correct | Squad_GetHealth(squad) -- this is correct | ||
Line 242: | Line 242: | ||
function Squad_NewHealthchecker(squad) | function Squad_NewHealthchecker(squad) | ||
- | return Squad_GetHealth(sQuad) -- this is a typo and will return a nil value so will seem to do nothing | + | |
end | end | ||
+ | </ | ||
Prevention: I tend to copy and paste names of functions and variables rather than retyping them to prevent this issue | Prevention: I tend to copy and paste names of functions and variables rather than retyping them to prevent this issue | ||
Line 255: | Line 255: | ||
Code: | Code: | ||
+ | < | ||
SGroup_Add(sgroup1) -- this won't work | SGroup_Add(sgroup1) -- this won't work | ||
SGroup_Add(sgroup1, | SGroup_Add(sgroup1, | ||
+ | </ | ||
Prevention/ | Prevention/ | ||
Line 277: | Line 277: | ||
Code: | Code: | ||
+ | < | ||
function This_IsMyNewFunction(squad) -- this function will only run if called from another function | function This_IsMyNewFunction(squad) -- this function will only run if called from another function | ||
- | print(" | + | |
end | end | ||
Line 288: | Line 288: | ||
-- original code that does stuff | -- original code that does stuff | ||
- | if Squad_IsInCombat(squad) then | + | |
- | This_IsMyNewFunction(squad) -- now this function calls my new function | + | |
- | end | + | |
end | end | ||
+ | </ | ||
The alternative is to call your code on initialisation using the Scar_AddInit() command. Once the simulation is loaded then Scar will run the initialisation commands (Scar_AddInit() does NOT work in .ai files) | The alternative is to call your code on initialisation using the Scar_AddInit() command. Once the simulation is loaded then Scar will run the initialisation commands (Scar_AddInit() does NOT work in .ai files) | ||
Code: | Code: | ||
+ | < | ||
function This_IsMyNewFunction() | function This_IsMyNewFunction() | ||
- | -- code that does stuff | + | |
end | end | ||
Line 310: | Line 310: | ||
function MyOnInit3() | function MyOnInit3() | ||
- | define_globalvariable1 = 3 | + | |
- | define_globalvariable2 = 30 | + | |
- | Rule_AddInterval(This_IsMyNewFunction, | + | |
end | end | ||
Scar_AddInit(MyOnInit3) -- make sure you give the oninit function a unique name different from all the other oninits to avoid conflict | Scar_AddInit(MyOnInit3) -- make sure you give the oninit function a unique name different from all the other oninits to avoid conflict | ||
+ | </ | ||
Related to this is forgetting to import your code. If you keep your code tidy by writing your functions in a separate file then you need to remember to add an import statement to one of the scar (or .ai) files | Related to this is forgetting to import your code. If you keep your code tidy by writing your functions in a separate file then you need to remember to add an import statement to one of the scar (or .ai) files | ||
Line 337: | Line 337: | ||
Code: | Code: | ||
+ | < | ||
function CeaseFireIfCitrus(squad) | function CeaseFireIfCitrus(squad) | ||
- | fruit_table = {" | + | |
- | if fruit_table[squad] == " | + | |
- | Squad_CeaseFire(squad) | + | |
- | end | + | |
end | end | ||
Line 352: | Line 352: | ||
function FruitCounter(squad) | function FruitCounter(squad) | ||
- | for i=1, squad do -- this won't work either | + | |
- | -- code that counts fruit | + | |
- | end | + | |
end | end | ||
**Failing to understand that the .ai and .scar sytems are separate** | **Failing to understand that the .ai and .scar sytems are separate** | ||
+ | </ | ||
The artificial intelligence for the game is split between the AI files (end in .ai) and the scar files (end in .scar). They are separates as if by a wall and do not talk to each other except through a specific function (AI_DoString) | The artificial intelligence for the game is split between the AI files (end in .ai) and the scar files (end in .scar). They are separates as if by a wall and do not talk to each other except through a specific function (AI_DoString) | ||
scar_coding.1705847307.txt.gz · Last modified: 2024/01/21 14:28 by rap6tor