Initializing data help needed

16 posts

Flag Post

I’m creating a word puzzle game and have come across a problem I can’t seem to find a satisfactory fix for.

I’m creating arrays of 500 words each for roughly 350k words (with approximately 60k words per actionscript as it seems not to load more than that). My problem is, the only way I can guarantee they all load on my machine is to separate the actionscripts frames with empty frames, thereby increasing my timeline at the beginning of the game (if I don’t, other things don’t seem to load correctly). This is not a game-stopping problem, except I have no control over how long it takes on other peoples computers. For faster ones, many of those extra frames are wasted, and on slower ones, I probably haven’t put in enough. So far I’m left trying to find a happy medium which allows the largest percentage of machines to initialize all these variables while keeping those with the fastest machines from having to wait needlessly (which right now I’m guessing the time to be around 10 secs for older computers, while on mine it’s half that and I’d guess on the fastest ones only 2-3 – thus 7-8 seconds wasted waiting, and potentially causing frustration, for the latter).

My question is, is there a better way to guarantee all variables are initialized before continuing on? I’ve looked into onLoadInit, but that only seems to work at time of compiling, not at runtime which is what I need. I’ve seen games that have an initializing bar after the loading bar, but I’m guessing it’s just an arbitrary time frame similar to what I’ve already gotten set up and is the same speed regardless of how fast the data is actually initialized.

As of now, I’m stuck with the fake initializing bar (which just covers a fixed number of frames), and doesn’t guarantee the data actually is even loaded fully on slow machines. If there is a better way, I’d love to hear it.

Any help would be most appreciated. Thank you.

 
Flag Post

Sorry, this is still a bit too vague to solve. Are you loading this data from an outside source? Why are you trying to use onLoadInit?

 
Flag Post

arcaneCoder, thank you for replying.

I’m not trying to use onLoadInit, I just mentioned I already looked at it and found it wasn’t what I was looking for (just in case someone else might have had the same idea and suggested it). Sorry if this confused you.

As for loading from an outside source, no I’m not. All the data is in the .swf to start with, there is just many variables (in the form of arrays) that need initialized at the beginning.
I’m trying to find a way to initialize this stuff in a way to minimize the time it takes and also be sure it is in memory before moving on. Right now my workaround is to just set an arbitrary number of frames in the timeline for this task. The problem is that for fast computers much of this time is wasted as the variables are initialized quickly, while on slow computers there might still not be enough time to finish before moving to the next tasks. I’m looking for a better way to do this, if there is one.

 
Flag Post

I was asking why you would even think of looking at onLoadInit.

If the data is already compiled into the swf, why dont you just us and EnterFrame event or setInterval and some loops to convert the data? In fact, why are you converting it at all? Why arent they in an array to start with? Its hard to understand what you mean about it ‘being in memory’, since its already in memory once the swf is fully loaded.

You do have a preloader at the beginning of your file to prevent the file from playing until the swf is 100% loaded right? You dont want to stream something like this in. The playhead shouldnt even be getting to the code with the arrays until your swf is 100% loaded.

 
Flag Post

They are already in the arrays to start, in the form of:

var arrayName:Array = Array(“blah”, “blah”, etc.);

with 500 elements per array, and about 700 arrays.

And yes there is a preloader, but that only loads the .swf, none of this actionscript with the arrays is read until the .swf is started. The problem is is the amount of information, even though it’s in arrays to start with, there is just so much that unless I separate it into multiple frames (my limit seems to be about 60k elements per frame whether there are blank frames between them or not) and space them out, they won’t all initialize (ie: load into RAM) and stuff after this gets left out (usually graphics, which won’t then show up on screen).

By spreading it out, I have no problem, but this is based on my computer and I have no way to judge how that same spacing will work on someone elses. I’m just asking if there is a different way to have all that actionscript read and entered into RAM than what I’m doing now.

edit: Oh, forgot to add, when you say "already in memory once the swf if fully loaded, you mean the .swf is in memory. What I’m talking about are the variable declarations in the code itself. This is not loaded until it is read, and that doesn’t happen until the play head gets to the frame with the actionscript on it. No variables are given memory space until that actionscript is parsed. At least this is my understanding.

 
Flag Post

Your question is pretty in-depth and I would say that I myself have had this problem before. If you are exporting MovieClips from the library, on the “first frame” you need to examine what exactly this is doing in the code… If you export all your MovieClips before the first frame (when your pre-loader is on the first frame), your pre-loader will not work. The solution is to include all objects which are exported from the library into a MovieClip on the second frame of the movie. The preloader is on the first frame, which will include all objects in the entire movie. When the Flash movie is loaded, it skips to the third frame which is the Flash game engine. The second frame (which contains all the [exported] objects in the library) is skipped, but it allows the pre-loader to determine the entire size of the .swf file.

Well, I hope that makes sense…

 
Flag Post

…in relation to above post:

Also this method requires you to uncheck the “export on first frame” option and choose a new frame to export to in the ActionScript settings. If you use classes, you can choose the frame that your main code starts on to export the classes to as well.

 
Flag Post

Well, the problem you are having sounds like an unusual one, and either you are doing something very wrong or you are breaking flash.

I would use either an EnterFrame event or an Interval with some loops to declare the array, check if they exist in memory [ie if (myArray) or if(myArray != undefined) etc] and if so, move on to the next frame/declaration. Dont let the timeline autoplay. I honestly cant tell you what your exact solution would be because this is an issue that should not be dealt with on the developers part and a solution can only really be tested if you have the code.

Your other option is to store the code externally and load / parse a series of files one at a time.

Mostly, you probably need to create some sort of continually running code that will load these things one at a time only when the current one has successfully loaded.

 
Flag Post

Thank you, both IndieFlashAr… and ArcaneCoder for your in depth responses.

Do you think it would make a difference if I put all these arrays into movieclips? Right now it’s just on the main timeline and not coupled with any objects in the library (I try to avoid putting any actionscript inside moviceclips/objects if I can help it).

Also, I hadn’t thought of using loops to check to see if the arrays are defined or not, I’ll have to look into this. As for loading externally, I’d like to keep it all internal if possible as some hosting sites only want one .swf file uploaded without extra files.

Another idea that occurred to me is to not load all of it at the beginning, but spread it out throughout the game. Not all of the arrays are used right at first normally, so spreading out would minimize the problem to the point the wait would be negligible. The only problem would be for saved games, but then having another “loading” screen seems to be the norm anyway, so might not be minded as much.

Oh, well, though. I was hoping there was something fundamental about declaring variables that I was missing, but I’m coming to the conclusion that there isn’t and I’m just bumping up to the boundaries of what flash can do.

 
Flag Post

Before I make this comment I want to give a disclaimer. I only recently started coding in Actionscript. However, I have quite a bit of experience in other languages. I understand your issue Mitijea, though I have never experienced it myself. Here are some ideas I would try. I know in other languages that static variables are instantiated at compile time since they can be accessed at any point during run time, so you may try creating a class with all of your static arrays. However, I know that Actionscript does some weird things with arrays in classes… in some cases I have to instantiate the arrays in the constructor in order for the arrays to be completely encapsulated in the class, but this does not sound like it would be an issue for you.

I also think arcane had a good idea in that you can try to keep your code the same, but use an EnterFrame event that detects when the arrays are fully loaded then you can merely delete the event to go on to the next step.

Indie’s idea describes a slightly different problem than you are having, but you could try doing what you had suggested and put the arrays into separate movie clips and just dump them onto frame 2 and hope that they load automatically.

Another idea would be to load the data through XML, but this would require a separate file which you are trying to avoid. Let us know if you find a solution!

 
Flag Post

I wouldnt put them in MovieClips. You can try, and it might help, but it would be a terrible hack of a solution (and a lot of work). I would try more proper coding techniques first.

 
Flag Post

Oh man I just re-read the post and I can see what you’re talking about now… yeah the Flash player evaluates the code on a frame only once the playhead reaches that frame, so it makes sense to assume the variables aren’t given memory space until then… but in your case it depends on what the arrays are loading, how you are loading the content in (via attachMovie, loadMovie, movieClipLoader, etc), and also it depends on whether you want to pre-load it or load it after the program is initialized… sounds like an overall design choice you need to make. Look into the MovieClipLoader and define a handler for the load progress event, so you can update a status bar while it’s loading, and then only skip to the next keyframe when it’s fully done loading.

 
Flag Post

Wow, thanks everyone. You’ve given me much to think about.

Right now my arrays are not attached to anything, but are declared in actionscript on blank keyframes. So I guess I’m not really loading them in by any of the methods Indie mentions. The MovieClipLoader sounds intriguing, and it’s only the arrays so it’s not code I’m going to be wanting to work on at all later so I guess it doesn’t really matter where it’s buried. Still, I’m guessing the onEnterFrame event might be the way to go at first, just to see if it works. I’ll let you all know if that helps or not.

Arcane, you mention “more proper coding techniques”, what would these be? My understanding is that trying to keep as much code out of movieclips and objects and onto the main timeline is considered a proper coding technique, but as I’m still learning, I may have the wrong idea.

 
Flag Post

Yes, you do want to keep your code out of MovieClip, but you also dont want to spread it out all over the timeline. Timeline coding was more proper before we have class support and better OOP. Most of my flas now have hardly any code in them, they are basically just art repositories. I detest coding extensively on the timelines.

What I mostly meant in this case though is that letting a timeline run in order to initialize data is not exactly a clean way, though I totally understanding you are working with some limits here. Right now you are lacking control of what happens, so the goal is to build some code that is more aware of what is going on and give the control back to you.

 
Flag Post

Right now my arrays are not attached to anything, but are declared in actionscript on blank keyframes. So I guess I’m not really loading them in by any of the methods Indie mentions.

Just to clarify, the arrays themselves (i.e. the actionscript code) are not ‘loaded’ in and of themselves. It is the MovieClip or bitmap content that you will need to load and then put into the arrays (Array.push() anyone?)… The Arrays themselves do not need to be loaded— it’s the content that you put into the array that needs to load.

 
Flag Post

Here’s a quick run-down on the different loading methods:

attachMovie – attaches a movieClip from the library which has been exported for ActionScript.

loadMovie – loads external content (.swf, .png, .jpg, .gif) into a MovieClip

MovieClipLoader – This is an object that you can create an instance of and use to monitor the load progress of your external files… it’s described fully in the Flash help.