samspade
Member
Map Script Tutorial
GM Version: All
Target Platform: All
Download: NA
Links: NA
Summary:
Map re-maps a number from one number range to another. It works with positive and negative numbers and outside of its given range.
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 script map() 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,
Example Uses:
Code
Expanded Version:
Single line version (thanks to frostycat):
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: All
Target Platform: All
Download: NA
Links: NA
Summary:
Map re-maps a number from one number range to another. It works with positive and negative numbers and outside of its given range.
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 script map() 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
Example Uses:
- 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
Expanded Version:
Code:
/// @description Maps a number from one range to another
/// @param value
/// @param current_lower_bound
/// @param current_upper_bound
/// @param desired_lower_bound
/// @param desired_upper_bound
var value = argument0;
var current_lower_bound = argument1;
var current_upper_bound = argument2;
var desired_lower_bound = argument3;
var desired_upper_bound = argument4;
var total = current_upper_bound - current_lower_bound;
var percent = (value - current_lower_bound) / total;
var new_val = lerp(desired_lower_bound, desired_upper_bound, percent);
return new_val;
Code:
/// @description Maps a number from one range to another
/// @param value
/// @param current_lower_bound
/// @param current_upper_bound
/// @param desired_lower_bound
/// @param desired_upper_bound
return (((argument0 - argument1) / (argument2 - argument1)) * (argument4 - argument3)) + argument3;
Code:
//draw a horizontal rectangle health bar where x1, y1, x2, and y2 are its coordinates on screen
var draw_hp_at = map(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(x, slider_start, slider_end, 0, 1);
Coding Math:
Last edited: