GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Re: Script Submission » Draw point of specified size » 2021-02-16 09:39:20

Issue with this is that depending of GPU (Intel/GeForce/AMD) rectangle might be offset by 1px, and when you use odd number, you have no idea on which pixel it will be rounded in this case. So, the result might be even ~2px wrong.

#2 Community » GMS 2.3.1 compatibility ? » 2020-09-08 16:29:25

gnysek
Replies: 3

Hi @xot, are you planning to update website, so people can choose between displaying scripts in 2.3 format (by default; function + JSDOC) and "legacy" (GM8 - 2.2.x) as alternative (maybe using tabs ?).

#3 Re: Script Submission » Get the diagonal screen size in inches » 2020-09-01 05:12:48

Of course not. I was rather thinking about covering all possibilities, but then script will be much bigger.

#4 Re: Script Submission » Get the diagonal screen size in inches » 2020-08-31 17:24:33

This returns size of first screen attached to current device in fact, as to get current screen you need to use combination of window_get_visible_rects + window_get_x/y to find on which screen game runs (except fullscreen - it's always on screen 1 from what I remember).

#5 Re: Script Submission » display_get_monitors_number » 2020-08-18 18:27:14

I just discovered this function 15 minutes before I made this post, when searching how to detect two monitors. And it was mentioned somewhere on YYG community between words by someone, but totally overlooked by others...

#6 Script Submission » display_get_monitors_number » 2020-08-14 17:38:54

gnysek
Replies: 2

pre-2.3

Expandreturn array_length_1d(window_get_visible_rects(0,0,1,1))/8;

2.3+

Expandreturn array_length(window_get_visible_rects(0,0,1,1))/8;

This returns the number of displays/monitors/screens on current PC.

#7 Re: Script Submission » Simple Debug Message Script » 2019-02-01 11:08:34

Expands+= (i > 0 ? ", " : "") + (is_real(argument[i]) ? string(argument[i]) : ("\"" + string(argument[i]) + "\""));

Then you can differ number from string, may be helpful in some debug cases.

#8 Re: Script Submission » Simple Debug Message Script » 2018-11-05 10:29:20

In gms2 you can do

Expands+= (i > 0 ", " : "") + string(argument[i]);

which make it even more beautiful smile

#9 Script Submission » path_get_nearest_pos » 2018-02-09 07:56:47

gnysek
Replies: 0

This function returns percentage position of path, nearest to given point (may be mouse_x/mouse_y for example).

Precision is between how many pixels it should check (for image below and precision = 1 it will be 1800 checks for example, as it checks each 1 pixel, and 180 checks if precision = 10).

This could be probably done by binary search, but since path lines may overlay on each other, I found no solution to make it good (it would work then only on convex paths probably).

Expand	// @param path
	// @param x
	// @param y
	// @param pixel_precision
	
	var dst = 9999;
	var pos = 0;
	var _precision = 1/path_get_length(argument0) * clamp(argument3, 1, 100);
	
	for(var i = 0; i < 1; i += _precision;) {
		var _dst = point_distance(
			argument1, argument2,
			path_get_x(0, i),
			path_get_y(0, i)
		);
		
		if _dst < dst {
			pos = i;
			dst = _dst;
		}
	}
	
	return pos;

you can use later pos value with functions path_get_x/path_get_y to get x/y coordinates on path of closests points.

#10 Script Submission » path_get_smooth_points » 2018-02-09 07:51:19

gnysek
Replies: 0

If path is smooth, path_get_point_x() still returns corners for straight one.

This script tries to find nearest points on smoothed path, and returns their percentage positions as array.

Precision is a param which says how long distance in pixels between checks should be - so this function is rather time-consuming (for path shown in image below, it have around 1800 checks in case of prevision = 1, and 180 when precision is 10). It's best to use it only in create event, and assign result to a value which will be later use.

Expand	/// @param pathid
	/// @param precision_pixels
	
	var _is_smooth = path_get_kind(argument0);
	var _corners = [];
	var _precision = 1/path_get_length(argument0) * clamp(argument1, 1, 10);
	
	// fix issue where if path is closed, it starts to find point 0 in half distance between points 0-1
	var _is_closed = path_get_closed(argument0);
	var _pos = 0;
	if (_is_closed) {
		var _l1 = path_get_length(argument0);
		path_set_closed(argument0, 0);
		var _l2 = path_get_length(argument0);
		path_set_closed(argument0, 1);
		var _l = _l1 - _l2;
		var _pos = - ( _l / _l1) + _precision;
	}
        // end of fix
	
	var _pts = path_get_number(argument0);
	for(var i = 0; i < _pts; i++) {
		var xx = path_get_point_x(argument0, i);
		var yy = path_get_point_y(argument0, i);
			
		var dst = 99999999;
			
		while (_pos < 1) {
			var xxx = path_get_x(argument0, (_pos + 1) % 1);
			var yyy = path_get_y(argument0, (_pos + 1) % 1);
				
			var _dst = point_distance(xx, yy, xxx, yyy);
				
			if (_dst < dst) {
				dst = _dst;
			}
				
			if (dst < 1.1 or _dst > dst) {
				_corners[ array_length_1d(_corners) ] = (_pos + 1) % 1;
				_pos += _precision;
				break;
			}
				
			_pos += _precision;
		}
	}
		
	return _corners;

On image below:
- green path is path when straight
- green points are positions returned by path_get_point_x/path_get_point_y BOTH when path is straight/smooth
- red is path when smooth
- yellow are positions returned by above functions for smooth path. See that they are really precise smile You need to use path_get_x(path_name, point[num]) to get x/y coordinates.

DVl0EZwWsAAXs0B.jpg

This script also solves issue where path_get_point_x/y(path, 0) returns middle position between points 0-1 on path when smooth. It moves backwards by half distance between path start/end and starts to search from there in this case.

#11 Script Submission » image_speed_legacy() [GMS 2.x] » 2018-02-06 11:41:50

gnysek
Replies: 0

As GMS 2.x takes into account frame speed entered in sprite editor too (so image_speed is always multiplied by sprite_get_speed(sprite_index)), here's the legacy version to set sprite animation speed like image_speed does it in 1.x version (relative to game_speed, no to sprite editor settings).

Expand/// @desc sets sprite animation speed like image_speed does it in 1.x version
/// @param speed int

if (sprite_index > -1) {
    image_speed = (game_get_speed(gamespeed_fps) / sprite_get_speed(sprite_index)) * argument0;
}

#12 Re: Script Submission » is_in » 2018-01-30 08:26:14

should be rather:

Expand/*		if is_in(value, x, y, z)
*/

as in GMS2 you can again use variable_exists functions, in which case "variableName" would suggest you need to pass a string.

#13 Re: Script Submission » instance_number_exact » 2018-01-30 08:24:15

It will be documented in next release of GMS2, as I reported this as bug, seeing it includes descendants. The reply was to create a script like above instead of fixing function smile

#14 Re: Script Submission » string_split » 2018-01-18 07:33:25

should be:

Expand}
if (str != "") {
    result[i] = str;
}

return result;

in case of "the sky is blue ".

#15 Script Submission » instance_number_exact » 2018-01-18 07:31:27

gnysek
Replies: 2

Since instance_number also counts childrens, here it's version which checks exactly for object type:


Expand	/// @desc returns number of instances of given object type, without it's childs
	/// @param object_id int

	var __total = 0;
	with (argument0) {
		if (object_index == argument0) __total++;
	}
	
	return __total;

#16 Re: Script Submission » merge_colors(col1,col2,...,amount) » 2017-06-21 05:24:43

it can be nice for day-night cycle, thanks!

#17 Re: Script Submission » string_truncate » 2017-01-13 07:24:50

When argument[1] would be smaller than (string first letter + "...") width, then returned string would be always too wide.

#18 Script Submission » range - creates array filled with numbers from a to b » 2016-11-09 08:49:30

gnysek
Replies: 0

Based on PHP function range() - http://php.net/manual/en/function.range.php

Expand///range(start, end[ , step])
// returns array filled with values from start to end, every X steps
// step is optional, by default it's 1

var _f = argument0;
var _t = argument1;
var _s = 1;
if (argument_count == 3) {
    if (argument[2] != 0) {
        _s = abs(argument[2]);
    }
}

var _array, _i, _j = 0;

if (_f < _t) {
    for(_i = _f; _i < _t; _i += _s) {
        _array[_j++] = _i;
    }
} else {
    for(_i = _f; _i > _t; _i -= _s) {
        _array[_j++] = _i;
    }
}

return _array;

#19 Script Submission » instance_on_top » 2016-11-09 08:40:37

gnysek
Replies: 0

This code returns which instance will be drawn first (valid only for GM:Studio 1.x, and GM5.x-8.x, since in GM:2 we have both layers and dephts (temporary layers))

Expand///instance_on_top(instance1, instance2)
// returns which of two given instances is drawn first

if (argument0.depth == argument1.depth) {
    return min(argument0, argument1);
} else if argument0.depth < argument1.depth {
    return argument0;
}
return argument1;

#20 Re: Script Submission » draw_line_dotted_width(x1, y1, x2, y2, w) » 2016-09-21 09:05:46

draw_line_dotted_width(5,5,100,30, 1); - draw two lines for me, so for sure it's not "dotted" line.

Also xx and yy variables should be predefined with "var" keyword, so they wont affect object variables.

Board footer

Powered by FluxBB