Be a Supporter!

As: Load External Data/cross-domain

  • 4,677 Views
  • 14 Replies
New Topic Respond to this Topic
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
As: Load External Data/cross-domain 2005-07-01 16:58:20 Reply

AS: Main

Loading external data

===================================================================

Pulling data from a local file using loadVars

Create a text file in notepad, and type something in it. Name it 'wah.txt' and place it in the same directory as your fla

Create a dynamic text box with the var: RADISH. This will hold the loaded data.
loadVars in its simplest form - add this code to the first frame of your timeline:

PIE=new LoadVars();
PIE.onData=function(raw){
RADISH=raw;
}

This will load the contents of your text file and display them in the textbox. To load the contents using a button, add this to your button:

on(press){_root.PIE.load("wah.txt");}

You can manipulate RADISH as you would any other string, once the load has finished.

===================================================================

Pulling data from a remote file using loadVars

NOTE: Some of these codes will NOT work when you preview the swf by pressing CTRL+ENTER.
If you get a 'Error opening URL' or 'undefined' values, publish the swf and then view it in the standalone player or a browser.

The limitations of accessing only local files are obvious. To achieve the full potential, you'll probably want to access a data file on the net.

- To access a remote file from your PC, just change the file path in the button actions:

on(press){_root.PIE.load("http://Domain_A.com/subfolder/wah.txt");}

- If your swf is uploaded and the "wah.txt" file is on the same server, use a relative path as you would on your machine

on(press){_root.PIE.load(../subfolder/wah.txt");} or on(press){_root.PIE.load("wah.txt");}

- NOW if your swf is uploaded (to Domain_A), but your data file is on a different domain (Domain_B), we hit complications (AKA Flash's Security Sandbox)

Firstly, you'll need to use an absolute path on your button. Then you'll need a System.security.allowDomain command.

on(press){
System.security.allowDomain("Domain_B.com")
_root.PIE.load("http://Domain_B.com/subfolder/wah.txt");
}

...but wait, it still doesn't work!!! WTF!?!?! (well, those were the sort of words I used when I first tried this)
So, apparently, we need a crossdomain file on Domain_B (the one that hosts 'wah.txt'). Open Notepad, and paste this in:

<?xml version="1.0"?>
<!-- http://www.Domain_B.com/crossdomain.xml -->
<cross-domain-policy>
<allow-access-from domain="*.Domain_A.com" />
</cross-domain-policy>

Save this file as crossdomain.xml (it must be named this), and upload it to the ROOT of Domain_B.
BINGO! Now you can host a swf on NG, and pull data (php, html, text, asp, etc) from your own site.

===================================================================

I'm aware that many people will want to send data as well as recieve it. Since I'm trying to keep the AS: threads easily readable, that will be covered in a separate topic (when I or someone else gets around to it).

Associated reading:

http://www.sitepoint.com/forums/showthread.php?t=120122&mode=linear
http://www.actionscript.org/forums/archive/index.php3/t-58351.html
http://livedocs.macromedia.com...MX_2004&file=00001418.html
http://www.moock.org/asdg/technotes/crossDomainPolicyFiles/


- - Flash - Music - Images - -

BBS Signature
gorman2001
gorman2001
  • Member since: Aug. 18, 2002
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As: Load External Data/cross-domain 2005-07-01 17:15:41 Reply

Nice. this will help solving issues with this kind of programming.

~gorman2001


website :: hugostonge.com
my job :: we+are

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Load External Data/cross-domain 2005-07-01 17:20:01 Reply

I was going to say "Cool,nice work Denvish!" When I just realised that I always say that.
Should I come up with something more original?

"I didn't read your post at all since I am lazy and tired.I am going to say good work because I guess your script works.I actually don't care about this thread right now,I am not going to read it,I am going to go to bed.Anyway,I guesss that tutorial is good."

"Cool,nice job,Denvish"


BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Load External Data/cross-domain 2005-07-01 17:51:46 Reply

At 7/1/05 05:20 PM, -Toast- wrote: "I didn't read your post at all since I am lazy and tired.I am going to say good work because I guess your script works.I actually don't care about this thread right now,I am not going to read it,I am going to go to bed.Anyway,I guesss that tutorial is good."

lol, thanks I guess. Sleep well...


- - Flash - Music - Images - -

BBS Signature
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Load External Data/cross-domain 2005-07-01 17:58:43 Reply

Now I have read it XD
It wasn't bad at all!
I don't think I will use it,but it's always god to have it bookmarked ; )


BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Load External Data/cross-domain 2005-07-02 07:17:45 Reply

very useful and informative :) especially the cross domain part

S-K
S-K
  • Member since: Sep. 17, 2002
  • Offline.
Forum Stats
Member
Level 20
Gamer
Response to As: Load External Data/cross-domain 2005-07-28 18:05:18 Reply

I heard rumours about some html comptability starting to come into flash with this feature... is this true? and/or would it involve a stupid amount of programming?

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Load External Data/cross-domain 2008-07-09 05:16:33 Reply

I need to be able to load only certain parts of the .txt file, is that possible?

I need to store a lot of different variables in the .txt file, and they draw them into the .swf, how would I go about that?


Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Load External Data/cross-domain 2008-07-09 05:18:35 Reply

At 7/9/08 05:16 AM, Hoeloe wrote: I need to be able to load only certain parts of the .txt file, is that possible?

I need to store a lot of different variables in the .txt file, and they draw them into the .swf, how would I go about that?

You cannot read just a bit of the file. May I suggest using a php file and passing it the name of the variable and it just outputs that single variable


- Matt, Rustyarcade.com

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Load External Data/cross-domain 2008-07-09 05:28:43 Reply

Haven't spoken to you in a while, NinjaChicken... But thanks, I'll give that a shot!


Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Load External Data/cross-domain 2008-07-09 05:55:23 Reply

At 7/9/08 05:28 AM, Hoeloe wrote: Haven't spoken to you in a while, NinjaChicken... But thanks, I'll give that a shot!

Just to give you a hand:

<?php
$var = $_POST['var'];
$a = "Hello";
$b = "World";
echo "ret=".${$var};
?>

That should work fine and dandy.
So pass the script a var name with sendAndLoad, and the result will always be in ret.


- Matt, Rustyarcade.com

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Load External Data/cross-domain 2008-07-09 05:56:23 Reply

Sorry for double post, but if ${$var} doesnt work then try $$var instead


- Matt, Rustyarcade.com

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Load External Data/cross-domain 2008-07-09 05:58:30 Reply

At 7/9/08 05:55 AM, Rustygames wrote:
At 7/9/08 05:28 AM, Hoeloe wrote: Haven't spoken to you in a while, NinjaChicken... But thanks, I'll give that a shot!
Just to give you a hand:

<?php
$var = $_POST['var'];
$a = "Hello";
$b = "World";
echo "ret=".${$var};
?>

That should work fine and dandy.
So pass the script a var name with sendAndLoad, and the result will always be in ret.

I'm really kinda new at external data... I only ever worked with cookies before, and never with php... I think I kinda get the gist, but just to be clear, I need to store hundreds of strings in an external file, then draw, say 50 of them at random into an array in the flash file. Is that possible?


Song of the Firefly is on Steam Greenlight and Kickstarter. Give them a look and support the project!
------------------------------

BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Load External Data/cross-domain 2008-07-09 06:01:38 Reply

Yes, do it like that, or use an external DB. Since you're so new I would do it like that first and later upgrade to a DB if needs be. This is more of a question of PHP, not flash


- Matt, Rustyarcade.com

mayoarm11
mayoarm11
  • Member since: Jun. 13, 2007
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Load External Data/cross-domain 2009-11-09 08:51:23 Reply

sorry for the bump, is it possible to CREATE text files within the parent directory, with a given name?


Awesome turret game in the making, featuring 85 uniqe turrets: News post

BBS Signature