Using "visit URL and then do..."

balore

  • Posts: 11
I am trying to create something that can accept custom information and then re-post it with more detail further on.

That is, embedding a game on a website which has login information and keeps track of the number of times played. Now I don't know a great deal about web design but I am guessing it is possible to have this data available to read from a URL somehow, and so using "visit URL and then do..." is the best option in Stencyl for importing outside variables.

The problem I am having is the help files covering the usage of these blocks is very limited and doesn't help to explain how to retrieve data or what format the the data has to be in to be received or even an example URL. At the moment I am clueless about how to even start using this function or what is involved to get it working with a website.

Has anyone used this functionality before that could provide an example?
Does anyone know how this works with websites?
What does the website have to do to send data?
What does a get request url even look like?
Is this even the best way to communicate variables like player ID from an outside website source?
Games Designer / 3D Modeler - Visit my Blog/Portfolio: www.badennett.co.uk

Joe

  • *
  • Posts: 2478
Hey balore,

The proper thing to do is to use the "visit URL" blocks to retrieve/"GET" information and the "POST" blocks to send information.

Making a GET request is as simple as appending a question mark and one or more name-value pairs onto the end of a URL. For example:

Quote
http://www.google.com/search?q=stencyl

Here, we expose a variable named "q" having the value "stencyl" to a server-side scripting language like PHP or Python. You can add more of these name-value pairs using an ampersand (&), like so:

Quote
http://www.google.com/search?q=stencyl&hl=en

The data can be returned to you from the server in any number of ways, like plain text, HTML, XML, or JSON. It gets put in the response block. Then it's up to you to make sense of the data on the StencylWorks side. Check out this topic for a related discussion using JSON.

Sending data is done similarly, albeit using the POST block. You might want to send a string like "playerName=balore&highScore=300". And again, the server will have access to variables called "playerName" and "highScore".

The server-side stuff is completely Stencyl-independent, and I'd recommend looking up some tutorials on "REST"-style APIs to help you along. Good luck!

balore

  • Posts: 11
Thanks! This will hopefully solve my problem. Luckily I am not involved in the server side stuff, but have passed on information that will probably lead the web designers to create such a system. With any luck they should pass me back a series of URL's to implement and a list of variables to post to them.
Games Designer / 3D Modeler - Visit my Blog/Portfolio: www.badennett.co.uk

Joe

  • *
  • Posts: 2478
Sounds good. Good luck, and do let us know if you run into any specific issues.

balore

  • Posts: 11
Finally got some work back for me to test this with. Doesn't currently work but want to confirm that I haven't made any silly mistakes before I go blaming the web developers. What I have done is put together a small scene behaviour that should send off a simple piece of text info to a .PHP file which I was told "Anything sent to it using POST would be saved into a log file" Behaviour is as follows:


From what I understand this action should
-Set variable text to "Unsent"
-When you click anywhere on the page it
--Sets variable text to "HelloWorld1349"
--Sends that text to the URL
--Once sent waits 1 second then changes text to "sent"

What actually happens is that once clicked, the variable changes to "HelloWorld1349" then doesn't do anything...

This is exported as a SWF file, uploaded to a private webspace and then tested from there (Local Flash settings doesn't allow communication to the web).

Is there something wrong with my Behaviour? What could cause the "set text to sent" not to work? Might this be something serverside?

Any help is greatly appreciated. I feel out of my depth somewhat in this.
Games Designer / 3D Modeler - Visit my Blog/Portfolio: www.badennett.co.uk

kehwar

  • Posts: 75
Haven't tested it yet but anything inside the POST/GET blocks happens "after" the response is retrieved
Try adding a print [response from wbsite] inside and see if there is an error or something happening

balore

  • Posts: 11
Got an error:

Error #2102: The string passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

It would be great if someone had a tutorial on using these blocks, they are NOT easy to use at all.
Games Designer / 3D Modeler - Visit my Blog/Portfolio: www.badennett.co.uk

Rhys

  • Posts: 1046
For POSTing data you need to define a variable to set, you can't just set nothing. Problem is I don't know how that works, try "text=helloworld".

For my level share I just use a normal get block. It visits the url http://popul8ed.com/11 (or any other number) and the response is the level code.

Note that for this to work your game needs to either be on the same site, be on another site but the site it's accessing must have cross domain policies, or have a local security exception made by flash. You can find out how to do that here:

http://www.stencyl.com/help/viewArticle/48

(might be outdated)

Joe

  • *
  • Posts: 2478
Problem is I don't know how that works, try "text=helloworld".

That should work. See my long-ish post above.

« Last Edit: November 19, 2011, 11:16:36 pm by Joe »