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.
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.
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.
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
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]
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.
12 Comments
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
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
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
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
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
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
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
Added a blurb about accessing Flash variables from a forum user: http://community.stencyl.com/index. php/topic,8326.0.html
0
@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