You are not logged in.
Since not everyone uses GMS2 and don't have the money to keep paying for subscriptions(seriously, not everything needs a sub model..), I made my own string concatenation scripts for GMS1.4!
What does all this mean? Well instead of doing:
draw_text(x, y - 20, "Player X = " + string(x) + "#Player Y = " + string(y));
Instead, you can do:
draw_text(x, y - 20, string_concat_var("Player X = {x}#Player Y = {y}"));
A much more simpler way of combining strings!
Here's the code for everything:
///string_concat(string, args...);
//Replaces any occurances of {#} with the arguments provided, then adds them all up to give you a new string with everything combined.
//Example: draw_text(room_width/2, room_height/2, string_concat("Hey, this is a {0}", "test!"));
var str_ = argument[0];
for ( var i = 0; i < argument_count - 1; i++; ) {
str_ = string_replace_all(str_, string_concat_simple("{", i, "}"), string(argument[i + 1]));
}
return str_;
///string_concat_quote(string, args...);
//Same as string_concat, but this script just adds quotes for you.
//Example: draw_text(room_width/2, room_height/2, string_concat_quote("hey who tf is {0} in this situation??", "we"));
var str_ = argument[0];
for ( var i = 0; i < argument_count - 1; i++; ) {
str_ = string_replace_all(str_, string_concat_simple("{", i, "}"), string_concat('"{0}"', argument[i + 1]));
}
return str_;
///string_concat_ext(string, start, end, args...);
//Replaces anything - inside your character pairs - with the arguments provided, then adds them all up to give you a new string with everything combined.
//Example: draw_text(room_width/2, room_height/2, string_concat_ext("Ooh %0%, another %1%!", "%", "%", "look", "test string"));
var str_ = argument[0];
var strS_ = argument[1];
var strE_ = argument[2];
for ( var i = 0; i < argument_count - 3; i++; ) {
str_ = string_replace_all(str_, string_concat_simple(strS_, i, strE_), string(argument[i + 3]));
}
return str_;
///string_concat_ext_quote(string, start, end, args...);
//Same as string_concat_ext, but this script just adds quotes for you.
//Example: draw_text(room_width/2, room_height/2, string_concat_ext("* You got the >0<!", ">", "<", "Test String"));
var str_ = argument[0];
var strS_ = argument[1];
var strE_ = argument[2];
for ( var i = 0; i < argument_count - 3; i++; ) {
str_ = string_replace_all(str_, string_concat_simple(strS_, i, strE_), string_concat('"{0}"', argument[i + 3]));
}
return str_;
///string_concat_simple(args...);
//A VERY simple version of a string concatenation function. All arguments are added into one big string. Supports variables too.
/*Example:
draw_text(room_width/2, room_height/2, string_concat_simple("Player speed = ", obj_player.spd));
var text_ = "fine evening?";
draw_text(room_width/2, room_height/2, string_concat_simple("Hey! How are you this ", text_, "#Good? Ahh, me too!"));
*/
var str_ = "";
for ( var i = 0; i < argument_count; i++; ) { str_ += string(argument[i]); }
return str_;
///string_concat_var(string, [start], [end], [quotes]);
/*
Replaces any variable, global or instance, inside { } with whatever that variable returns. No arguments needed.
Optionally, you can define the start and end letter paris to look for variables inbetween of. Also, quotes, if you want.
Supports builtin, instance, and global variables. I would've supported local variables too, but GameMaker got rid of the variable_local_get and variable_local_exists function.
You could edit the script and try implementing this https://yal.cc/gamemaker-get-set-variable-value-by-name/, but I won't support it myself.
Example:
draw_text(room_width/2, room_height/2, string_concat_var("Current FPS: {fps}"));
(Somewhere in your code, probably the Room Start event) global.myroom = room_get_name(room);
draw_text(room_width/2, room_height/2, string_concat_var("My Room: {myroom}", "{", "}", true));
draw_text(room_width/2, room_height/2, string_concat_var("Room Speed = <room_speed>", "<", ">"));
*/
var str_ = argument[0];
var strstart, strend, strquote;
if ( argument_count >= 3 ) { strstart = argument[1]; strend = argument[2]; } else { strstart = "{"; strend = "}"; }
if ( string_pos(strstart, str_) ) {
var getvar;
var strl_ = string_count(strstart, str_);
var stroff = 1;
for ( var i = 0; i < strl_; i++; ) {
var getvar = string_copy(str_, string_pos(strstart, str_) + stroff, ( string_pos(strend, str_) - string_pos(strstart, str_) ) - stroff); //Find whatever's inbetween { and }
var fetchvar = get_variable(getvar); //Find the value that getvar returned
if ( argument_count == 4 ) { strquote = argument[3]; } else { strquote = false; }
if ( !strquote ) { str_ = string_replace(str_, string_concat_simple(strstart, getvar, strend), fetchvar); } //Replace "{getvar}" with the value that getvar returned
else { str_ = string_replace(str_, string_concat_simple(strstart, getvar, strend), string_concat_quote("{0}", fetchvar)); }
}
}
return str_;
///get_variable(string, [id]);
//Required for string_concat_var()
var result, inst;
if ( argument_count == 2 ) { inst = argument[1]; } else { inst = id; }
if ( variable_instance_exists(inst, argument[0]) ) {
var result = variable_instance_get(inst, argument[0]);
//show_debug_message("instance variable");
}
else if ( variable_global_exists(argument[0]) ) {
var result = variable_global_get(argument[0]);
//show_debug_message("global variable");
}
else { show_error("No such variable, instance or global, exists.", true); }
return result;
Here's an example output using the various scripts described above:
Code to produce output shown above:
If you think there's a better way of coding this, do lemme know! Not the most experienced, but I tried. :)
Last edited by Soup Taels (2023-05-22 02:03:38)
idk about you guys...but i really like soup haha
Twitter: https://twitter.com/BowlOfTailsSoup
Game Jolt: https://gamejolt.com/@souptaels
Offline
Added support for string concatenation with variables!
idk about you guys...but i really like soup haha
Twitter: https://twitter.com/BowlOfTailsSoup
Game Jolt: https://gamejolt.com/@souptaels
Offline
Forgot to add the necessary script to make string_concat_var work. Added get_variable!
idk about you guys...but i really like soup haha
Twitter: https://twitter.com/BowlOfTailsSoup
Game Jolt: https://gamejolt.com/@souptaels
Offline