Advertisement

Author Topic: function not working in ArmA  (Read 1695 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   ---   FWW2 - Finland At War 1939-1945
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.

Offline Sick

  • Members
  • *
    • Dev-Heaven.net
Re: function not working in ArmA
« Reply #15 on: 24 Jan 2008, 15:23:10 »
Quote
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.
Why do you believe this Mandoble?
There is afaik no reason at all why you can not use sleep or long while's within a call.

There are places where you can not use sleep and that is:
  • Directly from .... Lines in Editor (Init, onActivation, onDeActivation, Condition)
  • Directly from eventHandlers

In these cases, you must use a spawn, exec or execVM if you wish to use sleeps (within call).
In any other case, functions called from a spawned, exec or execVM'ed script, all can include sleep.
However, sleep inside functions is not a common practice, as under usual circumstances one uses functions to make a calculation or for repeated code.
« Last Edit: 24 Jan 2008, 15:33:17 by Sick »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #16 on: 24 Jan 2008, 15:56:32 »
There is afaik no reason at all why you can not use sleep or long while's within a call.

As long as the following works for you (remember we were talking about functions):
Code: [Select]
// MyTest.sqf
while {true} do
{
   Sleep 1;
};

Code: [Select]
// init.sqf
MyTest = compile preprocessFileLineNumbers "MyTest.sqf";

[] call MyTest;

Offline Sick

  • Members
  • *
    • Dev-Heaven.net
Re: function not working in ArmA
« Reply #17 on: 24 Jan 2008, 15:59:22 »
Works 100% without any doubt, as long as the [] call MyTest; (which could just as easily've been just: call MyTest) is located in init.sqf, or any other script that was either exec, execVM, or spawned.





#removed unnecessary quote of previous post
« Last Edit: 24 Jan 2008, 17:08:30 by bedges »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #18 on: 24 Jan 2008, 16:09:39 »
There is an important limitation, call will not return at all and any code following that call in that init.sqf will not be executed.

"Unlike scripts, functions halt all other game engine processes until the function has completed its instructions. This means functions run faster than scripts, and the result of functions is immediate and unambiguous. It can also mean that if a function takes too long to run it will have an adverse effect on game play - large functions or CPU intensive functions can cause the game to seize up until it completes. When creating a functions you want the function to be short and sweet to achieve the best results." This is what BIKI says about functions. May be the effect of intense and continuous loops in functions are not as adverse as the BIKI indicates, havent tested that.

Offline Sick

  • Members
  • *
    • Dev-Heaven.net
Re: function not working in ArmA
« Reply #19 on: 24 Jan 2008, 16:13:26 »
Well, your statement was brute and simple: You can not use sleep or long while's inside call's
I claimed it was untrue :)

The fact that the script will halt in your example and not continue processing after the call MyTest is a natural and wanted feature in this case,
simply because you made MyTest a while loop that never ends.

It all comes down to what you want it to do.

At times I use conditions to pick the appropriate code, which get loaded in a variable, you just call the variable, and it doesn't matter if there is sleep, waitUntil, or long while's inside :)

I also use sleep for functions that take a long time to complete and are very intensive. We just give the engine some more breathing space by using sleeps, just as we would in scripts.


The danger of putting the engine on it's knees is always there with SQF, and depending on the script, also for SQS. Not just for functions.
It all comes down to properly dividing the load. Any SQF script or function can lock your game up completely, if the load is too heavy, or very quick loops without sleeps, and with a lot of commands can kill it easily.
« Last Edit: 24 Jan 2008, 16:26:46 by Sick »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #20 on: 24 Jan 2008, 16:38:09 »
This is a different matter. What I havent tested, and what BIKI claims (which might be wrong) is that the call stops the execution of anything else (halt all other game engine processes) until it returns, not only the execution of the calling script. If that claim is true then if I have 10 scripts running and I call a function which requires 4 seconds to return, the 10 scripts would be absolutely halted for 4 seconds, and not only these, but also anyother game engine processes. If I have a calling function with an infinite while, some process and sleep, then eveyother process would be halted forever in the game. If this is not the case, which would be great btw, then the BIKI would need some serious fixes.

Offline Sick

  • Members
  • *
    • Dev-Heaven.net
Re: function not working in ArmA
« Reply #21 on: 24 Jan 2008, 16:47:22 »
Then we better steam up our wiki skills because I believe the wiki is wrong.

I think what they tried to explain is this:

Example 1, init.sqf
Code: [Select]
test = { player sideChat "wee"; sleep 10 };
spawn test;
player sideChat "wee2";

Example 2, init.sqf
Code: [Select]
test = { player sideChat "wee"; sleep 10 };
call test;
player sideChat "wee2";


In example 1, the player will read in sidechat "wee" and immediatly followed "wee2" (or possibly the other way around, because the spawned code has a slight delay)
In example 2, the player will read in sidechat "wee" and 10 seconds later "wee2"


In example1, the script continues processing immediatly after the spawn, because you spawn the test code in another "thread"
In example2, the calling script executes and waits for the call to complete, before it continues processing within the file, exactly the same as if there really was written in this script: player sideChat "wee"; sleep 10;

Basicly, the below is exactly the same as Example2, except that you don't use the test variable to store the code:
Code: [Select]
player sideChat "wee";
sleep 10;
player sideChat "wee2";
(except that in the original Example2, test { } block is another scope)
« Last Edit: 24 Jan 2008, 16:58:14 by Sick »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: function not working in ArmA
« Reply #22 on: 24 Jan 2008, 17:08:03 »
Sick, this is a quite interesting topic, while it is a bit offtopic into this particulat topic. To do not let it die, you might open an individual topic to discuse call, spawn and execVM advantages and disadvantages and risks, as well as possible corrections to the BIKI which might apply also to the current OFPEC Comref.

Now back on topic:

Sparticus, I understand _firer is the vehicle which is firing at the target, and it is armed with a single "M2".
Didnt check it, but the line of sight check added here should work.

Code: [Select]
private[....,....,...,...., "_los", "_losok", "_posasl", "_distance", "_wdir"];
.....
.....
While {alive _target} do
{
     _ammo = _firer ammo "M2";
     _burst = _ammo - 20;
     _los = "logic" createVehicleLocal [0,0,0];
     while {(_ammo > _burst) && (alive _target)} do
     {     
          _gunner dotarget _target;
          Sleep 1;
          WaitUntil{unitready _gunner};
          _distance = _firer distance _target;
          _losok = true;
          _posasl = [getPosASL _firer select 0, getPosASL _firer select 1, (getPosASL _firer select 2) + 2];
          _wdir = _firer weaponDirection "M2";
          for [{_i=0},{(_i < _distance) && (_losok)},{_i = _i + 10}] do
          {
             _los setPosASL [(_posasl select 0)+(_wdir select 0)*_i,(_posasl select 1)+(_wdir select 1)*_i,(_posasl select 2)+(_wdir select 2)*_i];
             if ((getPos _los select 2) < 0.1) then
             {
                _losok = false; 
             };
          };
         
          if (_losok) then
          {
             _firer action ["useWeapon", _firer, _gunner, 0];
             _ammo = _firer ammo "M2";
          };
          sleep 0.18;
     };
     deleteVehicle _los;
     sleep 1;
};

« Last Edit: 24 Jan 2008, 17:31:14 by Mandoble »

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: function not working in ArmA
« Reply #23 on: 24 Jan 2008, 17:08:34 »
[aside]There was a long thread about this on the BIS forums some time ago. I believe the conclusion was that sleep and waitUntil do work in functions that are themselves called from sqf scripts. I can not remember whether using spawn or execVM made any difference. In terms of good coding practice, if a function takes too long to exit then it is probably time to refactor your code.[/aside]
« Last Edit: 25 Jan 2008, 14:56:02 by Mr.Peanut »
urp!

Offline Sick

  • Members
  • *
    • Dev-Heaven.net
Re: function not working in ArmA
« Reply #24 on: 24 Jan 2008, 17:52:43 »
I will think about creating another thread. For now I just noticed some things that I disagreed on and wanted to share my opinion and view about :)

[aside2]I believe this is simply depending on situation and wishes of the coder. As call can be used to execute code, the executed code does not necessairly has to be a function.[/aside2]




#removed unnecessary quote of previous post
« Last Edit: 24 Jan 2008, 18:29:56 by bedges »