Jump to content
Sign in to follow this  
Zombitch

What are scripting best practices ? And some beginner questions.

Recommended Posts

Hello,

As I am new to Arma3 scripting, I wonder what are best practices ? Can you share your tips and your practices ?

For instance I saw a lot of people scripting with external files such : init.sqf or intro.sdf.

And I guess init.sqf is to initialize some value/units/weapons before the mission start but I wonder where, when and how to put that script (in a game logic ?) ?

Same question for the intro.sdf ?

Thanks for your help.

Edit : some more question :

Is it possible to deal with global variable ? If so can you show me an example please ?

Is it possible to call a timer ? (I mean for instance that each X seconds the player loose some health , this is just an example)

What is the difference between *.sqs and *.sqf file ?

Edited by Zombitch

Share this post


Link to post
Share on other sites

The init.sqf is a file located in your mission directory, it runs automatically at mission start. You might be interested in this page:

http://community.bistudio.com/wiki/6thSense.eu:EG

Second, a global variable is something that is not a keyword (such as a command like 'getPos') and does not start with an underscore. An example is 'Target1'; that variable can be accessed and modified by any function. A local variable in a function (example: '_unit') is defined only in that function and in all lower scopes. Functions called by another function are a lower scope. See this page:

http://community.bistudio.com/wiki/Variables

Regarding a timer, you could use 'sleep x;'. You may be interested in these too:

http://community.bistudio.com/wiki/waitUntil

http://community.bistudio.com/wiki/addEventHandler

Further reading:

http://community.bistudio.com/wiki/Code_Optimisation

Edit: SQS is deprecated language that you do not need to use (I never have). SQF is the standard scripting syntax.

Edited by Zenophon

Share this post


Link to post
Share on other sites

If you already know the basics of sqf (if statements etc), skip the spoiler and head further down.

(A note, the example of loops and statements provided will not use ArmA-related commands and are more math related than anything else)

Honestly, how quick you are going to learn SQF largely depends on your knowledge in other object-oriented programming languages (like C++ for example (and yes I suck at describing programming languages so C++ might just not be an object-oriented language)). In SQF, you have very basic things that are present in other languages aswell. These are the following things:

if-statements

while loops

for each loops

if else statements (although they are executed differently than in languages such as, example, C++, I'll show you how further down)

for loops

Quick lesson on how each statement / loop is set up, if you want to learn them extensively, read up on them further, or just try and use them.

if statement, technical example:

if (condition) then {
// code to execute
};

The condition is closed into regular brackets, and a curly bracket opens up the actual code to execute. The line starting with // is a comment. An alternative version of a comment starts with /* and ends with */, and this version is most commonly used if you have a comment ranging over multiple lines of code. Here's a practical example of how to use an if-statement.

_i = 0;
if (_i == 0) then {
_i = _i + 1;
};

You'll notice we've introduced something called a local variable. It is local because it is preceded by an underscore. This makes it only accessible in that script, and nowhere else. The difference to this is a global variable, which is not preceded by an underscore. So the variable would simply be called i, not _i. A global variable can be accesed by any script in any folder, aslong as it is in the mission folder. (Read up on the mission folder hierarchy somewhere else, I'll provide some links later on)

An if else statement simply adds else to the equation. I'll jump to the technical example here:

if (condition) then {
// Code
} else (condition) {
// Code 
};

If the first condition is not true (false), then the else condition will kick in and execute, unless it is also false. At that point, since we have not added any more conditions, the if else statement will simply be ignored. I am not going to put up a practical example on this since it's basically the same as an if statement, just with an else added.

I personally recommend a forEach statement. It is much smoother than nesting tons of if else's at once. Technical example:

{
// Code
} foreach _variable / [array] / others;

Take note of the curly brackets. What forEach does it cycles through all the things provided to it (in the practical example it'll be numbers) and uses the things provided to it to complete the code written inbetween the curly brackets. Since that's pretty hard to understand, here's a practical example:

{
_a = _a + _x;
} forEach [1,2,3,4,5,6];

What this does is that _a receives the value of _a + _x. I'll go through what _a and _x actually are (_x is not a local variable in this case). _a is indeed a local variable, however there's something special about it. Remember when I told you about variables being preceded by an underscore are local? If you assign a value to a local variable within a scope, that variable will only be useable within said scope. A scope starts with a curly bracket and an ending curly bracket, so practically everything written within a statement or loop is within a scope. Since _a is assigned within a scope, you cannot use it outside the forEach loop. Make sure to define the variable outside the scope if you want to use it elsewhere.

_x is x as we know it from Algebra, math. It can be anything, and due to the forEach loop, _x will be replaced by every number provided to the loop within the array. An array is basically anything within sharp brackets (what's the actual english word for these?), so []. Arrays can store multiple things at once, and you access these things by using select (but please read up on that elsewhere).

The last one is the while loop. It runs everytime the condition is met, until the condition is false (not true). Technical example:

while {condition} do {
// Code
};

Again, take note of the curly brackets. A while loop, as mentioned, runs every second, until it's condition is not true anymore. This can be used to count things in a rapid way, for example.

_i = 0;
while {_i <= 10} do {
_i = _i + 1;
};

This basically adds 1 to _i every second. So at the beginning it's 0, then 1, then 2, etc. The loop will run until _i equals 10, so it'll run for 10 seconds total. Be aware that running a while loop for an extensive amount of time can cause the script to lag, and it takes quite some processing power for complicated while loops. There is also a more effective way to do the thing we just did in the practical example, and it can be achieved using a for loop. A for loop has 3 conditions, a starting condition, an end condition, and an increment condition. I can't really recall the for loop perfectly from my mind right now, so you'll have to look it up elsewhere.

These are the most basic things you will need to be successful at SQF scripting. These loops and statements are the foundation, and the rest is only ingame commands (such as getPos etc) that you can always look up on the Bohemia Interactive Wiki (BIKI).

I strongly recommend Mr. Murray's ArmA Editing Guide for newcomers. Be aware that you cannot use scripting examples in the book as they are written in SQS, which isn't what you want.

When it comes to practicing, and if you've got the main things I described in the spoiler down, then just make small missions. Really. Atleast try it, even if you'll hesitate, fail, or whatever. Start small, don't start to plan for another ArmA 3 Life server, that's out of your reach. Think about mission ideas. What about a small recon patrol being attacked by a 15 man force and suddenly a wild helicopter appears and kills all of them? You can accomplish the first part in the editor, but the second part will require you to think about using SQF. You'll have no idea how to accomplish it (unless you're already familiar with other languages, since it gets you in the thought process), and that's fine. But try to look around, what commands (I like to say 90% of SQF is commands and functions like getPos) or functions can you use to accomplish it, how do you do it, etc? Get in the thought process of it, and see how you can accomplsh it. But remember to start very small, and not plan big missions at once.

Share this post


Link to post
Share on other sites

Thanks for your answer guys.

@Zenophon

This is exactly what I was looking for ;)

@tryteyker

I'm used to language development, the things I want to know is the BIS functions and game logic ;)

And as you said, I am trying to make a simple rescue mission at the moment.

Share this post


Link to post
Share on other sites
Everything you need to know about Functions: /wiki/Functions_Library_(Arma_3)

For a simple rescue mission you really don't need to know half of the stuff in this thread though. Keep things simple, don't over complicate them.

This link help a lot (i didn't notice the function viewer in the editor) ! Thanks.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×