GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2024-02-15 15:37:16

maras
Member
Registered: 2021-04-25
Posts: 28
Website

scale_val

A tiny situational script to scale a value from one range to another

Expand/// scale_val(val, in_min, in_max, out_min, out_max) 
//
//  Scale a value from one range to another
//
/// GMLscripts.com/license

function scale_val(val, in_min, in_max, out_min, out_max) {
	
	if (in_min == in_max) return out_min;

	var in_len = in_max - in_min
	var out_len = out_max - out_min;
	
	var val0 = clamp(val, in_min, in_max) - in_min;
	var in_percent = abs(val0 / in_len);
	
	return in_percent * out_len + out_min;
}

Explaination:

Expandscale_val(75, 50, 100, -10, 10) returns: 0
75 is 50% between 50 and 100 therefore 50% between -10 and 10 is 0

scale_val(25, -25, 25, 0, 100) returns: 100
scale_val(-25, -25, 25, 0, 100) returns: 0

Last edited by maras (2024-02-15 15:54:01)


I'm on the official GM discord > maras_cz

Offline

#2 2024-04-20 03:50:56

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: scale_val

Hi, maras. Thanks for suggesting this. We have a script similar to this already.

https://www.gmlscripts.com/script/map_range


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB