Reading Maps back in to game array?
BlitzMax Forums/BlitzMax Beginners Area/Reading Maps back in to game array?
| ||
Hi! I know how to read files and have succesfully read my saved map back in to my game array. The question is, I need to be able to have the user select what map to load from a directory where many maps are saved. each maps name is given by the user so I would need to be able to select a map to load from a directory and load it. I'm not using MaxGUI just th standard graphics 800,600 mode. Any help would be appreciated. :) |
| ||
Global mappath:String = RequestFile( "Please select a map to load") Print mappath Take a look at the ouput. It would be wise to check the extension type of the provided file to make sure they selected one of your map objects. To do this you can go If Not (ExtractExt( mappath ) = "map") 'insert code here to handle the bad file entry EndIf You could, for instance, just reopen the file selector dialog with a new informative message when the user selects the wrong file like so If Not (ExtractExt( mappath ) = "map") mappath = RequestFile( "Please be sure the map file ends with a .map extension.") EndIf You may want/need to parse the path string further for whatever reason (if you wanted to extract the name of the file and display it on a menu or something). Look in the module documents under "File System" for a list of file path parsing functions. Hope this helps! :) |
| ||
Perfect. Thanks :) |
| ||
Just so you know, you might want to take a look at the additional parameters to "RequestFile". You can set default extensions to open, and also set a starting directory (which is definitely convenient for the end user.) Here's an example from the doc's of using the extension filter: filter$="Image Files:png,jpg,bmp;Text Files:txt;All Files:*" filename$=RequestFile( "Select graphic file to open",filter$ ) Print filename |
| ||
Oh and one more thing - even if you use the extension filter you should test the extension of hte file they've chosen. Why? Well, even if your filter is set up only for .MAP files, a user can still manually enter the name of a file or do something like type *.* in the file selector and get access to all of the files. Really, the filter is there as a convenience to the end user and although it's unlikely that they'd go through the trouble to select an invalid file with the filter in place, it's still not good programming practice to rely on getting a good result. So use the filter so the end user can more easily find the files they want, but still test the extension of the file you're getting (or test the file itself for the correct kind of header - if your map files have headers -) |