Skip to content

Instantly share code, notes, and snippets.

View katsaii's full-sized avatar
🟣
Icon by NightMargin

Kat katsaii

🟣
Icon by NightMargin
View GitHub Profile
@katsaii
katsaii / draw_polygon.gml
Last active September 19, 2019 18:34
Draws a polygon using the scan-line algorithm.
/// @desc Uses the scan line algorithm to render a polygon.
/// @param polygon {Integer} The id of the ds_list which stores pairwise positios of verticies.
/// @param outline {Boolean} Draw an outline instead of a filled shape.
/// @author Kat @katsaii
var count = ds_list_size(argument0) div 2;
if (count > 1) {
if (argument1) {
// outline
draw_primitive_begin(pr_linestrip);
draw_vertex( // last vertex
@katsaii
katsaii / ds_list_get_type.gml
Last active September 13, 2019 23:50
Finds the nested data structure type from a Game Maker list.
/// @desc Returns the type of data structure nested at an index of a ds_list.
/// @param id {Integer} The id of the ds_list to consider.
/// @param pos {Integer} The index of the ds_list to inspect.
/// @author Kat @katsaii
if (is_real(argument0[| argument1])) {
// copy the ds_list into a duplicate list that can be manipulated freely
var clone = ds_list_create();
ds_list_copy(json_list, argument0);
// delete all records other than what's required
repeat (argument1) {
@katsaii
katsaii / ds_map_get_type.gml
Last active September 13, 2019 23:51
Finds the nested data structure type from a Game Maker map.
/// @desc Returns the type of data structure nested at a key of a ds_map.
/// @param id {Integer} The id of the ds_map to consider.
/// @param key {String} The key of the ds_map to inspect.
/// @author Kat @katsaii
if (is_real(argument0[? argument1])) {
// copy the ds_map into a duplicate map that can be manipulated freely
var clone = ds_map_create();
ds_map_copy(clone, argument0);
// delete all records other than what's required
var key = ds_map_find_first(clone);
@katsaii
katsaii / vertex_format_size.gml
Last active March 21, 2021 15:58
Returns the size (in bytes) of a Game Maker vertex format.
/// @desc Returns the size (in bytes) of a vertex format.
/// @param vformat {Integer} The vertex format to inspect.
/// @author Kat @katsaii
var max_size = 256;
/* 32 bytes is a realistic size.
* 256 is an, unrealistic, finite upper limit.
*/
var buff = buffer_create(max_size, buffer_grow, 1);
var vbuff = vertex_create_buffer_from_buffer(buff, argument0);
var vbuff_buff = buffer_create_from_vertex_buffer(vbuff, buffer_grow, 1);
@katsaii
katsaii / dtri.gml
Last active September 13, 2019 23:31
Calculates a triangle wave at a specific phase in radians.
gml_pragma("forceinline");
/// @desc Calculates a triangle wave.
/// @param angle {Real} The angle in degrees to consider.
return tri(degtorad(argument0));
@katsaii
katsaii / script_current.gml
Last active November 3, 2019 09:43
A hack which finds the index of the current calling script.
/// @desc Returns the id of the current script.
/// @author Kat @katsaii
var callstack = debug_get_callstack();
var header = "gml_Script_";
var delimiter = ":";
for (var i = 1; i < array_length_1d(callstack); i++) {
var currentTrace = callstack[i];
if (string_pos(header, currentTrace) == 1) {
var name = string_replace(currentTrace, header, "");
var length = string_pos(delimiter, name);
@katsaii
katsaii / array_clone.gml
Last active September 13, 2019 23:21
A hack which clones a GameMaker array
/// @desc Returns a clone of an array.
/// @param variable {Array} The array to clone.
/// @author Kat @katsaii
if (array_length_1d(argument0) < 1) then return [];
argument0[0] = argument0[0];
return argument0;
@katsaii
katsaii / Properties.gml
Last active September 20, 2019 19:45
Pseudo properties using macros in GameMaker
#macro VAR "foo"
#macro GETTER variable_global_get(VAR)
#macro SETTER \
for (var __value = GETTER;; { \
if (is_real(__value) && __value > 0) { \
variable_global_set(VAR, __value); \
} else { \
show_error("Global property '" + VAR + "' must be a positive number", false); \
} \
break; \
@katsaii
katsaii / point_distance_line.gml
Last active September 13, 2019 23:20
Finds the distance between a point and a line
/// @desc Finds the minimum distance between a point and a 2D line.
/// @param x {Real} The X position of the point.
/// @param y {Real} The Y position of the point.
/// @param u1 {Real} The first X position of the line.
/// @param w1 {Real} The first Y position of the line.
/// @param u2 {Real} The second X position of the line.
/// @param w2 {Real} The second Y position of the line.
var qx = argument0;
var qy = argument1;
var px = argument2;
@katsaii
katsaii / draw_grid.gml
Last active September 13, 2019 22:47
Obfuscated code to draw the contents of a 2D array to the screen on a single line + JSDoc information.
///@param id,size,x,y
var G=argument0,W,H,S=argument1,i=0,j,k,a,b;if ds_exists(G,5){W=ds_grid_width(G)H=ds_grid_height(G)while(i<W*H){j=i%W;k=i++div W;a=argument2+j*S;b=argument3+k*S;draw_rectangle(a,b,a+S,b+S,1)draw_text(a,b,G[#j,k])}}