Jump to content
Sign in to follow this  
CombatComm

Please correct my script so I may learn

Recommended Posts

Hi, I'm new to advanced scripting usually sticking to simple statements in trigger on act sections. I like to use placed units as opposed to createvehicle for realism. IN this case a C-130 on standby for players call for air drop. It turns engines on, gets its waypoints (the players position) and than when it is within 30 meters of player it drops its cargo. Eventually id like to get the script to the point where based on the radio trigger called a different script is called within the script. A goto "Ammo1" goto "ammo2" scenario but I am nowhere close to that level yet. So here is my script, not working at the moment. Please look at it and let me know what im doing right and wrong. This is the best way for me to learn. Correction. Thank you in advance to anyone who takes the time.

start = this select 0;

end = this select 1;

if (fog < 5) then {player sidechat "Echo 1-1, aerial resupply is inbound! Ensure DZ is clear of enemy forces.";select 0} else {player sidechat "Weather is Echo 1-1 aerial ressupply is a nogo.";select 1};

start = sleep 8;

_distance = drop distance player;

drop setfuel 1;

way1 = (group drop) addWaypoint [position player, 0];

way1 setWaypointType "SAD";

way1 setWaypointBehaviour "COMBAT";

way1 setWaypointCombatMode "RED";

way1 setWaypointSpeed "NORMAL";

way2 = (group pilot) addWaypoint [getMarkerPos "HELI", 0];

way2 setWaypointType "GETOUT";

way2 setWaypointBehaviour "AWARE";

way2 setWaypointCombatMode "YELLOW";

way2 setWaypointSpeed "NORMAL";

way3 = (group pilot) addWaypoint [getMarkerPos "HOLD",0];

way3 setWaypointType "CYCLE";

way3 setWaypointBehaviour "AWARE";

way3 setWaypointCombatMode "YELLOW";

way3 setWaypointSpeed "NORMAL";

if (_distance < 30) then {_para = "ParachuteMediumWest_EP1" createVehicle [0,0,0];

_para (getpos drop select 2) - 20];

_ammo1 = "USOrdnanceBox" createVehicle _pos;

_ammo1 attachTo [_para, [0,0,-1.6]];

sleep 1.0;

WaitUntil{(getpos _ammo1 select 2) < 0.01};

detach ammo1;

end = exit;

Edited by CombatComm

Share this post


Link to post
Share on other sites

Still there are problems: not defined _pos variable and incomprehensible to me usage of "start" and "end" global variables

start = _this select 0;//why this variable "start" is global (without _))? Will be used outside this script? 
end = _this select 1;//lack of "_"

if (fog < 5) then //maximum fog is 1, as far, as I know maybe you want 0.5 here?
{
player sidechat "Echo 1-1, aerial resupply is inbound! Ensure DZ is clear of enemy forces."
} 
else 
{
player sidechat "Weather is Echo 1-1 aerial ressupply is a nogo."
};//deleted "selects". Don't know, why they was here, but select is not used that way


start = sleep 8;//What this should do? 8- second pause? then only "sleep 8;" should be here. To what refer "start" and end "variables"? (Try rather to name yours global variables in more distinctive, unique way eg CC_Start instead of Start, cause some other script or addons may to use global variable of same name when this name is too common)
_distance = CC_drop distance player;//"drop" is name reserved as some particle effect command, so change was required; added by me below "waitUntil" loop makes this line redundant - may be deleted

CC_drop setfuel 1;
_way1 = (group CC_drop) addWaypoint [position player, 0];
_way1 setWaypointType "SAD";
_way1 setWaypointBehaviour "COMBAT";
_way1 setWaypointCombatMode "RED";
_way1 setWaypointSpeed "NORMAL";//such waypoint (SAD&COMBAT&RED) will make CC_drop vehicle behavior very "militant", so its behavior will be hardest to control and to predict. This is, what you want? I think, better eg MOVE&SAFE&GREEN or something like that; Assumed, that way1 is not used by other scripts, so may be local, so added "_"

(group CC_drop) setCurrentWaypoint _way1; //possibly redundant, but will do not harm

_way2 = (group CC_pilot) addWaypoint [getMarkerPos "HELI", 0];
_way2 setWaypointType "GETOUT";
_way2 setWaypointBehaviour "AWARE";
_way2 setWaypointCombatMode "YELLOW";
_way2 setWaypointSpeed "NORMAL";

_way3 = (group CC_pilot) addWaypoint [getMarkerPos "HOLD",0];
_way3 setWaypointType "CYCLE";
_way3 setWaypointBehaviour "AWARE";
_way3 setWaypointCombatMode "YELLOW";
_way3 setWaypointSpeed "NORMAL";

waituntil
{
sleep 0.1;//to make script a little lighter
_distance = CC_drop distance player;
(_distance < 30)
};//_distance variable was previously defined only once, so was never changed since then, so this waituntil loop is needed for cyclically (10 times per second) refresh of its value and condition check.

_para = "ParachuteMediumWest_EP1" createVehicle [0,0,0];//when _distance < 30, loop is ended and next lines are executed

_para setpos [(getpos CC_drop) select 0,(getpos CC_drop) select 1,((getpos CC_drop) select 2) - 20];//I'm not sure, but probably this is code, that do, what you want here. Probably some brackets are redundant here, but makes code clearer, at least to me   
_ammo1 = "USOrdnanceBox" createVehicle _pos;// _pos variable not defined, should be earlier line like _pos = getPos something;
_ammo1 attachTo [_para, [0,0,-1.6]];
sleep 1.0;

WaitUntil
{
sleep 0.1;
((getpos _ammo1) select 2) < 0.2
};//sleep added, some bigger height in condition setted, cause sometimes height of object on the ground may be very slightly bigger than 0

detach _ammo1;//lack of "_"

end = exit;//what this should do? "exit" is a command, that is ignored in sqf. if this should to end script, just delete this line. Script will end here anyway. 

Maybe also create bookmark at this page:

http://community.bistudio.com/wiki/Category:Scripting_Commands_ArmA2

Is very useful...

Also, to check, why yours script is not working, try to find yours RPT file and use it often:

Where is my RPT file

Edited by Rydygier

Share this post


Link to post
Share on other sites

You need to understand how to structure your scripts. Your script doesn't make too much sense. For instance here's how you would check the distance between your aircraft and the player.

You need to loop the script over and over until a specific condition or set of conditions is met. Here's two different ways....

_distance = 9999;

while {_distance > 30} do {

//don't use "drop"...it's a reserved keyword
_distance = dropveh distance player;

 sleep 0.5;	//check every half second

};

[i]do your parachute stuff here....[/i]

or...

 waituntil {sleep 0.5; dropveh distance player < 30};

[i]do your parachute stuff here....[/i]

There's lots of other stuff wrong as well. I suggests you search the forums for "airdrop" and read a shitload of other peoples code and it will eventually make more and more sense. There's lots of questions about airdrops already answered.

The Sticky threads at the top of the Forum has links to a lot of useful stuff as well.

It's all about searching for your answers.

Share this post


Link to post
Share on other sites

Thank you both for taking the time. Ok so Im sure both of you were confused haha, preatty terrible first attempt. Rydgier, let me explain what I was trying to do with select/goto. I was hoping their was a way to do something like where whether the "then or else" is true than the script skips to a certain block of code, within the same script. Like the goto command I guess?? And than understand the loop system. Which I think I understand a little better from yalls help. The main thing is I wanted to if fog was too heavy the script to exit and the player would be forced to try again later if the fog has cleared up. Thats why I tried to send the script to exit in the "else" statement. I know im probably not making much more sense. I know commands and quite a few statements. I guess my main concern now is, like you said, learning to structure and loop my scripts. I need to be able to effectively and cleanly (not kill me CPU) check for conditions throughout the mission or after certain things happen. And than for more complicated scripts use variables and complicated scripts like attachto. If you all could continue to help me understand the select 0,1,2,3 thing and how or if that is used to select certain blocks of codes based on the output of conditions that would be awesome. :)

Share this post


Link to post
Share on other sites

You'll get the hang of it :) persistence is key if you're self learning.

...

If you all could continue to help me understand the select 0,1,2,3 thing and how or if that is used to select certain blocks of codes based on the output of conditions that would be awesome. :)

select stuff... well those must be arrays, selecting elements inside arrays.

Arrays hold indexed elements:

The number after a "select" represent the index of the element you want to make reference to. The first element is always "0".

The expression before a "select" is the array you want to select data from.

Elements within an array may be of any valid type o data (ie objects, numbers, strings of text, etc...). You can create and mix these types of data in the same array.

In regards to loops and code structure you may check this.

I would also suggest to keep this reference always side-by-side during your efforts. If you're looking for something new make a keyword search on that page, there are plenty of small examples there to help you through.

Share this post


Link to post
Share on other sites

Some useful links for you....

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

and most important.... Mr. Murray's Editing Guide.

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

With Mr Murray's Editing Guide there are a lot of .sqs examples. This is the old way of writing code.... but the guide still applies big time!

Don't use .sqs... use .sqf. It's much quicker and you can do more.

EDIT: ...also... when posting in here don't post your question like one big block. It's too hard to read.... many people will simply skip it! Use short paragraphs and separate them with empty lines.

Edited by twirly
Added stuff

Share this post


Link to post
Share on other sites
Rydgier, let me explain what I was trying to do with select/goto. I was hoping their was a way to do something like where whether the "then or else" is true than the script skips to a certain block of code, within the same script. Like the goto command I guess?? And than understand the loop system. Which I think I understand a little better from yalls help. The main thing is I wanted to if fog was too heavy the script to exit and the player would be forced to try again later if the fog has cleared up. Thats why I tried to send the script to exit in the "else" statement. I know im probably not making much more sense. I know commands and quite a few statements. I guess my main concern now is, like you said, learning to structure and loop my scripts. I need to be able to effectively and cleanly (not kill me CPU) check for conditions throughout the mission or after certain things happen. And than for more complicated scripts use variables and complicated scripts like attachto. If you all could continue to help me understand the select 0,1,2,3 thing and how or if that is used to select certain blocks of codes based on the output of conditions that would be awesome. :)

Ah yes, now I understand. You tried to use select for sqs style GOTO loops. I liked this loops too, but this is, I afraid, song of the past, as all sqs syntax (good as introduction into scripting for some people however). There isn't such "GOTO" loops in sqf... Well there is something somewhat similar, but it isn't exactly the same (never used by me):

ScopeName

breakTo

breakOut

still there are other, usually better methods instead to do same thing.

(inside spoiler more detailed info about scopes and _this select usage, please to correct me if anyone knows something, that I do not know)

It is important to understand, what is the "scope" thing. Simply - it is a script level. Generally code in {} brackets is a sub-scope - scope of more "inner" level (in rare situations, eg when spawn {some code}; is used, contents of {} (some code) will be completely separate scope, same as other, independent script - see below how to proceed then). Variables with "_" (local) are recognized only in scope, where was defined and in all more inner sub-scopes. There are also some other things with this concept, but enough of this for now. So, if you prepare such code:

if (fog > 0.5) then {_message = "noGo"};

player sidechat _message;

Message will not appear, because _message is definded in sub-scope (so recognized only in this and more inner scopes, if some exists), and next used in main script scope.

This however will work (message will appear regardless of fog, but kind of message will be dependant of fog level):

_message = "On the way!";
if (fog > 0.5) then {_message = "noGo"};

player sidechat _message;

because _message was defined in same, main scope, as that, in which _message is used at end of the code. It is also proper changed from "On the way!" to "NoGo", because changes maded after definig are recognized everywhere, where variable itself is recognized, unless private command is used...

This will work also, message will be dispalyed, when fog will be heavy enough

if (fog > 0.5) then {_message = "noGo";player sidechat _message};

and this too:

_message = "noGo";
if (fog > 0.5) then {player sidechat _message};

BTW about this:

start = _this select 0; 
end = _this select 1; 

Such code is used to introduce, here as two global variables (usually there will be local variables) of names "start" and "end" some values from entirely other scope (eg other script) into this script. _this represents an array of such values, select 0 represents first value from _this array, select 1 - second.

For example let's say, that you prepared "first.sqf" script:

_FirstVar = 0;
_SecondVar = "teddybear";

[] execVM "second.sqf";

There are two variables defined - a number and a string. And you want to use this values in executed from here "second.sqf" script, that looks like:

hint (str _FirstVar);
sleep 5;
hint _SecondVar;

But this is not working, because local variables was defined in other scope (other script). How to make this work? Two methods for example...

First: to make these variables global (delete "_"). Now, after defining, will be recognized by any other script/scope:

FirstVar = 0;
SecondVar = "teddybear";

[] execVM "second.sqf";

and:

hint (str FirstVar);
sleep 5;
hint SecondVar;

but if you change value of this global variables in "second.sqf", then for "first.sqf", and for any other script/scope too, their values also will be changed, so usually better way for "second.sqf" may be:

_first = FirstVar;
_second = SecondVar;//now you operate on local variables with same value, as these global, so there is no risk to change global variable value for all scripts/scopes

hint (str _first);
sleep 5;
hint _second;

Second way, probably best, is to use _this array:

"first.sqf"

_FirstVar = 0;
_SecondVar = "teddybear";

[_FirstVar, _SecondVar] execVM "second.sqf";//all inside [] brackets will pass into "second.sqf" as _this array

"second.sqf"

_first = _this select 0;//(means _first = _FirstVar)
_second = _this select 1;//(means _second = _SecondVar)

hint (str _first);
sleep 5;
hint _second;

Now "second.sqf" will work.

So, my example of yours code now will look like this:


if (fog >= 0.5) exitwith 
{
player sidechat "Weather is Echo 1-1 aerial ressupply is a nogo."
}; //this makes, that script will exit main scope (script itself) with such message when fog is 50% or more 

player sidechat "Echo 1-1, aerial resupply is inbound! Ensure DZ is clear of enemy forces."; // this message will appear and next lines will be executed only, if fog will be < 0.5, so script will not exit earlier. 

sleep 8;

CC_drop setfuel 1;
_way1 = (group CC_drop) addWaypoint [position player, 0];
_way1 setWaypointType "MOVE";
_way1 setWaypointBehaviour "CARELESS";
_way1 setWaypointCombatMode "GREEN";
_way1 setWaypointSpeed "NORMAL";//changed to MOVE&CARELESS&GREEN, so plane named "CC_drop"  shouldn't to engage in any combat or, hopefully, any other interacion or action beacuse of noticed enemy presence, so should to go, where is needed regardless of situation

(group CC_drop) setCurrentWaypoint _way1; //possibly redundant, but will do not harm

_way2 = (group CC_pilot) addWaypoint [getMarkerPos "HELI", 0];
_way2 setWaypointType "GETOUT";
_way2 setWaypointBehaviour "AWARE";
_way2 setWaypointCombatMode "YELLOW";
_way2 setWaypointSpeed "NORMAL";

_way3 = (group CC_pilot) addWaypoint [getMarkerPos "HOLD",0];
_way3 setWaypointType "CYCLE";
_way3 setWaypointBehaviour "AWARE";
_way3 setWaypointCombatMode "YELLOW";
_way3 setWaypointSpeed "NORMAL";

WaitUntil
{
sleep 0.1;
_distance = CC_drop distance player;
(_distance < 30)
};

_para = "ParachuteMediumWest_EP1" createVehicle [0,0,0];

_para setpos [(getPos CC_drop) select 0,(getPos CC_drop) select 1,((getPos CC_drop) select 2) - 20];   
_ammo1 = "USOrdnanceBox" createVehicle (getPos _para);//my proposition instead of undefined _pos. 
_ammo1 attachTo [_para, [0,0,-1.6]];
sleep 1.0;

WaitUntil
{
sleep 0.1;
((getpos _ammo1) select 2) < 0.2
};

detach _ammo1;

Edited by Rydygier

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  

×