Jump to content
Lorenz94

Return value problem

Recommended Posts

Hi,

 

I'm trying to write a simple function to choose a random date to start my mission with, but I'm a bit confused about obtaining return values.

 

The function aim is to choose the correct amount of days based on the random calculated month, but later in the script that calls this function I get an error stating that the variable _day is undefined.

 

Spoiler

_year = 2018 + random 22;
_bissextile = ["2020", "2024", "2028", "2032", "2036", "2040"];

_month = random 12;
_month30 = ["4", "6", "9", "11"];
_month31 = ["1", "3", "5", "7", "8", "10","12"];

BRZ_fnc_setCorrectDay = {
  if (_year in _bissextile) then {
      if (_month == 2) then {
        _day = random 29;
      };
      if (_month in _month30) then {
          _day = random 30;
      };
      if (_month in _month31) then {
          _day = random 31;
      };
      _day

    } else {

    if (_month == 2) then {
      _day = random 28;
    };
    if (_month in _month30) then {
        _day = random 30;
    };
    if (_month in _month31) then {
        _day = random 31;
    };
    _day
  };
};

[_year, _bissextile, _month, _month30, _month31] call BRZ_fnc_setCorrectDay; //Also tried []

_hour = random 24;
_minute = random 59;

setDate [_year, _month, _day, _hour, _minute];

 

Thanks!

Share this post


Link to post
Share on other sites

Scope issue,

_day only exists inside those if then statements.

Declare a default value for _day on top of your script.

 

Cheers

Share this post


Link to post
Share on other sites
19 minutes ago, Grumpy Old Man said:

Scope issue,

_day only exists inside those if then statements.

Declare a default value for _day on top of your script.

 

Cheers

I tried to add _day = 15 before _month, but then the mission starts always on the 15th day!

 

Even if I set it to 0 the number is "floored" to 1, so the mission starts on the 1st of the month.

 

I'm sure this is a stupid thing, but I seriously don't get it :/

 

Thank you.

Share this post


Link to post
Share on other sites

Think this might be simpler:

setDate (numberToDate [2018 + (floor random 23), random 1]);

Also, in your code it is extremely unlikely that random 22 will be a whole number, thus it will never match in your _bissextile. In fact none of your randoms will.

 

 

  • Like 3

Share this post


Link to post
Share on other sites
_year = 2018 + floor random 22;
_month = ceil random 12;

//Days in each month
_monthDays = [ 31, [ 28, 29 ], 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

//Select number of days in month
_days = _monthDays select ( _month - 1 );

//If february
if ( _month isEqualTo 2 ) then {
	//If year mod 4 is 0 then it is a leap year
	_isLeapYear = ( _year % 4 ) isEqualTo 0;
	//select number of days based of leap year
	_days = _days select _isLeapYear;
};

//get random day
_day = ceil random _days;

_hour = floor random 24;
_minute = floor random 60;

setDate [_year, _month, _day, _hour, _minute];

 

  • Like 1

Share this post


Link to post
Share on other sites

What about:

(numberToDate [2018,random 22]) call bis_fnc_fixDate

?

Share this post


Link to post
Share on other sites

Thank you all. I wanted something really random, that's because I tried to write something instead of using provided commands!

 

I'll try the solution submitted by Larrow!

 

Thank you again guys.

Share this post


Link to post
Share on other sites

Although you won't encounter it in this case (since 2018 - 2040), it is worth noting to any future readers that leap years (in the Gregorian calendar) are years which are multiples of four (with the exception of years divisible by 100 but not by 400):

_isLeapYear = (( _year % 4 ) isEqualTo 0) && {!(( _year % 100 ) isEqualTo 0) || {( _year % 400 ) isEqualTo 0}};

 

  • Like 1

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

×