PDA

View Full Version : [HOW TO] Debug (aka commands for testing) scar script



Cozmo
27th May 09, 4:16 PM
OK, blade made a request and I will fulfill it.

Into

Scar can be VERY useful when you want to test/debug things. You can map scar functions of keyboard keys/ combo's of keys. These functions can do anything you can program (except create new hot keys). I will also cover another very useful function, reloading the scripts.

Steps


Create a scar file. e.g.
debug.scar
Next we need to import this file so the game runs it, scarutil is used to import all of relics scar files and it can load ours too.

So add an import function to scarutil, so it would look like this:
(This is just the lines immediately around the new line, don't delete everything to make it look exactly like this).


import("TechTreeSetup.scar")
import("Screensaver.scar")
import("Team.scar")
import("Tanks.scar")
import("StateMachine.scar")
import("NIS.scar")
import("Debugger.scar")
import("GarrisonBuilding.scar")
import("debug.scar")

-- this must be loaded after the MissionPresets.scar file
if Misc_IsCommandLineOptionSet("dev") then
import("SP_Cheatscript.scar")
end
Now the file its self, I will show you how to spawn a unit at the position of the mouse when pressing the "0" key on the numpad.

We need to register the key binding. I do it in the OnInit function so it happens at the very beginning. But you can do it anywhere (except a function called by a key binding).


function OnInit()
Scar_DebugConsoleExecute("bind([[NUMPAD0]], [[Scar_DoString('Spawn_Unit()')]])")
Scar_DebugConsoleExecute("bind([[NUMPAD1]], [[Scar_DoString('Reload_Script()')]])")
end

Scar_AddInit(OnInit)

Time to break the thing down:

The first argument takes a string that defines the key to be binded to. The syntax for combo's is CTRL+NUMPAD0 (If you wanted it to run when you press control and "0" on the numpad)

Scar_DebugConsoleExecute("bind([[NUMPAD0]], [[Scar_DoString('Spawn_Unit()')]])")
This is what will happen when you press the keys you defined in the first argument. I am only covering the Scar_DoString (aka fire a function), But you can do a few other things too.

Scar_DebugConsoleExecute("bind([[NUMPAD0]], [[Scar_DoString('Spawn_Unit()')]])")

so now you have your bindings set up it is time to create the functions they call.

The reload script function. This is a very useful function means you can make amends to a scar script and not have to restart the whole application.

function Reload_Script()
Util_ReloadScript()
end
Just press your hot key, a message will be displayed in the console to the effect of "scripts reloaded", hit restart (in the main menu) and any scar scripts you amended will run the new code.
---------------------
Spawning, this takes 3 steps

Get the position (for this tutorial the position of the mouse on the terrain). This is very easy:

local pos = Misc_GetMouseOnTerrain()
create a variable and this function will return the position of the mouse on the terrain.
-----------------
Next we need to create an SGroup for the new unit to be spawned into.

local sg = SGroup_CreateIfNotFound("sg1")
SGroup_Clear(sg)
It is good practice to clear this group right away in-case it already exists and has units in it. If you just wanted to add a new squad to an existing SGroup you wouldn't need to create it again ;).
----------------
Now the spawning function, I use the high level function that pretty much directly interacts with the game. Relic has made a few lower level functions, but all they do it take the arguments you entered in them and enter them into this function.

Util_CreateSquads(Game_GetLocalPlayer(), sg, SBP.ALLIES.SHERMAN, pos)

There are loads of arguments for this function (see ScarDoc (http://omgmod.com/scar_do/scar_doc.htm)). However you only really need these first 4 for simple spawning: The player, The SGroup, The blueprint (either from LuaConst or from BP_GetSquadBlueprint) and the position.
--------
and the function in full:

function Spawn_Unit()
local pos = Misc_GetMouseOnTerrain()
local sg = SGroup_CreateIfNotFound("sg1")

Util_CreateSquads(Game_GetLocalPlayer(), sg, SBP.ALLIES.SHERMAN, pos)
end





And that pretty much is it, if you are good at scar/lua then you can do quite useful debugging scripts this way. I have tried to make this a newbie friendly as possible, so I haven't covered very much. But hopefully this answers Blade's question.

Darkbladecr
27th May 09, 4:51 PM
Nice work! Well written and easy to understand!
Thanks for the help coz! ;)

rex123210
28th May 09, 11:38 PM
what is scar??

sirpsychoj
29th May 09, 12:19 AM
Yeah very good Cozmo.

@ rex123210, scar is written code that can be implemented into any map or gamemode that can overide the standard play type, just like campaigns, its code to tell stuff to spawn, attack, defend, build, etc. It can enhance a map greatly by adding certain things done by code, or you can go full hog and write full on campaigns with movies etc, all done by code, which is called scar. One beauty of it is, it can be packed into a maps files and overide the normal attributes, eg. You can increase the population cap, the resources, upgrades, abilities, you can even use it to balance the game on certain units or all units that everyone seems to complains about being OP. Using both scar code and the Modstudio is a COH's modders dream, its well worth learning how to do some basic scar code rather than none at all, theres some excellent How To's in the How To's section for beginners