This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
arma:scripting [2008-04-30 15:03] snakeman added more script background info |
arma:scripting [2008-04-30 15:24] snakeman added bunch of info to the scripting. |
||
---|---|---|---|
Line 6: | Line 6: | ||
In a nutshell you never ever should create ArmA SQS content anymore, always create only SQF scripts. | In a nutshell you never ever should create ArmA SQS content anymore, always create only SQF scripts. | ||
+ | |||
====== Overview ====== | ====== Overview ====== | ||
- | ArmA has “” quotes for STRINGS only and {} curled braces for CODE only. Script commands doesn’t have to be in one line now with the curled braces. Command lines always end with ; instead of a ; *or* carriage return. So you can format code to be more readable | + | How do you run SQF script? |
+ | |||
+ | argument execVM filename | ||
+ | |||
+ | Example: | ||
+ | <code> | ||
+ | var = [player] execVM "test.sqf"; | ||
+ | </code> | ||
+ | |||
+ | Argument is passed to the script as local variable _this. | ||
+ | The Script is first searched for in the mission folder, then in the campaign scripts folder and finally in the global scripts folder. | ||
+ | |||
+ | ArmA has "" quotes for STRINGS only and {} curled braces for CODE only. Script commands doesn’t have to be in one line now with the curled braces. Command lines always end with ; instead of a ; *or* carriage return. So you can format code to be more readable | ||
<code> | <code> | ||
Line 21: | Line 34: | ||
DoSomething5; | DoSomething5; | ||
}; | }; | ||
- | } foreach array; | + | } forEach array; |
</code> | </code> | ||
Line 29: | Line 42: | ||
{if (condition) then {DoSomething1; DoSomething2; DoSomething3; DoSomething4; DoSomething5;};} foreach array; | {if (condition) then {DoSomething1; DoSomething2; DoSomething3; DoSomething4; DoSomething5;};} foreach array; | ||
</code> | </code> | ||
+ | |||
+ | ====== Comments ====== | ||
Comments are no longer the same character as the end of command character. | Comments are no longer the same character as the end of command character. | ||
Line 42: | Line 57: | ||
<code> | <code> | ||
/* | /* | ||
- | Comment line 1 | + | Comment line 1 |
- | Comment line 2 | + | Comment line 2 |
- | Comment line 3 | + | Comment line 3 |
- | Comment line 4 | + | Comment line 4 |
- | Comment line 5 | + | Comment line 5 |
- | Comment line 6 | + | Comment line 6 |
*/ | */ | ||
</code> | </code> | ||
Line 53: | Line 68: | ||
Quotes are no longer a valid substitute for braces. | Quotes are no longer a valid substitute for braces. | ||
- | Some SQF things | + | ====== Some SQF things ====== |
+ | |||
+ | Some misc SQF things here ;) | ||
<code> | <code> | ||
Line 64: | Line 81: | ||
</code> | </code> | ||
+ | And | ||
+ | <code> | ||
+ | myFunction1 = compile loadFile "myFunction1.sqf"; | ||
+ | myFunction2 = compile preprocessFile "myFunction2.sqf"; | ||
+ | |||
+ | call myFunction1; | ||
+ | [1, 2] call myFunction2; | ||
+ | </code> | ||
+ | |||
+ | Return value? | ||
+ | |||
+ | <code> | ||
+ | value = call compile preprocessFile "return.sqf"; | ||
+ | // value is now RETURN_VALUE | ||
+ | |||
+ | call compile preprocessFile "return.sqf"; | ||
+ | // valid, but RETURN_VALUE is not saved anywhere | ||
+ | </code> | ||
+ | |||
+ | ====== Private ====== | ||
+ | |||
+ | Using the private list. | ||
+ | |||
+ | <code> | ||
+ | private ["_t","_p","_yea"]; | ||
+ | </code> | ||
+ | Is to make the _local variables really a private ones in the SQF script. | ||
+ | |||
+ | ====== Exit while loop ====== | ||
+ | |||
+ | <code> | ||
+ | if (condition) exitWith {Code} | ||
+ | |||
+ | if (_x > 5) exitWith {echo "_x is too big"; _x} | ||
+ | </code> | ||
+ | |||
+ | and | ||
+ | |||
+ | <code> | ||
+ | for "_j" from 1 to 10 do | ||
+ | { | ||
+ | player sideChat format ["%1",_j]; | ||
+ | if (_j == 5) exitWith {player sideChat "5 is enough"}; | ||
+ | }; | ||
+ | player sideChat "Complete"; | ||
+ | </code> | ||
+ | |||
+ | ====== Switch ====== | ||
+ | |||
+ | Switch is quite nice option to check many conditions at once. | ||
+ | |||
+ | Example 1 | ||
+ | <code> | ||
+ | switch (_a) do | ||
+ | { | ||
+ | case 1: | ||
+ | { | ||
+ | hint "1"; | ||
+ | }; | ||
+ | case 2: | ||
+ | { | ||
+ | hint "2"; | ||
+ | }; | ||
+ | default | ||
+ | { | ||
+ | hint "default"; | ||
+ | }; | ||
+ | ;} | ||
+ | </code> | ||
+ | |||
+ | Example 2 | ||
+ | <code> | ||
+ | switch (_a) do | ||
+ | { | ||
+ | case true: | ||
+ | { | ||
+ | hint "true"; | ||
+ | }; | ||
+ | case false: | ||
+ | { | ||
+ | hint "false"; | ||
+ | }; | ||
+ | default | ||
+ | { | ||
+ | hint "default"; | ||
+ | }; | ||
+ | }; | ||
+ | </code> | ||
+ | |||
+ | Example 3 | ||
+ | <code> | ||
+ | switch (_a) do | ||
+ | { | ||
+ | case "string1": | ||
+ | { | ||
+ | hint "string1"; | ||
+ | }; | ||
+ | case "string2": | ||
+ | { | ||
+ | hint "string2"; | ||
+ | }; | ||
+ | default | ||
+ | { | ||
+ | hint "default"; | ||
+ | }; | ||
+ | }; | ||
+ | </code> | ||
+ | |||
+ | ====== Script to nil ====== | ||
+ | |||
+ | When you have script like this | ||
+ | <code> | ||
+ | MyScript = compile preProcessFile "myScript.sqf"; | ||
+ | </code> | ||
+ | |||
+ | When you want to unload the script, set MyScript equal to nil. | ||
+ | |||
+ | <code> | ||
+ | MyScript = nil; | ||
+ | </code> | ||
+ | |||
+ | ====== Dynamic Variables ====== | ||
+ | |||
+ | Dynamic variables are very cool, sometimes on those random/comples missions you want to make variables later in the mission, which you cannot specify at mission start (hardcode). | ||
+ | |||
+ | <code> | ||
+ | _MyVariable = 20; | ||
+ | _MyVariableContent = "MyContent"; | ||
+ | call compile format["MyDynamic_%1=_MyVariableContent",_MyVariable]; | ||
+ | </code> | ||
+ | |||
+ | This would create: MyDynamic_20 variable with content: "MyContent" | ||
+ | |||
+ | Another example | ||
+ | <code> | ||
+ | _name = format ["mytrigger%1", triggerIndex]; | ||
+ | call compile format ["%1 = createTrigger [....]", _name]; | ||
+ | </code> | ||
====== Misc PMC Example Scripts ====== | ====== Misc PMC Example Scripts ====== | ||
Place random vehicles + some units on all found **pmc_** gamelogics script, [[arma:scripting:pmc_logic1|here]]. | Place random vehicles + some units on all found **pmc_** gamelogics script, [[arma:scripting:pmc_logic1|here]]. |