1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

[SOLVED]Help with File Handling

Discussion in 'Programming' started by the_dude_abides, Oct 1, 2019.

  1. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    685
    Hi.

    I am trying to make a function that will loop through any scripts in a project, and read them as a text file into a data structure. The intent is to be able to edit the data structure content: remove unused variables / duplicated variables / correct errors and so forth, and then write the edited text file back into the script directory (or at least be saved, and then can be manually replaced)

    I've figured out how to loop through the script resources (this is while running the project) and create ds_lists that have newly created variables with extended names taken from the script names. Then at the end it deletes all of these structures. The process for this seems to be fine.

    The problem comes from whether it is possible to set up the file handling to automatically allow the user to read the scripts, once the directory is pointed to manually. Using "get_open_filename" gives the means to locate the files source, but I want to make this automated afterwards for multiple files from the script location, and can't figure out how to do that.

    Assuming that the issue isn't getting the file path etc, but that the issue is that access is only given each time "get_open_filename" is used (which I can see how I might be able to do that repeatedly, but it could prove tedious to use if there's lots of scripts to edit)

    Is there a way to get the location of where the scripts are, and repeatedly access it for different files, without having to just keep calling "get_open_filename"?

    OR

    Do any of the directories temp / working / program have the scripts stored in them? I tried all three without success, but maybe wasn't calling them correctly. Finding the scripts location manually isn't a problem, yet using the path it gives seems to fall foul of the sandboxing. So is there one of the accessible directories that has the scripts in it?

    Thanks for any help with this :)
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    991
    The only place where the script code exists in individual file form is in your project directory. I would imagine your running code has no implicit idea where that root directory is, so you'll have to disable the sandbox and provide the root directory one way or the other (ask for it, or hardcode it) for file open calls to use in conjunction with appending "/scripts/<name of script>/<name of script>.gml".
     
    the_dude_abides likes this.
  3. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    685
    Since the sandbox is hard coded into GMS, would I be right in thinking you're referring to use of a DLL to expand the scope?

    Code:
    GameMaker\Projects\script_reader_plus.gmx\scripts\scr_read_script.gml
    Is the ending of what I get from "get_open_filename", and I'd hoped to use it by letting the user edit out the 'scr_read_script.gml' part - just leave the path details behind. Then access new ones by adding in the script name and file type. I see that it doesn't have the same order as your example, is that just a mix up?

    Because, I haven't been able to translate this location into a path I can access repeatedly. Maybe this command only allows for one file? So I'd want to aim for a folder (scripts) and have access to that location - rather than a specific file. I tried using the edited path route with "file_find_first" and I'm unsure if that fell foul of the sandboxing, or wasn't configured correctly. If I remember correctly it crashed GMS, so wasn't an encouraging result :(

    Ultimately I can include the scripts as extra content, and the user just has to save them as text files / include them in the project - which allow them to be read and edited anyway, but isn't automatic. Though I really want to see if this can be made to be automatic, and limit how much behind the scenes things the user has to do.

    If they just find the start location, then it would be nice to take over from there and have everything go through to it's finish.
     
  4. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    991
    There's no such thing as a script to work with for a compiled game. It's all compiled in. GMS does not have the ability to load and execute external scripts. What you can do is look into market place offerings for interpreting strings as code for execution or embedding scripting languages like Lua.
     
    the_dude_abides likes this.
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    685
    @chamaeleon
    It's not an external script, and I don't want to load it. I want to be able to read the content (GML) of scripts that are in the game project, by accessing them as text files while running an exe.

    The GMS editor can save them as text files in the resource tree, and Windows Notepad can read .gml files as text files using auto decoding, so it wouldn't seem much of a stretch that GMS could do the same with it's own data format while running a project. However, in practice: when pointed to a '.gml' file it won't treat it as a text file and open it.

    Notepad is a super basic utility, and yet it can do that without an issue, so I still hold hope that GMS can do so too. Maybe it's not possible, or maybe I'm not explaining this very well :)

    EDIT:
    In Notepad:
    'all files' must be on to show the .gml files
    then using ANSI or UTF-8 it will read the .gml files as text

    So can GMS do the same conversion?
     
    Last edited: Oct 2, 2019
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    991
    Did I miss the 1.4 tag or was it added later? I could have sworn I looked for it before replying. Without an extension I don't think you can escape the sandbox without asking to open each file individually using the function get_open_filename. Gms 2 recently allowed you to disable the sandbox but it is not an option for 1.4 without an extension. But even so, can you verify that what you want to do is to open the file (as in make it accessible in code, not open in a text editor or in the GMS IDE), use the file read functions and string functions to change the code and write it out?
     
    the_dude_abides likes this.
  7. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    685
    I haven't changed the tags :)

    This is what I want to do, but it isn't possible with the .gml files. I'm thinking I might have to use an extension by Samuel Venables (can't recall the extensions name - power shell?) and open up Notepad in tandem with an exe. Then use that to convert the gml into a text file, and send it back to the project.

    That seems extra effort when GMS can do this by itself, albeit when not running any code (i.e in the editor), but it would be a "hack" I can do.....
     
  8. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    991
    Besides the sandbox, I have no idea why you think a GMS game/program wouldn't be able to read a script file using the file functions.. Your game project code would most certainly reside outside the sandbox, so due to that you're stuck attempting to read the file. But if that limitation wasn't there, you'd be good to go. So again, an extension to allow the running program to access a file anywhere, plus using the string functions, would get you over the hurdle.
     
    the_dude_abides likes this.
  9. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    685
    Code:
    file = get_open_filename("text file|*.gml", "");
    if file != ""
    {
    file_text_open_read(file);
    location = file_text_read_string(file);
    file_text_close(file);
    }
    This gives the access to the file, getting past the sandbox. 'file' is being set to a value, rather than the empty string, so activates the code block.

    At which point it says that 'file' is not open. That is taken from an example given by the manual, and should allow access to the file. But GMS does not recognise it as a text file.
     
  10. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    816
    The example in the GMS1.4 manual for get_open_filename is in fact a bit misleading, since file_text_open_read returns the file handle that should be stored in a variable and used for subsequent file calls. Just calling the function to open the file makes no sense.

    Here's the revised code. I assume you need a local variable for the file path instead of an instance variable; if you don't know what I'm talking about please look for "Variables and scope" in the manual, it's important stuff.

    Code:
    var file = get_open_filename("text file|*.gml", "");
    
    if (file != "")
    {
        var file_handle = file_text_open_read(file);
        var location = file_text_read_string(file_handle);
        file_text_close(file_handle);
    }
    
     
    the_dude_abides likes this.
  11. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    685
    Thanks - I just twigged this mistake. So it was a coding error on my part all along *coughs* who'da'thunk'it.... :)
     
  12. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    685
    @chamaeleon @Catan
    Thanks for the help. Now I know where it was done incorrectly I'm sure progress can be made. Cheers!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice