View Full Version : [AS3] retrieving variables URLLoader.data
skatehead
March 24th, 2007, 08:25 AM
hey, im back with another as3 question :)
i've been setting up a high score table in my game, and everything is working, but i cant work out how to get variables from the URLLoader.data that im retrieving
if i trace loadedScores.data, i get this
&name1=matt&score1=323250&name2=1234567890123456... and so on
i've tried passing it to a URLVariables constructor, but it says the string doesnt contain the correct name/variable pairs or something, and loadedScores.data.name1 doesnt work...
thanks for your help, cant work this one out for the life of me
cheers
skatehead
March 24th, 2007, 11:21 AM
i couldnt get it working so i wrote my own function, which i figured i'd post here in case someone had a similar problem
//======================
// get var
//======================
private function getVar(varName:String, variables:String):String{
var tString:String = "";
var tInt:int = variables.search("&" + varName + "=");
if (tInt == -1){
return "";
}
tInt += varName.length + 2;
var tInt2:int = tInt;
// start at tInt, go until "&"
for (var i:int = tInt; i < variables.length; i++){
if (variables.charAt(i) == "&"){
tInt2 = i;
break;
}
}
// make new string
return variables.slice(tInt, tInt2);
}
and you just pass in the name of the variable i.e "name1" and the string with all the variables in it like in my first post and it will return the matching variable
cheers
senocular
March 24th, 2007, 11:42 AM
are you using
URLLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
?
skatehead
March 24th, 2007, 11:47 AM
yeah i tried that but that just gave me an error when it was sending the request (im using armorbot.com for the scoreboard which is geared more towards as2)
im happy with the workaround, its a once off think so it doesnt have to be terribly efficient
thanks for the suggestion tho :)
senocular
March 24th, 2007, 08:47 PM
that might be because you're starting your variable string with "&" which is incorrect
skatehead
March 24th, 2007, 08:57 PM
i noticed that, and since i dont have any control over the data sent to me, i tried removing it from the string after it was loaded, then passing it to the URLVariables constructor but that didnt work either
michaelkara
June 4th, 2007, 02:40 PM
Step 1
get your php to echo
eg) name1=matt&score1=323250&name2=1234567890123456... and so on
notice the first var is &'less got it! name1 not &name1
Step 2
(actionscript3 code)
var url:String = "http://localhost/file.php";
var reqURL:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader(reqURL);
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
function handleComplete( event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
//do your stuff man.. for example
trace(loader.data["name1"]);
trace(loader.data["score1"]);
}
let me now if it worked out email at michaelk@mgi.ac.za
ps just check my spelling I typed out the code from my desktop to my laptop...
separate systems etc...
ngaycuoithang2
June 9th, 2008, 02:59 PM
that worked! thanks.
plaidharper
May 11th, 2009, 06:12 PM
actually having problems with this
trying to do the same with reading in variables from asp
using response.write in asp to 'echo' name/value pairs
only difference is my loader.data is returning the entire contents of the asp file
any clues?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.