S
Salvakiya
Guest
SalTools
hey all... here are some scripts which are a part of every project I make. If you see a way they can be optimised please let me know! Also keep in mind some of these are merely shortcuts or things which make my life easier when I code. This does not mean they are the fastest way to do things. you should always be aware of the overhead of calling such functions. They help me keep my code more organised and when I run into performance issues I can rewrite my code without the shortcuts.
If you have any questions or comments let me know!
Edit: thanks to Freddy Jones for help with multiple of these scripts!
Edit: added jarray.. usage arr = jarray("['a','b','c',1,2,3]"); for use with arrays larger than 16
Edit: fixed array to be more efficient on windows. thanks to Yal for sharing! https://forum.yoyogames.com/index.php?threads/initialize-an-array-in-one-line.4232/#post-32510
hey all... here are some scripts which are a part of every project I make. If you see a way they can be optimised please let me know! Also keep in mind some of these are merely shortcuts or things which make my life easier when I code. This does not mean they are the fastest way to do things. you should always be aware of the overhead of calling such functions. They help me keep my code more organised and when I run into performance issues I can rewrite my code without the shortcuts.
usage:
script:
Code:
my_string_length = len("hello")
my_array_length = len(array(1,2,3,4,5,4,3))
Code:
///len(type)// returns length of given array or string
/*
For array returns double;
For array2d returns array of length 2. defaults to MAX possible size.
returns -1 if no valid type found
*/
var type = argument[0];
if is_string(type){return string_length(type)}else
if is_array(type){
//is it 1d?
var ah = array_height_2d(type);
var al = array_length_2d(type,0);
if ah == al{/*yes return 1d*/return al}else{
//return 2d
var m = 0;
for (i=0; i<ah; i+=1)
{
al = array_length_2d(type,i);
m = max(al,m);
};
//return size in array len: return[0],return[1]
return array(ah,m);
}
}
return -1;
usage:
script:
Code:
obj = new(object0)
obj = new(object0,x,y)
obj = new(object0,x,y,z)
Code:
///new( object, [ x[, y[, z] );
if( argument_count == 0 || argument_count > 4 ){ show_error("Incorect arguments supplied", true); }
var args = array( argument[0], 0, 0, 0);
for( var i = 1; i < argument_count; i ++ ){
args[ i ] = argument[i];
}
with( instance_create( args[1], args[2], args[0]) {
z = args[3];
return id;
}
Code:
#define print
///print(str message[,bool debug]);
var message = "";
for (var i=0; i<argument_count; i+=1)
{
message+=string(argument[i]);
};
show_debug_message(message)
Code:
///asset_exists(str name) returns bool
//shortened thanks to Freddy Jones =)
return (asset_get_index(argument[0]) != -1 );
Code:
///array(val,val,val,val,val,val,val,ect);
var arr;
for (i=0; i<argument_count; i+=1)
{
arr[i] = argument[i];
};
return arr;
Thanks to Freddy Jones for the fix!
Code:
///jarray(json_list)
var map = json_decode(argument0);
// if the json could not be parsed json_decode returns -1
if( map == -1 ){ show_error("Json could not be read.", false);}
// if it was a list, the list is found in "default
var list = ds_map_find_value(map, "default");
var len = ds_list_size(list);
for (var i = 0, arr; i < len; i ++ ){
arr[i] = list[|i];
}
ds_map_destroy(map);
ds_list_destroy(list);
return arr;
If you have any questions or comments let me know!
Edit: thanks to Freddy Jones for help with multiple of these scripts!
Edit: added jarray.. usage arr = jarray("['a','b','c',1,2,3]"); for use with arrays larger than 16
Edit: fixed array to be more efficient on windows. thanks to Yal for sharing! https://forum.yoyogames.com/index.php?threads/initialize-an-array-in-one-line.4232/#post-32510
Last edited: