YoYo Games Wiki

Manipulating A String

From YoYoGames Wiki

Portions of content from this page are originally found in issue 11 of GMTech Magazine, written by Rixeno.

If you have ever read the GM help file and checked the string handling functions, some of you might be thinking that this is great, and some of you might be wondering if there can be more. Well, the answer to both is simply yes. By using GM’s already built-in string handling functions, you can manipulate a string in almost any way possible, but, like everything, it needs some time. Here are some handy scripts that can help you along the road of string manipulation.


Reverse a string

This takes a string that you assign and reverses it. For example, “GMTech” reversed will equal “hceTMG”.

   //string_reverse
   //Takes a string and reverses it
   //Argument(s):
   //argument0 = string
   //Returns: The reveresed string
   var str_length,str_new,i1,i2,str_char;
   str_length=string_length(argument0);
   str_new=””;
   i1=str_length;
   i2=1;
   repeat(str_length)
   {
   str_char=string_char_at(argument0,i1);
   str_new=string_insert(str_char,str_new,i2);
   i1-=1;
   i2+=1;
   }
   return string(str_new);


Randomize a string

By taking every character in the string and putting it in a random position, you randomize it. Randomizing has many possibilities and it increases with the size of the string.

   //string_randomize
   //This script takes a string and places the characters at random positions
   //Argument(s):
   //argument0 = string
   //Returns: The randomized string
   var str_length,str_new,str_char,i1;
   str_length=string_length(argument0)
   str_new=””;
   i1=1;
   repeat (str_length)
   {
   str_char=string_char_at(argument0,i1);
   str_new=string_insert(str_char,str_new,random(
   str_length));
   i1+=1;
   }
   return string(str_new);


Encrypt/Decrypt a string

There are so many topics on scripts to encrypt and decrypt a string. This is a very simple, yet very effective, script that only requires a string and a number. The number will change the ASCII value of every character. If you put the number high enough, it will be all black, or question marks, and once you set the negative value of the encryption number, you get the decrypted version of the string.

   //string_encrypt
   //This script encrypts/decrypts a string using the numerical value
   //Ex: Encrypt level 5
   // Decrypt level (-5)
   //Argument(s):
   //argument0 = string
   //argument1 = encryptic value (number)
   //Returns: The encrypted/decrypted string
   //Written by Rixeno
   var str_length,str_new,str_char,str_crypt,
   str_number,i1;
   str_length=string_length(argument0)
   str_new=””;
   i1=1;
   repeat (str_length)
   {
   str_char=string_char_at(argument0,i1)
   str_crypt=real(ord(str_char)+argument1)
   str_new=string_insert(chr(str_crypt),str_new,i1)
   i1+=1;
   }
   return string(str_new);