Jump to content
Sign in to follow this  
stiivais

Need help regarding basic scripting (syntax/formatting?).

Recommended Posts

And don't do:

if (x) then {first line of code};

if (x) then {second line of code};

Do it this way:

if (x) then {first line of code; second line of code;}

Share this post


Link to post
Share on other sites

Also, not that it really matters, but you can comment your scripts like this:

/*

at the beginning of script a local variable "detected" would be set to 0

then the script would check the distance the player is away from 2 NPCs named target1 and target2 (don't know how to put in 1 statement)

then a comparison would be made, if the distance is lower than 20 meters, then the variable detected would be set to 1

then a check would be made, if the player is detected, then the enableAI "FSM" command would be ran for both of the targets

if the player is not detected, the script would rerun itself until the player is detected

*/

Instead of:

//at the beginning of script a local variable "detected" would be set to 0

//then the script would check the distance the player is away from 2 NPCs named target1 and target2 (don't know how to put in 1 statement)

//then a comparison would be made, if the distance is lower than 20 meters, then the variable detected would be set to 1

//then a check would be made, if the player is detected, then the enableAI "FSM" command would be ran for both of the targets

//if the player is not detected, the script would rerun itself until the player is detected

Share this post


Link to post
Share on other sites

//All the variables needed. Also note that if you want to check the distance of the player to the target, you don't need to name the unit, since it's automatically named player.

_discovered = 0;
_distance1 = player distance target1;
_distance2 = player distance target2;

// The if-statement is just there for testing to see of the variable _discovered is equal to 0 at mission start
if (_discovered == 0) then {
hint "Not detected";
};

/*
The while loop checks if _discovered is equal 0 zero all the time, if it's not, it will not execute the switch statement.
The switch statement checks different cases (I've modified the distance from 20 to 60, since 20 forced me to stand right next to target1 to even trigger it)
and triggers the code within the {}.
*/
while {_discovered == 0} do {
switch (true) do {
case (_distance1 < 60):
 {
 _discovered = 1;
 hint "Detected by Target1!";
 };
  case (_distance2 < 60):
 {
 _discovered = 1;
 hint "Detected by Target2!";
 };
};
};

Helpful link regarding all the while loops and switch statements:

http://seit.unsw.adfa.edu.au/coursework/ZEIT2305/Resources/SQF_Tutorial/basic_sqf.HTML

Additionally, if you want to read more about scripting:

http://www.armaholic.com/page.php?id=9220

Also note that the switch statement returns either true or false in all cases, since it's basically comparing numbers.

You can always add more cases to the above if you need to.

If you want to use a default message which displays if none of the above cases is true, simply add this after all the cases:

default 
{
// Block of code here
};

I strongly recommend not adding a default hint message when the switch is inside a while loop, since the while loop is checking this every second. Using hint hurts your ears. Try adding a sleep after the hint:

default
{
hint "Testing";
sleep 10;
};

This will, however, stop the complete while loop (if I tested it right), so I don't recommend using it with the above script.

Share this post


Link to post
Share on other sites

Forgot to mention hintsilent. It's usable, but it still uses up CPU / RAM due to it being executed every second.

Also whilst uisng default and sleep I didn't get it to execute any other cases with my code. You can play around with it though.

Oh and another thing that might be interesting:

|| = or

&& = and

== = equal to (= is an assignment used to assign a value to a variable, for example)

Also, I recommend looking up if, if else, else if statements.

Edited by tryteyker

Share this post


Link to post
Share on other sites

discovered = 0;
distance1 = player1 distance target1;
distance2 = player1 distance target2;

while {discovered == 0} do {
switch (true) do {
case (true):
 {
 distance1 = player1 distance target1;
 distance2 = player1 distance target2;
 hintSilent format["Triggers at 30 meters.\n\nDistances to targets:\n %1 and %2\n\nDetected:\n%3",round(distance1),round(distance2),discovered];
 };
 case (distance1 < 30):
 {
 hint "Detected by Target1!";
 discovered = 1;
 };
 case (distance2 < 30):
 {
 hint "Detected by Target2!";
 discovered = 1;
 };
 case (discovered == 1):
 {
 target1 enableai "AUTOTARGET";
 target1 enableai "FSM";
 target2 enableai "AUTOTARGET";
 target2 enableai "FSM";
 }
};
};

I've made the variables global. It updates the distance every second or so, but it does not switch cases. The case "true" is obviously true so it's going to stick with it forever, updating the distance and all that but not resetting the variable discovered.

I'll continue experimenting with it. If I insert the hintSilent into the while loop and remove the case "true" it doesn't update the distance or anything at all.

Share this post


Link to post
Share on other sites

discovered = 0;
distance1 = player1 distance target1;
distance2 = player1 distance target2;

while {discovered == 0} do {
hintSilent format["Triggers at 30 meters.\n\nDistances to targets:\n %1 and %2\n\nDetected:\n%3",round(distance1),round(distance2),discovered];
switch (true) do {
 case (distance1 < 30):
 {
 hint "Detected by Target1!";
 discovered = 1;
 };
 case (distance2 < 30):
 {
 hint "Detected by Target2!";
 discovered = 1;
 };
 case (discovered == 1):
 {
 target1 enableai "AUTOTARGET";
 target1 enableai "FSM";
 target2 enableai "AUTOTARGET";
 target2 enableai "FSM";
 };
};
};

There you go.

I'm still trying to figure out how to get him to check frequently for the variables.

Share this post


Link to post
Share on other sites

The hint in my script wasn't supposed to update.

Also, I'm not sure why you define _distance1 and _distance2 once again, it's unnecessary.

If you have a variable that's not defined at the start of the script (every variable at the top of the script can be used by everything within the script if it is local (e.g. _distance). If it is global (e.g. distance) then it can be used by other scripts in the same folder aswell) you have to define it within the while loop for example.

Share this post


Link to post
Share on other sites

You need to define _distance1 and _distance2 at the top of the file if you want to use them in different things.

For example:

_distance1 = player distance target1
_distance2 = player distance target2

if (_distance1 == _distance2) then {
//Block of code
}

_distance1 and 2 are already defined in this case.

Another example, where we define the variable within the scope.

_target = getpos target1 select 0;
_target1 = getpos target2 select 0;

if (_target == _target1) {
_distance = player distance _target;
}

In this example _distance is defined within the if-statement and thus only useable within the scope (basically the {} (braces)). You wouldn't be able to use _distance in another statement since it is not defined outside the if-statement.

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  

×