Home   Help Search Login Register  

Author Topic: function not working in ArmA  (Read 4797 times)

0 Members and 1 Guest are viewing this topic.

Offline Sparticus76

  • Members
  • *
function not working in ArmA
« on: 16 Jan 2008, 07:29:44 »
hi, the problem is in the ENGAGE TARGET section. Everything before that worked perfectly...
here's the script....


Code: [Select]
private ["_firer","_range","_rangeb","_gunner","_type","_crew","_distance","_targets","_target","_numtgts","_v","_n0","_n1","_tgttype","_tgtcnt","_enunits","_ttype","_tid","_fpos","_typcnt","_distvar","_na","_nb"];
_firer = _this select 0;
_range = _this select 1;
_rangeb = _range;
_gunner = gunner _firer;
_type = [];
_distance = [];
_enunits = [];
_ttype = [];
_tid = [];
_fpos = [];
_distvar = [];
_target = [];
_targets = _firer NearTargets _range;
_numtgts = count _targets;
_v = 0;
_n0 = 0;
_n1 = 1;
_tgttype = ["DSHKM","AGS","D30","BRDM2","UAZMG","UAZ_AGS30","BRDM2_ATGM","BMP2","ZSU","KA50","MI17_MG","SU34B","RHIB","RHIB2Turret"];
_tgtcnt = count _targets;



/*
; SCREEN FOR THREAT
*/

for [{_v = 0},{_v < _tgtcnt},{_v = _v + 1}] do
{
     _enunits = _targets select _v;   
     _ttype = _enunits select 1;
     _tid = _enunits select 4;
     if (_ttype in _tgttype) then {_type = _type + [_tid];};
};



/*
; GET RANGES TO NEWLY ARRAYED UNITS
*/

_typcnt = count _type;
for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _fpos = getpos _firer;
     _ttype = _type select _v;             
     _distance = _fpos distance getpos _ttype;
     _distvar = _distvar + [_distance];
};



/*
; USE DISTANCE ARRAY TO FIND OUT CLOSEST TARGET
*/

for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _ttype = _distvar select _v;
     if (_ttype < _rangeb) then {_rangeb = _ttype};
};


/*
; FIND TARGET IN ENEMY ARRAY THAT MATCHES CLOSEST TARGET RANGE
*/

for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _ttype = _type select _v;
     _tpos = getpos _ttype;
     _distance = _firer distance getpos _ttype;
     if ((_distance < _rangeb + 5) and (_distance > _rangeb - 5)) then {_target = _ttype};
};


/*
; ENGAGE TARGET
*/


_crew = crew _target;
for [{_v = 0},{alive _target},{_v = _v + 1}] do
{
     _ammo = _firer ammo "M2";
     _burst = _ammo - 20;
     for [{_p = 0},{_ammo > _burst},{_v = _v + 1}] do
     {     
          if ((!alive (_crew select 1)) and (!alive (_crew select 2)) or (getdammage _target == 1)) then {exitWith [_firer,_range] exec "findtgt.sqs"};
          _firer dowatch getpos _target;
          _firer action ["useWeapon", _firer, _gunner, 0];
          _ammo = _firer ammo "M2";
     };
     sleep 0.5;
};
exitWith [_firer,_range] exec "findtgt.sqs";


and here's the error's.....


Error in expression <etdammage _target == 1)) then {exitWith [_firer,_range] exec "findtgt.sqs"};
_fi>
  Error position: <[_firer,_range] exec "findtgt.sqs"};
_fi>
  Error Missing ;
File C:\Users\User\Documents\ArmA Other Profiles\Ahren\missions\test.Intro\target.sqf, line 94
Error in expression <irer ammo "M2";
};
sleep 5;
};
exitWith [_firer,_range] exec "findtgt.sqs";

>
  Error position: <[_firer,_range] exec "findtgt.sqs";

>
  Error Missing ;
File C:\Users\User\Documents\ArmA Other Profiles\Ahren\missions\test.Intro\target.sqf, line 101
Error in expression <irer ammo "M2";
};
sleep 5;
};
exitWith [_firer,_range] exec "findtgt.sqs";

>
  Error position: <[_firer,_range] exec "findtgt.sqs";

>
  Error Missing ;
File C:\Users\User\Documents\ArmA Other Profiles\Ahren\missions\test.Intro\target.sqf, line 101
Error in expression <elect 2)) or (getdammage _target == 1)) then {exitWith [_firer,_range] exec "fin>
  Error position: <then {exitWith [_firer,_range] exec "fin>
  Error then: Type String, expected Array,code
File C:\Users\User\Documents\ArmA Other Profiles\Ahren\missions\test.Intro\target.sqf, line 94


Any help much appreciated.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: function not working in ArmA
« Reply #1 on: 16 Jan 2008, 08:19:45 »
exitWith needs a boolean condition (true/false), so you have to have something in this vein
Code: [Select]
if (this == that) exitWith {blah};
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #2 on: 16 Jan 2008, 10:39:34 »
Thanks, got that working and adjusted things a little....it fires at the target now, but freezes when the crew jump out of the damaged vehicle.....here's the new script..

Code: [Select]
private ["_firer","_range","_return","_rangeb","_gunner","_type","_crew","_distance","_targets","_target","_numtgts","_v","_n0","_n1","_tgttype","_tgtcnt","_enunits","_ttype","_tid","_fpos","_typcnt","_distvar","_na","_nb"];
_firer = _this select 0;
_range = _this select 1;
_rangeb = _range;
_gunner = gunner _firer;
_type = [];
_distance = [];
_enunits = [];
_ttype = [];
_tid = [];
_fpos = [];
_distvar = [];
_target = [];
_targets = _firer NearTargets _range;
_numtgts = count _targets;
_return = true;
_v = 0;
_n0 = 0;
_n1 = 1;
_tgttype = ["DSHKM","AGS","D30","BRDM2","UAZMG","UAZ_AGS30","BRDM2_ATGM","BMP2","ZSU","KA50","MI17_MG","SU34B","RHIB","RHIB2Turret"];
_tgtcnt = count _targets;



/*
; SCREEN FOR THREAT
*/

for [{_v = 0},{_v < _tgtcnt},{_v = _v + 1}] do
{
     _enunits = _targets select _v;   
     _ttype = _enunits select 1;
     _tid = _enunits select 4;
     if (_ttype in _tgttype) then {_type = _type + [_tid];};
};



/*
; GET RANGES TO NEWLY ARRAYED UNITS
*/

_typcnt = count _type;
if (_typcnt == 0) then {_return;};_return;
for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _fpos = getpos _firer;
     _ttype = _type select _v;             
     _distance = _fpos distance getpos _ttype;
     _distvar = _distvar + [_distance];
};



/*
; USE DISTANCE ARRAY TO FIND OUT CLOSEST TARGET
*/

for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _ttype = _distvar select _v;
     if (_ttype < _rangeb) then {_rangeb = _ttype};
};


/*
; FIND TARGET IN ENEMY ARRAY THAT MATCHES CLOSEST TARGET RANGE
*/

for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _ttype = _type select _v;
     _tpos = getpos _ttype;
     _distance = _firer distance getpos _ttype;
     if ((_distance < _rangeb + 5) and (_distance > _rangeb - 5)) then {_target = _ttype};
};


/*
; ENGAGE TARGET
*/


_crew = crew _target;




While {alive _target} do
{
     _ammo = _firer ammo "M2";
     _burst = _ammo - 20;
     while {_ammo > _burst} do
     {     
         
          if ((damage _target) > 0.9) then {[_firer,_range] exec "findtgt.sqs";};_return;
          _firer dotarget _target;
          WaitUntil{unitready _firer};
          _firer action ["useWeapon", _firer, _gunner, 0];
          _ammo = _firer ammo "M2";
          sleep 0.18;
     };
     sleep 1;
};
_return;

....and here's the error report..

=====================================================================
== E:\Games\ArmA\arma.exe
=====================================================================
Exe version: Wed Oct 31 16:28:06 2007

Item STR_LIB_HILUX listed twice
Updating base class ->Default, by ca\anims\characters\config.bin/CfgMovesBasic/DefaultDie/
Updating base class ->Default, by ca\anims\characters\config.bin/CfgMovesMaleSdr/States/Crew/
Updating base class ->MGunCore, by ca\weapons\config.bin/cfgWeapons/MGun/
Applying controller scheme Default
Applying controller scheme Default
=======================================================
Date: 01/16/08  Time: 19:30:22
-------------------------------------------------------
Exception code: C0000005 ACCESS_VIOLATION at 0082098E
graphics:  D3D9, Device: NVIDIA GeForce 8800 GTS, Driver:nvd3dum.dll 7.15.11.6369
resolution:  1024x768x32
Addons:
  Datsun_armed_DBE1 in CA\DBE1\datsun_armed\, DBE1 in CA\DBE1\, Desert in ca\desert\
  CAIntroAnims in ca\introanims\, CASounds in ca\sounds\, DBE1_UI in CA\DBE1\ui\
  Desert2_Buildings in ca\Desert2\build\
  CA_Missions_Armory1 in ca\missions\armory1.intro\
  CAweapons3_m107 in CA\weapons3\m107\
  CAweapons3_m16a4_acg in CA\weapons3\m16a4_acg\, CAWheeled in ca\wheeled\
  Desert2_Objects in ca\Desert2\objects\, CA_Missions in ca\missions\
  CARoads in ca\roads\, CAWheeled3_TT650 in CA\wheeled3\tt650\, CAAir in ca\air\
  CA_Anims in ca\anims\, CAAnimals in CA\animals\
  CA_desert2_Characters in ca\Desert2\characters\, CAUI in ca\ui\
  CAWeapons in ca\weapons\, CAweapons3_m16a4 in CA\weapons3\m16a4\
  CAWeapons3_ammocrates in CA\weapons3\ammocrates\, CAAir3_Su34 in CA\air3\su34\
  CACharacters in ca\characters\, CA_CutSceneAnims in ca\cutSceneAnims\
  DC3_DBE1 in CA\DBE1\dc3\, CASigns in CA\signs\, CAFonts in ca\UIFonts\
  CAweapons3_m16a4_gl in CA\weapons3\m16a4_gl\, CAWeapons3 in CA\weapons3\
  CAWheeled3_M1030 in CA\wheeled3\m1030\, CAAir3 in CA\air3\, CAMisc in ca\misc\
  CAPlants in CA\plants\, CAVoice in ca\voice\, CAData in ca\
  CALanguage in ca\language\, CAWater in CA\water\
  CAweapons3_aks74pso in CA\weapons3\aks74pso\
  CA_Anims_Char in ca\anims\characters\, CATracked in ca\tracked\
  CAweapons3_ksvk in CA\weapons3\ksvk\, CAA10 in ca\a10\, Porto in ca\Desert2\
  6G30_DBE1 in CA\DBE1\6g30\, Sara in ca\sara\, CAWheeled3 in CA\wheeled3\
  CABuildings in ca\buildings\, Hilux_armed_DBE1 in CA\DBE1\hilux_armed\
  CARocks in Ca\Rocks\, CAweapons3_m16a4_acg_gl in CA\weapons3\m16a4_acg_gl\
Mods: CA
Distribution: 1294
Version 1.08.5163
Fault address:  0082098E 01:0041F98E E:\Games\ArmA\arma.exe
file:     test
world:    Intro
Prev. code bytes: 75 D0 32 C0 5F 5E C9 C3 8B 4C 24 08 8B 44 24 04
Fault code bytes: 8A 10 3A 11 75 17 84 D2 74 10 8A 50 01 3A 51 01

Registers:
EAX:203A7476 EBX:00917F38
ECX:7C264EE0 EDX:00000083
ESI:7FF998D8 EDI:00000001
CS:EIP:001B:0082098E
SS:ESP:0023:01C4F17C  EBP:01C4F1B8
DS:0023  ES:0023  FS:003B  GS:0000
Flags:00010212
=======================================================
note: Minidump has been generated into the file C:\Users\User\AppData\Local\ArmA\arma.mdmp


Also I dont know if I'm using the _return correctly to exit the script.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #3 on: 17 Jan 2008, 10:36:40 »
Didnt have time to check your script, but the _return is not used correctly there as scripts dont return anything. If that script was supposed to work as a function, then you cannot use any sleep or "long" whiles.

And
Code: [Select]
          _firer dotarget _target;
          WaitUntil{unitready _firer};
the waitUntil there will more than likely return immediately, as the unit will be ready just after the doTarget command.
Code: [Select]
          _firer dotarget _target;
          // Sleep 1 or more here
          Sleep 1;
          WaitUntil{unitready _firer};

Also there are some variables working into inner scopes like _ammo and _burst that are not defined in the global scope (private), that might cause unnexpected problems there.

consider also that another script is playing here: findtgt.sqs

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #4 on: 17 Jan 2008, 11:06:56 »
Thanks for the help.
Findtgt.sqs runs first, and then starts this script and exits until the target is destroyed, then this script re-initiates the findtgt.sqs and it all starts over again.
If you're saying this script initiates findtgt.sqs and doesnt exit itself, then how can I make the sqf exit after initiating the other script?

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #5 on: 17 Jan 2008, 11:27:58 »
No, what I'm saying is that, aside of the other points I described above (the scope one is way important), there is another script involved which might participate also in the crash.

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #6 on: 17 Jan 2008, 11:58:25 »
I set _return = true; because I read in one of the turorials, SnYpirs I think...that even if your function doesnt return something, you should make it return true.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #7 on: 17 Jan 2008, 12:05:54 »
Problem is that your code is not a function but a script. A function is executed with call command, a script with execVM or exec. What you execute with spawn command is also code equivalent to that of a script.

Basically, with call command you execute compiled code that doesnt have any sleep inside, that doesnt have any time consuming loop inside and that might return a value. With scripts you dont have these limitations, but you cannot return any value.

In any case, functions and sqf scripts use the same syntax, but are different things.

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #8 on: 17 Jan 2008, 12:11:43 »
so in other words, my sqf is just a script like sqs but with different syntax and is no more quicker, unlike a function would be?

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #9 on: 17 Jan 2008, 12:50:19 »
Well, yes and nope. Your sqf is a script, yes, but faster than SQS ones. execVM command first precompiles the code and then executes it. Exec command doesnt precompile anything, SQS scripts are interpreted on-the-fly, so SQS are generally slower than SQF. There are situations where your script is basically a loop with a condition check and a sleep of several seconds between an interation and the next one, for these cases you will not notice any performance difference between SQF and SQS.

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #10 on: 17 Jan 2008, 12:56:43 »
Thanks for all the info Mandoble, another thing I forgot to mention is that I added the...unitready... bit after the dotarget because the firing vehicle was destroying the vehicle and then continuing the burst to the next target in essence, "hose piping" all the way over to it. Would this stop it or does unitready only work if the unit is moving?

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #11 on: 22 Jan 2008, 13:57:41 »
Ok ok, here it is, my two scripts for engaging those pesky armoured vehicles........problem is it engages, destroys the target and then fires into the distance. I know it's the last part of the second script somewhere, but I dont know where. Can anyone help out?


Code: [Select]
; detects if any targets nearby are east

; Initiated with [firing vehicle,range to detect out to] exec "findtgt.sqs"

_detector = _this select 0
_range = _this select 1
_v = 0
goto "loop"
hint " starting script "
#start
_v = 0
~10
#loop
_targets = _detector Neartargets 1000
_tgtamnt = count _targets
? (_tgtamnt == 0) : goto "start"
_question = _targets select _v
? east in [_question select 2] : hint " starting script " ; call {[_detector,_range] execvm "target.sqf"} ; exit
_v = _v + 1
? (_v == _tgtamnt) : goto "start"
hint "looping again"
goto "loop"



; called by above script to find closest armoured target and destroy it

private ["_firer","_range","_return","_ammo","_burst","_rangeb","_gunner","_type","_crew","_distance","_targets","_target","_numtgts","_v","_n0","_n1","_tgttype","_tgtcnt","_enunits","_ttype","_tid","_fpos","_typcnt","_distvar","_na","_nb"];
_firer = _this select 0;
_range = _this select 1;
_rangeb = _range;
_gunner = gunner _firer;
_type = [];
_distance = [];
_enunits = [];
_ttype = [];
_tid = [];
_fpos = [];
_distvar = [];
_target = [];
_targets = _firer NearTargets _range;
_numtgts = count _targets;
_return = true;
_v = 0;
_n0 = 0;
_n1 = 1;
_tgttype = ["DSHKM","AGS","D30","BRDM2","UAZMG","UAZ_AGS30","BRDM2_ATGM","BMP2","ZSU","KA50","MI17_MG","SU34B","RHIB","RHIB2Turret"];
_tgtcnt = count _targets;



/*
; SCREEN FOR THREAT
*/

for [{_v = 0},{_v < _tgtcnt},{_v = _v + 1}] do
{
     _enunits = _targets select _v;   
     _ttype = _enunits select 1;
     _tid = _enunits select 4;
     if (_ttype in _tgttype) then {_type = _type + [_tid];};
};



/*
; GET RANGES TO NEWLY ARRAYED UNITS
*/

_typcnt = count _type;
if (_typcnt == 0) then {_return;};_return;
for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _fpos = getpos _firer;
     _ttype = _type select _v;             
     _distance = _fpos distance getpos _ttype;
     _distvar = _distvar + [_distance];
};



/*
; USE DISTANCE ARRAY TO FIND OUT CLOSEST TARGET
*/

for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _ttype = _distvar select _v;
     if (_ttype < _rangeb) then {_rangeb = _ttype};
};


/*
; FIND TARGET IN ENEMY ARRAY THAT MATCHES CLOSEST TARGET RANGE
*/

for [{_v = 0},{_v < _typcnt},{_v = _v + 1}] do
{
     _ttype = _type select _v;
     _tpos = getpos _ttype;
     _distance = _firer distance getpos _ttype;
     if ((_distance < _rangeb + 5) and (_distance > _rangeb - 5)) then {_target = _ttype};
};


/*
; ENGAGE TARGET
*/


_crew = crew _target;




While {alive _target} do
{
     _ammo = _firer ammo "M2";
     _burst = _ammo - 20;
     while {_ammo > _burst} do
     {     
          _gunner dotarget _target;
          WaitUntil{unitready _firer};
          _firer action ["useWeapon", _firer, _gunner, 0];
          _ammo = _firer ammo "M2";
          sleep 0.18;
     };
     sleep 1;
};
_target == objnull;
if !(alive _target) then {hint "target destroyed";[_firer, _range] exec "findtgt.sqs";};

/* supposed to go back to findtgt.sqs here, but instead just fires into the distance*/

much appreciated...thanks.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #12 on: 22 Jan 2008, 16:44:52 »
Only possible place is, obviously:
Code: [Select]
while {_ammo > _burst} do

So, if for some reason _ammo > _burst, it will keep there firing forever.

You might change it by:
Code: [Select]
while {(_ammo > _burst) && alive _target} do

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #13 on: 23 Jan 2008, 11:59:52 »
Awesome, thanks alot Mandoble. That pretty much fixed it. The script had a few minor errors that were easily fixed other than that bit. Learning more all the time because of blokes like yourself, thanks.

Offline Sparticus76

  • Members
  • *
Re: function not working in ArmA
« Reply #14 on: 24 Jan 2008, 03:20:05 »
And just when I think the problems are over, another pops up after some testing. The target is engaged and everything is working beautifully until the target moves over a ridge....and the firer keeps engaging, hitting the ground as if trying to shoot through the hill. Is there anything to say that target is not seen anymore because knowsabout takes a while to come back down from 4 I believe.