As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

59,249

Views

Intermediate

Level

12

Comments

Web Requests

by Jon (Updated on 2015-10-18)


Contents

  • Visiting a URL
  • HTTP Requests
  • Example: Loading levels from a server
  • Accessing Flash variables
  • Launching Apps on Mobile using URLs
  • Frequently Asked Questions

Visiting a URL

Using the open URL block (under Game > Web), you can visit any URL from within a game. The link will open up inside a new tab, using the user's default browser. On Flash/Desktop, a pop-up blocker can prevent this from happening.

Open URL block

Tip: If you want an in-app browser, check out the Web Views extension.

What could you use this for?

  • Visiting Social Media (Facebook, Twitter)
  • Asking for a Paypal donation
  • Linking to your mobile game’s entry

Note: We have pre-built behaviors for the first 2 scenarios. Check them out under the “Utilities” category the next time you import a behavior for a scene.

HTTP Requests (GET / POST)

What if you want your game to communicate with a web-based API? Stencyl also supports making HTTP requests, so that your game can communicate with REST-based APIs and can submit data to scripts that you've built yourself.

How to make an HTTP Request

Make an HTTP request using either the visit URL block or POST data block (both under Game > Web). The first block corresponds to a GET request, while the second corresponds to a POST request.

HTTP request blocks

Either way, calls are asynchronous (non-blocking). We callback by executing the body of the blocks when you receive a response, which can be fetched using the red site's response block.

If the HTTP request fails, no callback will be issued. We recommend using a timeout (via a do-after block, for example) to handle the case of failure.

Multiple Field POST Requests

If you're trying to submit data, a POST request is the preferred method for doing that. What if you want to submit multiple pieces of data?

You can pass in multiple fields at once by separating them with ampersands (&), like this:

name=John&id=123456

Example: Loading Levels from a Server

In an early Stencyl game that featured a level editor, the developer used HTTP requests to implement a level sharing system. We'll now step through a simplified example of this.

Suppose that we’re building a simple game that stores its levels online. We want to create a level loader for this game that takes the data and creates actors based on the type and location.

Here's some sample data: http://dl.dropbox.com/u/2769678/level1.txt

This file contains just 3 entries, one line each. The 3 "columns" correspond to: Name of the actor, x-location, y-location.

robot,0,0
robot,128,128
hero,256,256

Here’s how we could parse this. We tokenize ech line one by one, knowing that each entry is separated by commas.

Example

If you're building your own web services to communicate with Stencyl, keep your data formats simple, and you can achieve pretty cool things with web requests.

Launching Apps on Mobile using URLs

Facebook

Opens the Facebook app to the page you define. Useful for directing users to your Facebook page, so they can "Like" it without having to use the official API.

fb://profile/[your ID]

Twitter

Opens Twitter app to the page you define. For example, you could open it up to the page for your game/studio to encourage a "Follow" without having to use the official API.

twitter://user?id=YOUR_ID

See this forum topic for more examples.

Frequently Asked Questions

Does Stencyl provide XML / JSON parsing out of the box?

Many API's provide responses in standard formats such as XML or JSON. Parsing XML and JSON-based responses is not handled by Stencyl out of the box (in the sense that our blocks support it), but it is supported in code via Haxe.

Can Stencyl communicate directly with a database?

No. For security reasons, it is not best practice to allow a game to directly query a database. Instead, write a script on your server that Stencyl can communicate with using HTTP requests.

If I'm making a mobile game, can I show the page inside the app, instead of launching Safari / Chrome?

If you want an in-app browser, check out the Web Views extension.

Summary

  • Visit URLs to point players to your social media pages, Paypal page or iOS game entry.
  • HTTP requests let you do just about anything. The “Populate” game for Flash and iOS uses HTTP requests to implement a level share system.
  • Flash variables can be used to communicate with a Flash game's containing webpage.
Print Article Edit Article How to Edit an Article
Disclaimer: Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

12 Comments

fmgames
Hola podria actualizar el link de dropbox porque sale error 404 :v
Hi, I could update the dropbox link because error 404 comes up :v
Thanks :u
Gracias :)

0 6 years, 3 months ago
wolf2013
Hi Jon,
you showed how to load values from a TXT file, what about from a PHP file ? Would it be possible to load from array inside a php file ?

1 7 years, 6 months ago
freemanmix
I cant seem to be able to communicate with the server at all through my game can some please just give me a small example of sending text to the server using the visit url thank you.
0 8 years, 8 months ago
Jon
The answer to that question is beyond the scope of Stencyl and doesn't require anything beyond the HTTP requests we provide. Because we get the question, I'll answer it here briefly.

Build a script in any language of your choice (PHP, Python, etc.), pass in the variables via GET/POST and have the script query the database. Stencyl cannot and should never be expected to interact directly with a database.

1 8 years, 12 months ago
wolf2013
why is no one able to answer how to send SCORE to PHP/DATABASE?
1 9 years, 1 week ago
WarrenTierney
How can I save game attributes to a server? I have set up a php file accessing a mysql database, and I also have a crossdomain file allowing access between websites.
1 9 years, 10 months ago
guicamarotto
how can I upload the highscore of the player in my server, and after, load a list of the top 10 best scores and show on game?
0 10 years, 1 month ago
decletro
Does anyone know how viable this is in a popular flash game? If a game got, say, millions of views, would you need a really good server if your game was constantly using web requests?
1 12 years, 4 months ago
Joe
Added a blurb about accessing Flash variables from a forum user: http://community.stencyl.com/index. php/topic,8326.0.html
0 12 years, 6 months ago
Jon
@chris - Rhys developed his own format and hosted it on his own server.

@greyman - Yep, that's a good point. I'll note it.

0 12 years, 6 months ago

Sign In to Comment