PDA

View Full Version : Returning multiple values from a function


madcat
02-06-2009, 02:43 PM
I'm pulling info from an XML file... there are three variables (year, month, day) that I need to process further in another function, but I can't figure out how to send them out of the function and into the next for processing.

Is it possible to make their values globally accessible?


var year:uint;
var month:uint;
var day:uint;

// GET XML
var schedule:XMLManager = new XMLManager("schedule.xml");
schedule.addEventListener(Event.COMPLETE, formatXMLContent);
schedule.start();

// set XML
function formatXMLContent(event:Event) {
// trace(newInstance.xmlContent);
// trace(newInstance.xmlContent.concert.name.text());
dtEvent.text = schedule.xmlContent.concert.(@id == 1).name.text();
dtLocation.text = schedule.xmlContent.concert.(@id == 1).location.text();

year = schedule.xmlContent.concert.(@id == 1).year.text();
month = schedule.xmlContent.concert.(@id == 1).month.text();
day = schedule.xmlContent.concert.(@id == 1).day.text();
}


Thanks,
M

wvxvw
02-06-2009, 02:49 PM
Why not just pass the entire node? (<concert/>)
1. it'll be more efficient because you'd execute E4X only once.
2. Actually, solves your problem...

madcat
02-06-2009, 02:56 PM
Would that be the same answer if I told you that year, month, and date are bound for a variable endDate, which is then run through a timer scenario?


this would be next...
var endDate:Date = new Date(year, month, day);
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();

function updateTime(event:TimerEvent):void {
var now:Date = new Date();
var timeLeft:Number = endDate.getTime() - now.getTime();
var seconds:Number = Math.floor(timeLeft / 1000);
var minutes:Number = Math.floor(seconds / 60);
var hours:Number = Math.floor(minutes / 60);
var days:Number = Math.floor(hours / 24);
...
...
...


I'll try and look at some other ways to break this code down to take better advantage of E4X. I think I'm trying to fit a square peg through a round hole.

My entire goal is to figure out a way to display one node at a time based on a timer event.

madcat
02-06-2009, 03:05 PM
Just saw http://www.actionscript.org/forums/showthread.php3?t=195916 which might help...

wvxvw
02-06-2009, 03:30 PM
var dateXML:XML = <date year="2009" month="5" day="23"/>;
var endDate:Date = new Date(dateXML.@year, dateXML.@month, dateXML.@day);
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();

function updateTime(event:TimerEvent):void
{
var now:Date = new Date();
var timeLeft:Number = endDate.getTime() - now.getTime();
var seconds:int = timeLeft / 1000;
var minutes:int = seconds / 60;
var hours:int = minutes / 60;
var days:int = hours / 24;
var xml:XML = <time-to-event
timeLeft={timeLeft}
seconds={seconds}
minutes={minutes}
hours={hours}
days={days}
/>
trace(xml.toXMLString());
}
Hope this answers it, not sure I understood you though...

GFX Complex
02-06-2009, 03:48 PM
All I can say is what you are asking is not the same as what you think it means.

First off, if you try to return a value in a event method handler you are breaking convention and will most likly have other problems down the road. You should make an other function that you call with in your event handler that does all the encoding/decoding if you need to expect a return value.

Second off, the "return" statement can only return one value, so if you need to return multiple values, use an array, object or some other form of container instead. That way you are passing the container as a single object vs passing multiple arguments.

madcat
02-06-2009, 03:53 PM
GFX Complex, I have no problem admitting that ;)
Thanks for the tips above...

Just so you know clearly what I'm doing it's like this.

I have a class called XMLManager, that takes any XML file and sends back the content to my FLA, or any other class using a dispatch event. This is the code I use to call back the XML file's content from the FLA:


var schedule:XMLManager = new XMLManager("schedule.xml");
schedule.addEventListener(Event.COMPLETE, formatXMLContent);
schedule.start();


Now I want to get all the data from XML in the formatXMLContent function (using trace prints out all the data plus tags):


function formatXMLContent(event:Event) {
trace(schedule.xmlContent);
}


Each node contains data destined for a dynamic text box on stage. The year, month, day values from the XML file however, need to be processed by the timer function(s) before getting to a dynamic text box.

Hence, if I use formatXMLContent to pull the data out, the values are contained in the formatXMLContent function, but I need those values outside of the function, hence the problem. It's as though I need to send them from formatXMLContent and make them available globally.

And it's just something with the process or my lack of knowledge that's creating the issue.

Thanks again for your help -- I'll piece it together bit by bit.

M

GFX Complex
02-06-2009, 07:25 PM
Then this would be your best bet.

var schedule:XMLManager = new XMLManager("schedule.xml");
schedule.addEventListener(Event.COMPLETE, formatXMLContent);
schedule.start();

var _model:Object;


function formatXMLContent(event:Event) {
//Make a new instance of your data model
_model = {day, year, time, blaa blaa blaa}
//Call some other function now that the data is ready
dosomething(_model);

//or if you need a return use this function
var temp:Object = returnSomething(_model);
trace(schedule.xmlContent);

//Or now you can just tell something random that the _model has data
dispatchEvent(new Event(Event.COMPLETE));
}

//This function will do something but not return something;
function dosomething(model:Object):void
//take var and dosomething
}


//This function will return a result for you;
function returnSomething(model:Object):Object{
//take var anddosomething then return result
}

madcat
02-06-2009, 07:58 PM
function formatXMLContent(event:Event) {
//Make a new instance of your data model
_model = {day, year, time, blaa blaa blaa}
//Call some other function now that the data is ready
dosomething(_model);

//or if you need a return use this function
var temp:Object = returnSomething(_model);
trace(schedule.xmlContent);

//Or now you can just tell something random that the _model has data
dispatchEvent(new Event(Event.COMPLETE));
}

//This function will do something but not return something;
function dosomething(model:Object):void
//take var and dosomething
}


//This function will return a result for you;
function returnSomething(model:Object):Object{
//take var anddosomething then return result
}


Dude, that's awesome... this helps me better understand how things work.

Thanks!

madcat
02-13-2009, 03:06 PM
...well, I've come a long way and I'm almost there.

I think I'm getting the object syntax incorrect though... really, anything I try using braces I get 1084 (expecting colon) and 1083 (not expecting right brace) errors...

_model = { param1, param2, param3, param4 };

I have the livedocs open as we speak but none of the examples seem to do the trick. Is the syntax above missing something?

I'm working in a class, trying to dispatch to the fla.

wvxvw
02-13-2009, 03:17 PM
var object:Object = { objectProperty: "propertyValue", anotherProperty: 123 }