Jump to content
HonzaVinCZ

How can I simplify this script?

Recommended Posts

Hello guys, I have done my own weather script but as you can see, I'm not any scripting wizard and I know all these lines must be quite stressful for engine to read but don't know how to simplify the loop to make it more lag free.
Basically this script has three main features: Weather, time acceleration and skip to given time. Weather is changed every ingame half hour and it takes another ingame half hour to change. The ingame half hour is calculated from time acceleration. It's the _delayHalfHourFloored variable.
The _debug function is WIP. 😄

_weather = true;        // Set dynamic weather on/off. Value: true/false. Default: true
_timeMultiplier = 1;    // Set time acceleration. Value: 0.1-120. Default: 1
_timeTrue = true;		// Set scripted mission start time on/off (using skipTime function)
_time = 10;				// Set mission start time. Value in 24 hours format (1-24). Minutes usage example: 18.24 means 18:24. Default: 10
_debug = true;			// Set debug on/off

_delay1Hour = 3600 / _timeMultiplier;
_delay1HourFloored = floor _delay1Hour;
_delayHalfHour = _delay1HourFloored / 2;
_delayHalfHourFloored = floor _delayHalfHour;

skipTime (_time - daytime + 24 ) % 24;

if (_timeTrue) then {
	setTimeMultiplier _timeMultiplier;
};

while {_weather} do 
{
      _daytime = floor daytime;
      _valueOvercast = random [0, 0.3, 1];

      if (_daytime == 0) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 1) then
		{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 2) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 3) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 4) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 5) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 6) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 7) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 8) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 9) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 10) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 11) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 12) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 13) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 14) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 15) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 16) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 17) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 18) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 19) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 20) then
		{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 21) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 22) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};

      if (_daytime == 23) then
      	{
            _delayHalfHourFloored setOvercast _valueOvercast;
            if (_valueOvercast >= 0.5) then
            {
            	_vauleRain = _valueOvercast;
            	_delayHalfHourFloored setRain _vauleRain;
            	if (_vauleRain <= 0.4) then 
            	{
            		_delayHalfHourFloored setFog random [0, 0.2, 0.5];
            	};
            }
            else
            {
            	_delayHalfHourFloored setRain 0;
            };
      	};
sleep _delayHalfHourFloored;
};

 

Share this post


Link to post
Share on other sites

Why do you have multiple _daytime checks with the same code in the then block?

you can just do

if (_daytime == 5 || _daytime == 6 || _daytime == 7) then {...}

or

if (_daytime >=5 && _daytime <= 7) then {...}

 

Also why do you need the delay floored? Why can't you just use the unrounded one?

 

for _debug you can use a macro and #ifdef instead of a local variable. A local variable would always be checked everytime that line comes up, a macro with #ifdef is resolved once when the script is compiled.

47 minutes ago, HonzaVinCZ said:

_vauleRain

You have a typo there.

 

47 minutes ago, HonzaVinCZ said:

while {_weather} do

_weather variable will never change, so why not just while{true} ?

 

48 minutes ago, HonzaVinCZ said:

_timeTrue = true; // Set scripted mission start time on/off (using skipTime function)

But.. Thats not actually what that variable does. Comment doesn't match result.

 

  • Like 4

Share this post


Link to post
Share on other sites

 

30 minutes ago, Dedmen said:

Why do you have multiple _daytime checks with the same code in the then block?

you can just do

if (_daytime == 5 || _daytime == 6 || _daytime == 7) then {...}

or

if (_daytime >=5 && _daytime <= 7) then {...}

As I said, I'm not very experienced and I'm very amateur in scripting. It suprised myself that this script actually works well but had no idea how could I optimise it. Will use that, thank you.
 

30 minutes ago, Dedmen said:

for _debug you can use a macro and #ifdef instead of a local variable. A local variable would always be checked everytime that line comes up, a macro with #ifdef is resolved once when the script is compiled.

Sorry, I have no idea how this works, never worked with that.

 

30 minutes ago, Dedmen said:

Also why do you need the delay floored? Why can't you just use the unrounded one?

Because I was not sure if the value was a bit different due to some lag or something, the "if" condition would then be false. I was working with that it outputs dayTime in HH:MM:SS but just now realised I can put there "HH" condition so I don't need floor condition anymore if I'm right - sometimes I just don't know what I'm doing and just only testing what will work.
 

30 minutes ago, Dedmen said:

_vauleRain
You have a typo there.

Didn't even notice that, thank you.

 

30 minutes ago, Dedmen said:

_weather variable will never change, so why not just while{true} ?

There's _weather = true condition for turning the weather on/off. I don't want to disable whole script in init because there are more functions in one script I can enable or disable.
 

30 minutes ago, Dedmen said:

_timeTrue = true; // Set scripted mission start time on/off (using skipTime function)

But.. Thats not actually what that variable does. Comment doesn't match result.

It does. If it is true, it will enable using of scripted time. If _time = 10, then the script will skip time to make mission start time 10:00. If it is false, the condition will not be executed so there will be default time set in editor on mission start. 

Share this post


Link to post
Share on other sites
21 minutes ago, HonzaVinCZ said:

the "if" condition would then be false

you don't have any if condition on the delay. you only use it as arguments to some script commands.

With delay I meant _delay1HourFloored and _delayHalfHourFloored

 

21 minutes ago, HonzaVinCZ said:

It does. If it is true, it will enable using of scripted time. If _time = 10, then the script will skip time to make mission start time 10:00. If it is false, the condition will not be executed so there will be default time set in editor on mission start. 

No it doesn't. skipTime is always executed. all that _timeTrue influences is whether the timeMultiplier is changed.

 

23 minutes ago, HonzaVinCZ said:

Sorry, I have no idea how this works, never worked with that.

https://community.bistudio.com/wiki/PreProcessor_Commands

Here is a whole wiki page about that.

  • Like 2

Share this post


Link to post
Share on other sites
17 minutes ago, Dedmen said:

you don't have any if condition on the delay. you only use it as arguments to some script commands.

With delay I meant _delay1HourFloored and _delayHalfHourFloored

Oh that... I floored it there just for sure of performance. For example if the calculation results in number like 0,5444444444... . Not sure if it helps somehow. Is the floor useless here? 

 

17 minutes ago, Dedmen said:

No it doesn't. skipTime is always executed. all that _timeTrue influences is whether the timeMultiplier is changed.

You right, I'm just blind. Will move that under _timeTrue condition. Mistaken it with setTimeMultiplier, didn't read it when I was switching between browser and text editor.
 

17 minutes ago, Dedmen said:

https://community.bistudio.com/wiki/PreProcessor_Commands

Here is a whole wiki page about that.

Thank you, will take look on it. 

  • Like 1

Share this post


Link to post
Share on other sites
16 minutes ago, HonzaVinCZ said:

Not sure if it helps somehow. Is the floor useless here?

It won't make any performance difference.

  • Like 1

Share this post


Link to post
Share on other sites

Why not generate all random weather params on the start then set it and let the engine do the work instead of while true this like it is mega important and has to be evaluated constantly

  • Like 4

Share this post


Link to post
Share on other sites

@HonzaVinCZ,

Sil's dynamic weather may be a useful reference. It's super simple.


Here's another example, too:

Have fun!

  • Like 3

Share this post


Link to post
Share on other sites
23 hours ago, killzone_kid said:

Why not generate all random weather params on the start then set it and let the engine do the work instead of while true this like it is mega important and has to be evaluated constantly

I had no idea that there can be dynamic weather generated by Arma engine itself. 

Share this post


Link to post
Share on other sites
19 hours ago, wogz187 said:

@HonzaVinCZ,

Sil's dynamic weather may be a useful reference. It's super simple.


Here's another example, too:

Have fun!

Thank you! I saw scripts like very often used in Exile but there's dynamic weather hard coded to be very often rainy.
Also I wanted to try make dynamic weather script on my own, I wanted to learn something new. 

  • Like 1

Share this post


Link to post
Share on other sites

Sure, as KK said, use the weather menu in editor. You're just writing useless lines.

A while {something true} do {if (this time) then {same code } 24 times} is just a waste of code.

First of all dayTime depends on time multiplier, so there is no need for your delay stuff. Btw, also follow agentRev remark in setOvercast command:

(1800 * timeMultiplier) setOvercast _value;

Furthermore your time multiplier is never updated in your loop! So, useless.

 

So, if you want to change weather on each hour, spawn :

while {_weather} do {
  private _valueOvercast = random [0, 0.3, 1];
  (1800 * timeMultiplier) setOvercast _valueOvercast;
  if (_valueOvercast >= 0.5) then {
    _vauleRain = _valueOvercast;
    (1800 * timeMultiplier) setRain _vauleRain;
    if (_vauleRain <= 0.4) then {
      (1800 * timeMultiplier) setFog random [0, 0.2, 0.5];
    };
  } else {
    (1800 * timeMultiplier) setRain 0;
  };
  uiSleep 3600;
};

 

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites
On 11/7/2019 at 9:04 PM, pierremgi said:

while {_weather} do { private _valueOvercast = random [0, 0.3, 1]; (1800 * timeMultiplier) setOvercast _valueOvercast; if (_valueOvercast >= 0.5) then { _vauleRain = _valueOvercast; (1800 * timeMultiplier) setRain _vauleRain; if (_vauleRain <= 0.4) then { (1800 * timeMultiplier) setFog random [0, 0.2, 0.5]; }; } else { (1800 * timeMultiplier) setRain 0; }; uiSleep 3600; };

Will this work on a dedicated server executed from initServer.sqf? Also sync with JIP?

Why uiSleep?

while {true} do {
	private _valueOvercast = random [0,0.3,1];
	(900 * timeMultiplier) setOvercast _valueOvercast;
if (_valueOvercast >= 0.5) then {
	_valueRain = _valueOvercast;
	(900 * timeMultiplier) setRain _valueRain;
if (_valueRain <=4) then {
	(900 * timeMultiplier) setFog random [0,0.2,0.5];
	};
} else {
	(900 * timeMultiplier) setRain 0;
	};
sleep 1800;
};

 

Share this post


Link to post
Share on other sites

Your time multiplier comes from setTimeMultiplier (which is a server command) or from mission parameters (class params in description.ext then BIS_fnc_getParamValue )

In the above code, you randomize the overcast (setOverCast is local) but you don't want to get different values for clients!

setRain & setFog are  server commands...

So, definitely, yes, the code is to be run on server (dedicated or not). Then, the engine sync will do the work for clients (JIP or not).

uiSleep is more accurate/precise than sleep, in Multiplayer. I'm no more using sleep, except if, in single player, I need a code to be hanged during any pause (not so much).

Share this post


Link to post
Share on other sites
On 11/7/2019 at 9:04 PM, pierremgi said:

while {_weather} do { private _valueOvercast = random [0, 0.3, 1]; (1800 * timeMultiplier) setOvercast _valueOvercast; if (_valueOvercast >= 0.5) then { _vauleRain = _valueOvercast; (1800 * timeMultiplier) setRain _vauleRain; if (_vauleRain <= 0.4) then { (1800 * timeMultiplier) setFog random [0, 0.2, 0.5]; }; } else { (1800 * timeMultiplier) setRain 0; }; uiSleep 3600; };

How does this work if the timeMultiplier is set to example 12? Do i need to lower the number to achieve a transition of 1800 seconds?


timeMultiplier = 12;
1800 * timeMultiplier = 21600

150 * timeMultiplier = 1800

Share this post


Link to post
Share on other sites
1 hour ago, Robustcolor said:

How does this work if the timeMultiplier is set to example 12? Do i need to lower the number to achieve a transition of 1800 seconds?


timeMultiplier = 12;
1800 * timeMultiplier = 21600

150 * timeMultiplier = 1800

 

if you need to spend 1800 for a transition in real life, you need to set  the code at 1800 * timeMultiplier  because time in game is accelerated (by 12 in your case).

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

×