Using a variable in a file path

● ARCHIVED · READ-ONLY
Started by manpaint 5 posts View original ↗
  1. Hello, I am seeking to use a variable in a path for conditional script reading (in events to be clear):

    Example:


    Code:
    File.file?('Tmp/1/something.data')

    There is a conditional branch that check if the file exists.

    What I need:
    Code:
    File.file?('Tmp/VAR1/something.data')

    I need the VAR1 to be replaced by an actual variable (or something easily settable with a script).

    Do anyone know how to do this?
  2. Does "an actual variable" mean a $game_variables variable?

    File.file?('Tmp/' + $game_variables[id] + '/something.data')

    and will only work when you have a game in progress, because $game_variables only exist, with valid values, while a game is in progress.
  3. Shaz said:
    Does "an actual variable" mean a $game_variables variable?

    File.file?('Tmp/' + $game_variables[id] + '/something.data')

    and will only work when you have a game in progress, because $game_variables only exist, with valid values, while a game is in progress.

    I had to be edit it a bit but yu were on the right path, thanks!

    What I ended up doing if anyone else wonder:
    Code:
    File.file?('Tmp/' + $game_variables[0042].to_s + '/something.data')

    I then just set the varaiable accordingly.

    Also something I noticed: If you set the variable to 1 but the folder is named 01, it will still work.
  4. Remove the 00 from the [0042] as it sometimes causes problems.

    Not sure why 1 and 01 work, as you don't have any formatting on that variable output. You don't happen to have both folders, do you? Check out the DataManager script and see what it does when it loads a map (I think the method is called load or load_map). You should be able to do the same thing, if you want to have leading zeros.
  5. If you want to have control over your file names I'd recommend using sprintf to format your string, that helps handling leading zeros.
    how to use sprintf
    Code:
    # Create your string first
    
    my_filepath_string = "Tmp/%d/something.data"

    This is how editable strings are declared in the Vocab module so you can use that as a reference. Using %d in a string means that when you use sprintf ruby replace that with an integer. If you want to have total control over leading zeros in that file path you can add a number starting with 0 between % and d.

    Code:
    my_filepath_string = "Tmp/%05d/something.data"

    What happens when you use sprintf is that %05d becomes a 5 digit number with leading zeros used to fill missing digits.

    Example:
    6 -> 00006
    153 -> 00153
    2445 -> 02445

    This way you know exactly how many leading zeros are there.

    What is left is just to use sprintf.

    Code:
    my_filepath_string = "Tmp/%05d/something.data"
    real_filepath_string = sprintf(my_filepath_string, $game_variables[id_goes_here])

    If you have doubts about how sprintf works in ruby you can check the documentation.

    In this example I used a variable to store the original path string. When you use it in your code it is much better if you change my_filepath_string to be a constant to avoid editing it by accident.

    Code:
    MY_FILEPATH_STRING = "Tmp/%05d/something.data"
    real_filepath_string = sprintf(MY_FILEPATH_STRING, $game_variables[id_goes_here])
    File.file?(real_filepath_string)
    
    # since sprintf returns the new string you should be able to use it this way as well
    
    MY_FILEPATH_STRING = "Tmp/%05d/something.data"
    File.file?(sprintf(MY_FILEPATH_STRING, $game_variables[id_goes_here]))

    I recommend using the 1st way so that if you ever need the new file path string to interact with the file later you can use it without having to use sprintf again (it takes more time than just calling a variable). Since File.file? only checks existence I suppose you will actually need it later.
    NOTE: it works even if you don't use sprintf since there are many ways to achieve something like this. I pointed this out because, as stated before, it gives you total control over leading zeros and the number of digits used. Knowing different ways of doing something gives you the possibility to adapt to the situation so maybe even if you don't need it now you find it useful to solve another situation later on.[/CODE]