samspade
Member
map_value Function Tutorial
GM Version: 2.3+
Target Platform: All
Download: NA
Links: Video Explanation
Summary:
map_value re-maps a number from one number range to another. It works with positive and negative numbers and outside of its given range updated for GameMaker 2.3+.
Tutorial:
It is often very useful to be able to take a number from one range and find its equivalent in another range. That is what the function map_value() does. It takes five arguments, a value, the current min and max range and the destination or desired min and max range. It then takes the relation of that value to the first range and figures out what the number should be in relation to the second range and returns that number. For example,
Code
Example Use
If you would like a more detailed understanding of the math behind it, watch the following three videos on YouTube (not by me).
Coding Math:
GM Version: 2.3+
Target Platform: All
Download: NA
Links: Video Explanation
Summary:
map_value re-maps a number from one number range to another. It works with positive and negative numbers and outside of its given range updated for GameMaker 2.3+.
Tutorial:
It is often very useful to be able to take a number from one range and find its equivalent in another range. That is what the function map_value() does. It takes five arguments, a value, the current min and max range and the destination or desired min and max range. It then takes the relation of that value to the first range and figures out what the number should be in relation to the second range and returns that number. For example,
- 15 in range 0...10 becomes 150 in range 0...100
- -5 in range -10...0 becomes 50 in range 0...100
- 5 in range 0...10 becomes 0 in range -5...5
- -5 in range 0...-10 becomes 250 in range 200...300
- 5 in range 0...10 becomes -5 in range 0...-10
- drawing health bars to the screen (where health is the value, 0 and max health is the current range and the position for the start and end of the health bar is the desired min and max range)
- sliders (turn the value of any slider into any other range)
- functions or formulas that return a range (such as sin, cos, or gamepad_axis_value)
- relative value of one thing to another (such as scaling damage within a range based on max health or scaling an image based upon another value)
Code
GML:
//map a value from one range to another
function map_value(_value, _current_lower_bound, _current_upper_bound, _desired_lowered_bound, _desired_upper_bound) {
return (((_value - _current_lower_bound) / (_current_upper_bound - _current_lower_bound)) * (_desired_upper_bound - _desired_lowered_bound)) + _desired_lowered_bound;
}
GML:
//draw a horizontal rectangle health bar where x1, y1, x2, and y2 are its coordinates on screen
var draw_hp_at = map_value(current_hp, 0, max_hp, x1, x2);
draw_set_color(c_red);
draw_rectangle(x1, y1, draw_hp_at , y2, false);
draw_set_color(c_black);
draw_rectangle(x1, y1, x2, y2, true);
//translate a sliders value to 0-1 assuming a slider where
//x represents the sliders position and slider_start and slider_end represent the min and max x values
global.slider_value = map_value(x, slider_start, slider_end, 0, 1);
Coding Math:
Last edited: