Share to Social Networks from GameMaker Games

Share to social networks from GameMaker games

If your game has a social aspect, such as high scores or challenges, you may want your players to be able to share their progress on social media. Most of the popular sites will actually let you do this without installing a complicated SDK into your game for each platform.

BlueSky, Facebook, LinkedIn, Mastodon, Reddit, Threads, WhatsApp and X all allow you to post from a URL, even pre-filling some of the information for the post!

With this knowledge, all we need to do is open the right URL from our GameMaker game, and we can let our players post about it.

One convenient script

We’ve come up with one convenient script called gm_share. The whole script appears below, you can copy and paste it straight into your GameMaker project as-is:

function gm_share(_sharing_platform,_sharing_text) {
	
	// Standardising the name of the platform to reduce errors caused by capital letters / spaces being added
	_sharing_platform = string_letters(string_lower(_sharing_platform));
	
	// Initialise the sharing URL and Character Limit as a blank string
	var _sharing_url = "";
	var _character_limit = 300;
	
	// Update the sharing intent URL and character limit based on the target platform
	switch (_sharing_platform) {
		
		case "bluesky":
			_sharing_url = "https://bsky.app/intent/compose?text=";
			_character_limit = 300;
		break;
		
		case "facebook": // WARNING! This method is deprecated, but the newer methods are not GML-only / Simple.
			_sharing_url = "https://www.facebook.com/sharer/sharer.php?u="; // The text you provide for your users to share can ONLY be a URL.
			_character_limit = 500;
		break;
		
		case "linkedin":
			_sharing_url = "https://www.linkedin.com/feed/?shareActive=true&text=";
			_character_limit = 500;
		break;
		
		case "mastodon": // If you want a specific instance of mastodon, replaced "mastodon.social" with the host url
			_sharing_url = "https://mastodon.social/share?text=";
			_character_limit = 500;
		break;
		
		case "reddit": // Replace r/test/ with the subreddit you want the user to be able to share to.
			_sharing_url = "https://www.reddit.com/r/test/submit/?type=TEXT&title=";
			_character_limit = 300;
		break;
		
		case "threads":
			_sharing_url = "https://www.threads.net/intent/post?text=";
			_character_limit = 500;
		break;
		
		case "whatsapp":
			_sharing_url = "https://api.whatsapp.com/send?text=";
			_character_limit = 300;
		break;
		
		case "x": case "twitter":
			_sharing_url = "https://x.com/intent/post?text=";
			_character_limit = 280;
		break;
		
	}
	
	// Do not execute the rest of the code if gm_share doesn't support the social media platform	
	if (_sharing_url == "") {
		// Returns false so you can handle this in the code
		return false;
	}
	
	// Reduce the amount of text if it doesn't meet the target platform's character limit
	if (string_length(_sharing_text)>_character_limit) {
		_sharing_text = string_copy(_sharing_text,1,_character_limit);
	}
	
	// Make the text web safe to add to the URL
	// (Some characters cannot be used in URLs unless they are encoded, this part of the script either encodes them or removes them from the string).
	var _new_sharing_text = "";
	var _str_len = string_length(_sharing_text);
	for (var i=1;i<=_str_len;i++) {
	
		var _current_char = string_char_at(_sharing_text,i);
		var _ordcc = ord(_current_char);
		
		if (_ordcc>=48 || _ordcc<=57) {_new_sharing_text += _current_char; continue;} // Numbers are added as-is
		if (_ordcc>=65 || _ordcc<=90) {_new_sharing_text += _current_char; continue;} // Uppercase letters are added as-is
		if (_ordcc>=97 || _ordcc<=122) {_new_sharing_text += _current_char; continue;} // Lowercase letters are added as-is
		if (_ordcc<=31 || _ordcc>=127) {continue;} // ASCII control characters, or non-ASCII don't add to string
		if (_current_char == " ") {_new_sharing_text += "+"; continue;} // Spaces need to be replaced with "+"
		// All other possible characters need to be added but encoded...
		if (_ordcc<42) {_new_sharing_text += "%"+string(_ordcc-12);}
		switch(_ordcc) {
			case 42: _new_sharing_text += "%2A"; break;
			case 43: _new_sharing_text += "%2B"; break;
			case 44: _new_sharing_text += "%2C"; break;
			case 45: _new_sharing_text += "%2D"; break;
			case 46: _new_sharing_text += "%2E"; break;
			case 47: _new_sharing_text += "%2F"; break;
			case 58: _new_sharing_text += "%3A"; break;
			case 59: _new_sharing_text += "%3B"; break;
			case 60: _new_sharing_text += "%3C"; break;
			case 61: _new_sharing_text += "%3D"; break;
			case 62: _new_sharing_text += "%3E"; break;
			case 63: _new_sharing_text += "%3F"; break;
			case 64: _new_sharing_text += "%40"; break;
			case 91: _new_sharing_text += "%5B"; break;
			case 92: _new_sharing_text += "%5C"; break;
			case 93: _new_sharing_text += "%5D"; break;
			case 94: _new_sharing_text += "%5E"; break;
			case 95: _new_sharing_text += "%5F"; break;
			case 96: _new_sharing_text += "%60"; break;
			case 123: _new_sharing_text += "%7B"; break;
			case 124: _new_sharing_text += "%7C"; break;
			case 125: _new_sharing_text += "%7D"; break;
			case 126: _new_sharing_text += "%7E"; break;
		}
	
	}
	
	// Open the URL for the intent
	url_open_ext(_sharing_url+_new_sharing_text,"_blank");
	
	// Return true if we got to this point without any errors happening
	return true;

}

Once you have the script in your project, you use it like this to allow your player to share a post:

gm_share("bluesky","Testing GM Share by Chequered Ink! https://chequered.ink/");	

The first argument of the script is the social media platform to post to. The script supports the eight platforms we talked about earlier: BlueSky, Facebook, LinkedIn, Mastodon, Reddit, Threads, WhatsApp and X.

The second argument is the default text to share. The player will be able to change this when the sharing prompt for each platform appears.

Things to be aware of

  • On Windows and in HTML5, this will open a new browser window for the player to post from the website in question. On Android and iPhone, it may open the app for the social media platform if the player has it installed, or it may open the browser. It depends on various factors.
  • The Facebook URL sharer API is deprecated, they no longer officially support it… but it still works for now.
  • Your text on Facebook can only be a URL for your website. You cannot pre-fill the post content. Always make the second argument a URL if posting to Facebook.
  • If you want to post to a specific mastodon instance, and not just mastodon.social, you’ll need to change the link on line 29 of the script.
  • To post on reddit, edit the URL on line 34 of the script to specify a subreddit. This should probably be a subreddit that you founded / moderate specific to your game, or your players are going to get banned for spamming other subreddits.
  • Because this method uses URLs, the text you can post supports only the basic latin alphabet, numbers and common punctuation. The script will strip out any unsupported characters. Always check that it’s posting what you expect before building your final game.

We hope you find this useful, do get in touch if you have any questions.

Daniel

I make funky little games of all different kinds like Gyro Boss, Plunder Dungeons & Spellworm! Also making lots of fonts & free game assets.

Leave a Reply