Home   Help Search Login Register  

Author Topic: While alive and distance check  (Read 1652 times)

0 Members and 1 Guest are viewing this topic.

Offline Turk

  • Members
  • *
While alive and distance check
« on: 24 Oct 2008, 13:24:41 »
Hello,

Can you take a look at this code and tell me where I'm going wrong...

Code: [Select]
while {alive soldier} do
{
 if ((soldier distance (Tank) < 200)) then

  {
 
 sleep 0.2 + random 0.3;
 Null = [Tank] execVM "Fire.sqf";
   
  };
exitwith
};


Basically when "Soldier" enters a trigger the trigger calls the above script and within this script another script is called, the idea is if "Soldier" is alive and within 200 meters of "Tank" the script keeps calling on the fire script and if "Soldier" dies or moves 200 meters away from "Tank" then the script stops calling on the fire script.

Well, it works just how I want it to work except for the fact that once the "Soldier" moves 200 meters away the game takes a massive FPS hit and really slows down. Its fine if the "Soldier" dies, just if he moves away, its as if the script is caught in a endless loop and wont exit.

I've tried adding a large sleep command just before the Exitwith which seems to work but then it slows the rate at which the Fire.Sqf script is called and totally messes the fire effect up. 

Any ideas please?

Turk. 

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: While alive and distance check
« Reply #1 on: 24 Oct 2008, 13:31:18 »
That's probably because you're using exitWith wrong. In essence: just remove exitwith and it should work out fine!

Exitwith {} is a command used in .sqf to get out of the current scope - in this case the while...do loop. Since the while...do loop terminates when the soldier is dead anyway, exitwith is quite redundant.

A proper usage of exitWith (although quite redundant) would've been something like:

Code: [Select]
while {true} do
{
 if ((soldier distance (Tank) < 200)) then

  {
 
 sleep 0.2 + random 0.3;
 Null = [Tank] execVM "Fire.sqf";
   
  };
if (!alive soldier) exitwith {hint "soldier dead, quitting endless loop"}
};

But just remove the exitwith and it should be fine :) unless of course there's more script below that we haven't been shown yet?

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline Turk

  • Members
  • *
Re: While alive and distance check
« Reply #2 on: 24 Oct 2008, 15:40:23 »
Hi,

Thanks for your prompt reply Wolfrug.

That's it for the script there's no more to it. Unfortunately removing the Exitwith just yields the same result, the FPS takes a massive hit. However, the code you inserted into this script did help me out in solving the problem.

Code: [Select]
while {true} do
{
 if ((soldier distance (Tank) < 200)) then

  {
 
 sleep 0.2 + random 0.3;
 Null = [Tank] execVM "Fire.sqf";
   
  };
if ((soldier distance (Tank) > 200)) exitwith {};
};

Doing it like this works great and there's no FPS loss at all, I tried without the "{}" and also without the "Exitwith", but I either got errors or a loss of FPS. I don't know if this code is a crude way of doing it but it seems to work.

Cheers mate.

Turk.

EDIT... I've just done some more testing and I'm still getting a big hit on FPS, funny it seemed to work the first time I tried it. I'll play some more.!
« Last Edit: 24 Oct 2008, 16:16:49 by Turk »

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: While alive and distance check
« Reply #3 on: 24 Oct 2008, 16:16:43 »
I trimmed down your syntax somewhat (yours isn't wrong, it is just a bit convoluted):
Code: [Select]
while { ((soldier distance Tank) < 200) } do
{  
    [Tank] execVM "Fire.sqf";
    sleep 0.2 + random 0.3;
};

I hope this makes things a bit clearer (note that you don't need the "null =" inside scripts; only in editor).

This script won't have a noticeable effect on FPS, but Fire.sqf might!
« Last Edit: 24 Oct 2008, 16:21:51 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: While alive and distance check
« Reply #4 on: 24 Oct 2008, 16:19:50 »
Actually now that I look at it again the problem is that there's no sleep in the actual loop! If the if...then is not true, then the loop looks like this:

Code: [Select]
while {alive soldier} do {};
Which is obviously no good. So you need to put in a short sleep in that loop as well, something like 0.2 seconds or less. :) Then whenever the soldier is further away than 200 meters, the loop shouldn't go all hacky.

Code: [Select]
while {alive soldier} do
{
 if ((soldier distance (Tank) < 200)) then

  {
 
 sleep 0.2 + random 0.3;
 Null = [Tank] execVM "Fire.sqf";
   
  };
sleep 0.2;
};

And this works as you want it to work, since in the example you provided, if the soldier enters into the 200 meters radius again he'd need to restart the script from the top (since the exitwith would've quitted it).  :good:

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: While alive and distance check
« Reply #5 on: 24 Oct 2008, 16:23:27 »
Crap, you are quite right. I completely forgot about the alive soldier bit. Duh!

Need to think before I post *sighs*
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Turk

  • Members
  • *
Re: While alive and distance check
« Reply #6 on: 24 Oct 2008, 17:15:19 »
Ok thanks lads.

I was so near yet so far with my original code and I spent some hours on it last night trying to get it to work!

Wolfrug, that sleep does the trick cheers. Spooner, with your code I added 'if (!alive soldier) exitwith {};' after the sleep commands and that works too, cheers. 

Turk.