GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2016-09-21 19:20:03

KurtBlissZ
Member
Registered: 2016-09-21
Posts: 4

wrap(val, min, max)

I've ran into issues before where I needed to check my own direction value to tell what way the player was facing. But since I used my own direction variable it wouldn't keep in between 0 and 359 like the built in variable would. So I thought this would make a good script. 

Expand/// wrap(val,min,max);
//
//  Returns the value warped in the given range. 
//  Give the value you want to wrap, 
//  and the minimal and maximum values you want to keep your value in
//  
//      val     value of the varible your setting   , real
//      min     the lowest value                    , real
//      max     the greatest value                  , real
//
//  Use your own direction value and keep between 0 and 359
//  ex: dir = wrap(dir, 0, 359);
//
/// GMLscripts.com/license
{
    var _val   = argument0;
    var _min   = argument1;
    var _max   = argument2;
    var _range = _max - _min;
    
    // Keep adding or subtracting range to the value until its wraped.
    while (_val < _min) _val += _range;
    while (_val > _max) _val -= _range; 
    
    // Return the value.
    return(_val);
}

Offline

#2 2016-09-26 02:58:35

Juju
Member
Registered: 2015-10-01
Posts: 45

Re: wrap(val, min, max)

Expand///wrap( value, minimum, maximum )
var _mod = ( argument0 - argument1 ) mod ( argument2 - argument1 );
if ( _mod < 0 ) return _mod + argument2 else return _mod + argument1;

Last edited by Juju (2016-09-26 02:58:59)

Offline

#3 2016-10-30 16:50:07

KurtBlissZ
Member
Registered: 2016-09-21
Posts: 4

Re: wrap(val, min, max)

Thanks Juju, I've been using your script now. it is faster when I used get_timer to compare the two.

Offline

Board footer

Powered by FluxBB