Encoding URLs

A common issue when using the GameMaker:Studio http_ functions or the url_open() function, is that you often need to encode the url that you are using, otherwise it will not work correctly (if at all).

NOTE: You can find out more about URL Encoding here: http://www.w3schools.com/tags/ref_urlencode.asp

An example of this would be when creating a "post to Twitter" button. Twitter requires that the message information which you are posting be correctly encoded, but when dealing with multiple message elements or a written text message, this can become complex to do yourself.

Now, GameMaker:Studio does not currently have a url encode function, but you can easily make your own using this script here from Insane (a member of the GMC):

////////////////////////////////////////////////////////////////
//
//url_encode(argument0);
//
//URL-Encodes a string according to RFC 1738
//Written by Insane
//http://gmc.yoyogames.com/index.php?showtopic=327587 
////////////////////////////////////////////////////////////////
var orig,new,char,tmp,ans;
orig = argument0;
new = "";
char = 0;
tmp = 0;
ans = 0;
for (ps=1; ps<=string_length(orig); ps+=1)
    {
    char = string_char_at(orig,ps);
    char = ord(char);
    if (char < 32) || (char > 126) || (char == 36) || (char == 38) || (char == 43) || (char == 44) || (char == 47) || (char == 58) || (char == 59) || (char == 61) || (char == 63) || (char == 64) || (char == 32) || (char == 34) || (char == 60) || (char == 62) || (char == 35) || (char == 37) || (char == 123) || (char == 125) || (char == 124) || (char == 92) || (char == 94) || (char == 126) || (char == 91) || (char == 93) || (char == 96)
        {
        tmp = floor(char/16);
        ans = char-tmp*16;
        tmp = string(tmp);
        if (tmp = "10") tmp = "A";
        if (tmp = "11") tmp = "B";
        if (tmp = "12") tmp = "C";
        if (tmp = "13") tmp = "D";
        if (tmp = "14") tmp = "E";
        if (tmp = "15") tmp = "F";
        ans = string(ans);
        if (ans = "10") ans = "A";
        if (ans = "11") ans = "B";
        if (ans = "12") ans = "C";
        if (ans = "13") ans = "D";
        if (ans = "14") ans = "E";
        if (ans = "15") ans = "F";
        new = new+"%"+tmp+ans;
        }
    else
        {
        new = new+chr(char);
        }
   }
return new;

You should create a new script resource in GameMaker:Studio, and call it "url_encode", then copy and paste the above into the script and save it.

To use this script you would call it like any other function, for example:

referrer = url_encode("http://YoYoGames.com");
message = url_encode("Test Tweet!");

The above will set two variables to hold correctly encoded information ready to be sent over the internet. If this was for our twitter button, then you would send the tweet like this: 

url_open('https://twitter.com/intent/tweet?original_referer=' +  referrer + '&text=' + message);

As you can see, this small script is very useful and will save you a lot of time and headaches when sending data over the internet.

You can find a tutorial for using this script to post to Twitter from the RSS Tutorials feed on the GameMaker:studio startup menu.

Have more questions? Submit a request

0 Comments

Article is closed for comments.