Home   Help Search Login Register  

Author Topic: Faster script wanted. Is it possible to "pump this script up" ?  (Read 2373 times)

0 Members and 1 Guest are viewing this topic.

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
A script that is called by:

pull = player addAction ["Pull Ammo Box", "pulling.sqf"]

pulling.sqf

Code: [Select]
player removeAction pull;
letgo = player addaction ["Drop Ammo Box", "letgo.sqs"];

_realbox = nearestObject [player, "specialBoxWest"];
_realboxpos = getpos _realbox;
_realbox setpos [_realboxpos select 0, _realboxpos select 1, - 10];

dropping = false;

_box = "specialBoxWest" createVehicleLocal _realboxpos;
_box setpos _realboxpos;
clearWeaponCargo _box;
clearMagazineCargo _box;

_xa = 0.5;
_ya = 1;
_za = 0.6;

while { (! (dropping)) AND (alive player) AND (_box distance player <= 3) AND (speed player < 15) AND (vehicle player == player) } do
{
_box setdir getdir player;
_box setpos [(getpos player select 0) +((((_xa^2)+(_ya^2))^0.5) * sin (getdir player)), (getpos player select 1) +((((_xa^2)+(_ya^2))^0.5) * cos  (getdir player)), (getpos player select 2) +_z];
sleep 0.001;
};

deletevehicle _box; _realbox setpos [(getpos player select 0) +((((_xa^2)+(_ya^2))^0.5) * sin (getdir player)), (getpos player select 1) +((((_xa^2)+(_ya^2))^0.5) * cos  (getdir player)), (getpos player select 2) +_z]; _realbox setdir getdir player; player removeaction letgo;

It works welll, part from when the ammo boxes are trading places, there is a slight delay.
In the first switch the boxes overlap eachother for 1-2 seconds, in the second switch there is NO box for 1-2 seconds. This only when you are a client in MP of course  :P

Can that be done faster ?

Laggy

EDIT: This seems to work much better:

Code: [Select]
player removeAction pull;
letgo = player addaction ["Drop Ammo Box", "letgo.sqs"];

_realbox = nearestObject [player, "specialBoxWest"];
_realboxpos = getpos _realbox;
_realbox setpos [_realboxpos select 0, _realboxpos select 1, - 20];

dropping = false;

_box = "specialBoxWest" createVehicleLocal _realboxpos;
_box setpos [_realboxpos select 0, _realboxpos select 1, - 10];
clearWeaponCargo _box;
clearMagazineCargo _box;

waitUntil {_realbox distance player > 5};

_box setpos _realboxpos;

_xa = 0.5;
_ya = 1;
_za = 0.6;

while { (! (dropping)) AND (alive player) AND (_box distance player <= 3) AND (speed player < 15) AND (vehicle player == player) } do
{
_box setdir getdir player;
_box setpos [(getpos player select 0) +((((_xa^2)+(_ya^2))^0.5) * sin (getdir player)), (getpos player select 1) +((((_xa^2)+(_ya^2))^0.5) * cos  (getdir player)), (getpos player select 2) +_z];
sleep 0.001;
};

player removeaction letgo; _realbox setpos [(getpos player select 0) +((((_xa^2)+(_ya^2))^0.5) * sin (getdir player)), (getpos player select 1) +((((_xa^2)+(_ya^2))^0.5) * cos  (getdir player)), (getpos player select 2) +_z]; _realbox setdir getdir player; waitUntil {_realbox distance player < 5}; deletevehicle _box;
« Last Edit: 27 Mar 2009, 00:03:10 by laggy »
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Walter_E_Kurtz

  • Guest
If you've settled on values for _xa, _ya and _za, you could do the the calculations and plug in the result.

(((_xa^2)+(_ya^2))^0.5)    =  1.118
_za = 0.6

Thus:
_box setpos [(getpos player select 0) + (1.118 * sin (getdir player)), (getpos player select 1) + (1.118 * cos  (getdir player)), (getpos player select 2) + 0.6];

NB. In your versions you had _z rather than the _za I assume you meant.


And, at the end, why are you calculating the position again? Try:
player removeaction letgo; _realbox setpos getPos _box; _realbox setdir getdir player; waitUntil {_realbox distance player < 5}; deletevehicle _box;

Offline Sparticus76

  • Members
  • *
Just on a bit of a side note..or maybe not, your sleep is 0.001, that's 1000 iterations per second I believe. Is that necessary? The human eye cant recognise anything over 25 frames per second, and I've never seen a Frap framerate more than 75 or so, so could 0.01 (which is 100 iterations/frames per second) be a better way to do this?

Also, why do you have to put the original box underground and then create a new box to drag around, then setpos the original box in the new boxes spot? You cant just drag the original? And if not, why?
« Last Edit: 27 Mar 2009, 03:27:08 by Sparticus76 »

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
sleep actually means "wait for at least this amount of time". In no way does it guarantee that it will wait for exactly that amount of time. The earliest that the command can finish and allow the next command to run is on the next frame, so the absolute most you can run a loop with a sleep 0.001 in is one iteration per frame.

However, as I said, sleep says "wait for at least this amount of time" which does not guarantee synchronisation with the graphics frame. Because of this, your jerkiness certainly has nothing to do with the amount of processing that your loop is performing, which is actually minimal; you don't actually need it to be faster, but you do need it to be synchronised. To ensure perfect synchronisation, you should use waitUntil, which does guarantee to always iterate exactly once every frame (and you'll also notice that I avoided messing about with maths by using modelToWorld):
Code: [Select]
waitUntil
{
   if (! (dropping)) AND (alive player) AND (_box distance player <= 3) AND (speed player < 15) AND (vehicle player == player) then
   {
_box setdir getdir player;
_box setpos (player modelToWorld [0, 1.118, 0.6]);
false; // Don't break the waitUntil loop.
   }
   else
   {
true; // Break out of the waitUntil loop.
   };
};
« Last Edit: 27 Mar 2009, 05:50:46 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
Thanks all,

Also, why do you have to put the original box underground and then create a new box to drag around, then setpos the original box in the new boxes spot? You cant just drag the original? And if not, why?

This is why:

http://www.ofpec.com/forum/index.php?topic=33193.0
« Last Edit: 27 Mar 2009, 10:42:52 by laggy »
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline Sparticus76

  • Members
  • *
Ah ok, so if you pull the original around, it's sever based and looks all jittery, however if you createlocal the thing, it's local and moves smoothly. Thanks.