User Tools

Site Tools


scar_coding

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
scar_coding [2024/01/21 14:28] – created rap6torscar_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://www.ebswift.com/scite-text-editor-installer.html I've corrected the link to ScIte, however a modern code writing program may be just as good.+https://www.ebswift.com/scite-text-editor-installer.html //We've corrected the link to ScIte, however a modern code writing program may be just as good.//
  
 **COMMON LUA ERRORS** **COMMON LUA ERRORS**
Line 37: Line 37:
  
 Code: Code:
 +<code>
 arms = 1000 -- qqq temporary test value to see if function working arms = 1000 -- qqq temporary test value to see if function working
 +</code>
 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:
 +<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)+  killsquad(squad)
  
 end end
  
 ]]     -- the last 2 square brackets ends the commenting ]]     -- the last 2 square brackets ends the commenting
 +</code>
 Prevention: Liberally use comments Prevention: Liberally use comments
  
Line 61: Line 61:
  
 Code: Code:
 +<code>
 -**- compare these 2** -**- compare these 2**
  
Line 82: Line 82:
 function Indentexample() function Indentexample()
  
-if count = 1 then+  if count = 1 then
  
-if alphabet = 26 then+    if alphabet = 26 then
  
-if language = "English" then+      if language = "English" then
  
-print ("25 to go")+         print ("25 to go")
  
-end+       end
  
-end    -- so easy to see which if this end belongs to!+    end    -- so easy to see which if this end belongs to!
  
-end+  end
  
 end     -- end of function end     -- end of function
 +</code>
 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:
 +<code>
 function Check_SoldierHasTwoArms(soldier) function Check_SoldierHasTwoArms(soldier)
  
-if arms = 2 then+  if arms = 2 then
  
-print ("yes")+    print ("yes")
  
-end -- end of if chunk+  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)
 +</code>
 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:
 +<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)+  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)+  Cmd_Retreat(squad.sgroup)
  
 end end
 +</code>
 **Using = instead of == when making comparisons** **Using = instead of == when making comparisons**
  
Line 169: Line 169:
  
 Code: Code:
 +<code>
 mynewtable ={ mynewtable ={
  
-Scout,                      -- make sure you format tables like this so you can easily see your errors+  Scout,                      -- make sure you format tables like this so you can easily see your errors
  
-Tac                         -- oops a missing comma+  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+  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:
  
 -- --
 +</code>
 **Mucking it up by cutting and pasting** **Mucking it up by cutting and pasting**
  
Line 235: Line 235:
  
 Code: 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+  return Squad_GetHealth(sQuad) -- this is a typo and will return a nil value so will seem to do nothing
  
 end end
 +</code>
 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:
 +<code>
 SGroup_Add(sgroup1)  -- this won't work SGroup_Add(sgroup1)  -- this won't work
  
 SGroup_Add(sgroup1, squad2) -- this will work SGroup_Add(sgroup1, squad2) -- this will work
 +</code>
 Prevention/Fix: Check the scar doc for the right number of parameters for the function Prevention/Fix: Check the scar doc for the right number of parameters for the function
  
Line 277: Line 277:
  
 Code: 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("Eldar are OP")+  print("Eldar are OP")
  
 end end
Line 288: Line 288:
 -- original code that does stuff -- original code that does stuff
  
-if Squad_IsInCombat(squad) then+  if Squad_IsInCombat(squad) then
  
-This_IsMyNewFunction(squad) -- now this function calls my new function+    This_IsMyNewFunction(squad) -- now this function calls my new function
  
-end+  end
  
 end end
 +</code>
 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:
 +<code>
 function This_IsMyNewFunction() function This_IsMyNewFunction()
  
--- code that does stuff+  -- code that does stuff
  
 end end
Line 310: Line 310:
 function MyOnInit3() function MyOnInit3()
  
-define_globalvariable1 = 3+  define_globalvariable1 = 3
  
-define_globalvariable2 = 30+  define_globalvariable2 = 30
  
-Rule_AddInterval(This_IsMyNewFunction, 3) -- note that Rules cannot be called with parameters (a workaround using local functions is available)+  Rule_AddInterval(This_IsMyNewFunction, 3) -- note that Rules cannot be called with parameters (a workaround using local functions is available)
  
 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
 +</code>
 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:
 +<code>
 function CeaseFireIfCitrus(squad) function CeaseFireIfCitrus(squad)
  
-fruit_table = {"apple", "pear", "orange"}+  fruit_table = {"apple", "pear", "orange"}
  
-if fruit_table[squad] == "orange" then    -- this won't work+  if fruit_table[squad] == "orange" then    -- this won't work
  
-Squad_CeaseFire(squad)+    Squad_CeaseFire(squad)
  
-end+  end
  
 end end
Line 352: Line 352:
 function FruitCounter(squad) function FruitCounter(squad)
  
-for i=1, squad do                -- this won't work either+  for i=1, squad do                -- this won't work either
  
--- code that counts fruit+  -- code that counts fruit
  
-end+  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**
 +</code>
 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