18,313

Views

Intermediate

Level

6

Comments

Web Requests

by Jon (Updated on 2014-04-07)


Contents

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


Visiting a URL

Did you know that you could visit URLs from within a game? It’s as easy as using this block under Game > Web. The link will open up in your default browser inside a new tab.


What could you use this for?

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


Incidentally, we have pre-built behaviors for the first 2 - check them out under the “Utilities” category the next time you import a behavior for a scene.

Note: On mobile platforms, the default browser app will display. If you want an in-app browser, check out the user-built extension for Web Panes.
Note: Some pop-up blockers may prevent your game from opening a new tab.

 

HTTP Requests (GET / POST)

Your game can make HTTP GET and POST requests to remote sites and servers. This is great for fetching data from REST-based APIs or submitting new data.

Calls are asynchronous (non-blocking), and we callback by executing the body of the blocks when you receive a response.

Like the visit URL block, these are found under Game > Web.

Note: For POST requests, you can pass in multiple fields at once by separating them with ampersands (&), like this:

name=John&id=123456

In Populate, the developer used HTTP requests to implement a level sharing system. We step through a simplified example of this in the next section below.

Parsing XML and JSON-based responses is beyond the scope of this article (we'll add one in the future), but check out our example, which uses HTTP GET to load in simple data from a remote server.

 

Example: Loading Levels from a Server

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. 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.

Moral of the story is this: keep your data formats simple, and you can achieve pretty cool things with web requests.

 

Accessing Flash Variables

Not only does Stencyl let you access data from remote sites, but using Flash variables, you can also access data from a Flash game's containing webpage. The following code snippet can be used in a Code block or a Code Mode Behavior to achieve this:

var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(FlxG.stage.root.loaderInfo).parameters;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]);
   print(keyStr);
   print(valueStr);
}

TODO: This is an AS3 example that needs to be ported over to Haxe.

 

Launching Apps on Mobile using URLs

See: http://community.stencyl.com/index.php/topic,30964.0.html

 

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.
Disclaimer: All articles are geared towards Stencyl 3.0 and above. 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!

6 Comments

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 1 week, 4 days ago
Weasel8778
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?
0 2 years, 3 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 2 years, 4 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 2 years, 4 months ago
kinifi
What did Populate use for their level editor? any specific platform?
0 2 years, 5 months ago
Greyman22
Nice one, the in app browser is a great idea, look forward to it.

I also found that you can POST multiple pairs of data if you put it in the form:
name=John;id=123456

Now just need to work on Cookie handling and submitting....

0 2 years, 5 months ago

Sign In to Comment