I’m doing some work in Actionscript 2.0 for the first time in a while (really simple stuff, just pulling content from a text file), and I can not fathom for the life of me why I’m getting such unpredictable output here.
Sometimes when I test build a simple script like this, variables are listed as undefined, and sometimes they aren’t.
I’d assume that this might be because the data loaded in from cookware.txt wasn’t loaded into the memory yet, but this doesn’t seem to be the case – according to the Actionscript dictionary here, the onLoad function only fires when data has been loaded as is accessible to the rest of the program.
Can anyone shed some light? Or see why this could be happening?
Contents of cookware.txt:
pots=44&kettles=43
Code
_global.pots; _global.kettles; trace('variables not assigned') trace('before: kettles (global) = ' + _global.kettles); trace('before: pots (global) = ' + _global.pots); var my_lv:LoadVars = new LoadVars(); my_lv.onLoad = function(success:Boolean):Void { if (success) { trace('variables clearly loaded: kettles = ' + kettles); _global.kettles = this.kettles; trace('assigned during loop: kettles in = ' + _global.kettles); trace('pots = ' + kettles); _global.pots = this.pots; trace('during: pots = ' + _global.pots); } else { trace('Error'); } } my_lv.load('cookware.txt'); trace('after: kettles (global) = ' + _global.kettles); trace('after: pots (global) = ' + _global.pots);
Output::
Sometimes it’s this:
variables not assigned before: kettles (global) = undefined before: pots (global) = undefined after: kettles (global) = undefined after: pots (global) = undefined variables clearly loaded: kettles = undefined assigned during loop: kettles in = 43 pots = 43 during: pots = 43
and sometimes it’s this:
variables not assigned before: kettles (global) = 43 before: pots (global) = 44 after: kettles (global) = 43 after: pots (global) = 44 variables clearly loaded: kettles = 43 assigned during loop: kettles in = 43 pots = 43 during: pots = 43
Again, whether variables are undefined or not seems totally random. What is going on here?
Thanks for the help guys – I didn’t click before that LoadVars() was an asynchronous function, and that the rest of the script would continue without checking if it was loaded.
I managed to fix this by checking it loadVar has completed on each frame, then executing the code after the loadVar was completed.
I had compiling the swf for testing (command + return).