Skip to content

Commit 5abc70e

Browse files
committed
add vector ops, split_string
1 parent efc317a commit 5abc70e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

current-scripts/Demos/useful-scripts/scripts/math_structs/math_structs.gml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ function Vector2(_x, _y) constructor {
66
static to_string = function() {
77
return "[ " + string(x) + ", " + string(y) + " ]";
88
}
9+
10+
static add = function(_vec2) {
11+
return new Vector2(x + _vec2.x, y + _vec2.y);
12+
}
13+
14+
static subtract = function(_vec2) {
15+
return new Vector2(x - _vec2.x, y - _vec2.y);
16+
}
917
}
1018

1119
/// @func Vector3(x, y, z)
@@ -17,6 +25,14 @@ function Vector3(_x, _y, _z) constructor {
1725
static to_string = function() {
1826
return "[ " + string(x) + ", " + string(y) + ", " + string(z) + " ]";
1927
}
28+
29+
static add = function(_vec3) {
30+
return new Vector2(x + _vec3.x, y + _vec3.y, z + _vec3.z);
31+
}
32+
33+
static subtract = function(_vec3) {
34+
return new Vector2(x - _vec3.x, y - _vec3.y, z - _vec3.z);
35+
}
2036
}
2137

2238
enum DVLimitMode {

current-scripts/Demos/useful-scripts/scripts/strings/strings.gml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ function dialogue_line_breaker(_str, _max_width, _font) {
7878
return _out_str;
7979
}
8080

81+
/// @func split_string(str, delimiter)
82+
/// @desc Splits string into substrings
83+
/// @arg {string} str
84+
/// @arg {string} delimiter
85+
function split_string(_str, _delimiter) {
86+
var _substrings = [];
87+
var _str_len = string_length(_str);
88+
var _buffer = "";
89+
90+
for (var i=1; i<=_str_len; i++) {
91+
var _char = string_char_at(_str, i);
92+
if (_char == _delimiter) {
93+
array_push(_substrings, _buffer);
94+
_buffer = "";
95+
} else {
96+
_buffer += _char;
97+
}
98+
}
99+
100+
array_push(_substrings, _buffer);
101+
return _substrings;
102+
}
103+
81104
/// @func pad_string(str, char, position, width)
82105
/// @desc Pads a string with char until it is a certain width
83106
/// @arg {string} str

0 commit comments

Comments
 (0)